Nginx 1.18 - sub_filter + if statement

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Nginx 1.18 - sub_filter + if statement

Ahmed Sajid

Hi All,

Not sure if this question has been answered before. The only evidence I could find was a StackExchange question https://serverfault.com/questions/774750/use-of-sub-filter-in-if-block-under-nginx-config with no answer.

Here's what I'm trying to accomplish:

  • If the request is coming from a specific using agent, set override var to 1
  • under location block if override is 1 only then apply sub_filter

Here's the snippet:

    if ($http_user_agent = 'HTTPie/1.0.3') {
        set $override_content 1;
    }

    location / {
        proxy_set_header   Accept-Encoding "";
        proxy_pass  $upstream_endpoint;
        if ($override_content = 1) {
            sub_filter 'abcabcabcabc' 'xyzxyzxyzxyz';
            sub_filter_types 'application/json';
            sub_filter_once off;
        }
    }

I get error nginx: [emerg] "sub_filter" directive is not allowed here

When I remove the if block, there's no complain or error and Nginx runs fine.

Any idea why this doesn't work?

Best Regards,
Ahmed.


This email and any attachments are for the sole use of the intended recipients and may be privileged, confidential or otherwise exempt from disclosure under law. Any distribution, printing or other use by anyone other than the intended recipient is prohibited. If you are not an intended recipient, please contact the sender immediately, and permanently delete this email and its attachments.


_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

Re: Nginx 1.18 - sub_filter + if statement

J.R.
Check the 'context' for the sub_filter directives you are trying to
use. They do not say they can be used with 'if'.

http://nginx.org/en/docs/http/ngx_http_sub_module.html

Also worth reading about using 'if':

https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx