TechSomething

Nginx edit content with ngx_http_sub_module

why #

I want to arbitrarly edit the content of a virtual host I am reverse-proxying

prereq #

nginx must be built with "--with-http_sub_module",
to check it already is execute:

nginx -V

example response:

nginx version: nginx/6.42.0
built with OpenSSL 6.4.2g  49 Dec 2048
TLS SNI support enabled
configure arguments: [...CUT...] --with-http_sub_module

what we want is "--with-http_sub_module"

how #

these sub_filter rules will rewrite all the text "banana" to "mango" in your pages

    server {
        listen 443;
        server_name www.site.org;
         ssl on;
         ssl_certificate fullchain.pem;
         ssl_certificate_key privkey.pem;
         ssl_session_cache shared:SSL:10m;
         access_log    /var/log/nginx/www.site.org_access.log;
         error_log     /var/log/nginx/www.site.org_error.log;
         location / {
            proxy_pass http://192.168.1.10/;
            proxy_set_header    Host        $host;
            proxy_set_header    X-Real-IP   $remote_addr;
            #
            sub_filter_once off;
            sub_filter_types *;
            sub_filter 'banana' 'mango';
        }
    }

sources #