Search code examples
nginx-reverse-proxy

Fixing nginx sub_filter?


I'm trying to fix a misbehaving app behind my reverse proxy - basically, it drops absolute URLs into pages.

My conf looks like

...

location /openproject/ {
    proxy_set_header Host <internal host>;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://<internal host>/openproject/;

    sub_filter 'http:<internal url>' 'https:<external url>';
    sub_filter_once on;
    sub_filter_last_modified on;
}
...

things in <> are replaced to protect my hosts.. and yes the misbehaving app is OpenProject

It doesn't seem to be working and debug doesn't say anything about substitutions happening..

Is there anything that I've done wrong?

nginx -V is

nginx version: nginx/1.18.0 built with OpenSSL 1.1.1k 25 Mar 2021 (running with OpenSSL 1.1.1o 3 May 2022) TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -ffile-prefix-map=/build/nginx-q9LD4J/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module


Solution

  • Found this https://marsbard.github.io/2016-07-30-replace-urls-behind-nginx-reverse-proxy/

    seems I needed the config to be:

    location /openproject/ {
    
            proxy_set_header Accept-Encoding ""; # no compression allowed or next won't work
    
            sub_filter "http://<internal host>/" "https://<external host>/";
            sub_filter_once off;
    
            proxy_set_header Host <internal host>;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Host $host:$server_port;
    
            proxy_pass http://<internal host>/openproject/;
    
    }