<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<p>Hi All,</p>
<p>Not sure if this question has been answered before. The only evidence I could find was a StackExchange question
<a class="moz-txt-link-freetext" href="https://serverfault.com/questions/774750/use-of-sub-filter-in-if-block-under-nginx-config">
https://serverfault.com/questions/774750/use-of-sub-filter-in-if-block-under-nginx-config</a> with no answer.</p>
<p>Here's what I'm trying to accomplish:</p>
<ul>
<li>If the request is coming from a specific using agent, set override var to 1</li><li>under location block if override is 1 only then apply sub_filter</li></ul>
<p>Here's the snippet:</p>
<p><font face="Courier New, Courier, monospace">    if ($http_user_agent = 'HTTPie/1.0.3') {<br>
        set $override_content 1;<br>
    }<br>
<br>
    location / {<br>
        proxy_set_header   Accept-Encoding "";<br>
        proxy_pass  $upstream_endpoint;<br>
        if ($override_content = 1) {<br>
            sub_filter 'abcabcabcabc' 'xyzxyzxyzxyz';<br>
            sub_filter_types 'application/json';<br>
            sub_filter_once off;<br>
        }<br>
    }</font><br>
</p>
<p>I get error <b>nginx: [emerg] "sub_filter" directive is not allowed here</b></p>
<p>When I remove the <b>if</b> block, there's no complain or error and Nginx runs fine.<br>
</p>
<p>Any idea why this doesn't work?</p>
<p>Best Regards,<br>
Ahmed.<br>
</p>
<p><br>
</p>
<div>
<p id="body" style="font-size:7.5pt; color:darkgray; line-height:10pt; font-family: 'Arial','times roman',serif;">
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.</p>
</div>
</body>
</html>