Nginx 1.18 - sub_filter + if statement

Ahmed Sajid ahmed.sajid at
Thu Sep 10 22:45:31 UTC 2020

Hi All,

Not sure if this question has been answered before. The only evidence I could find was a StackExchange question 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,

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list