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/