Skip to main content

NGINX - Replace strings in output with sub_filter

Category: NGINX
nginx

This page includes external resources from a Content Delivery Network for performance optimization. Those resources will get blocked when accessing this site as hidden service via Tor Browser.

Because of that I was looking for a solution without adding more complexity on the CMS side to replace external with internal resources in page output. Therefore I've added the following directives to the specific nginx server block:

# configuration for php files
location ~ \.php$ {
    #[..]
    include     fastcgi_params;
            
    #only accept clear type content
    fastcgi_param HTTP_ACCEPT_ENCODING "";    
    proxy_set_header Accept-Encoding "";

    sub_filter 'https://cdn.website.tld'     'http://$http_host' ;
    sub_filter 'cdn.website.tld'     '$http_host' ;
    # replace all
    sub_filter_once off;
 }

Please take care that gzip compression is deactivated.