Nginx's sub_filter does not replace paths in CSS and JS

!QVAS! nginx-forum at forum.nginx.org
Wed Feb 5 12:04:09 UTC 2020


I have two nginx servers, let's call them nginx1 and nginx2. Nginx1 is
located in a separate infrastructure and processes requests, and also
provides static files for examplesite.com. Nginx2 is in a different
infrastructure and processes requests, and also gives static files on
request 172.22.3.15. It is necessary to make the page 172.22.3.15 open at
the request of examplesite.com/new. I realized that in order for the page to
be processed correctly, you need to edit the paths to static files.

To do this, a configuration was added to the nginx1 file location:

location /new {
        proxy_pass http://172.22.3.15/;
    }

The following settings were added to the nginx2 configuration file:

proxy_set_header X-Forwarded-Proto $scheme; 
proxy_set_header Host $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-Server $host;
proxy_redirect off;

     server {
            listen       80;
            server_name  172.22.3.15;
            client_max_body_size 32m;
            charset utf-8;
            location /public/ {
                alias /usr/src/app/public/;
            }
            location /static/ {
                alias /usr/src/app/static/;
            }

            location / {
                proxy_pass http://172.22.3.16:8000;
                proxy_redirect / /new;
                sub_filter '"/' '"/new/';
                sub_filter "'/" "'/new/";
                sub_filter_types *;
                sub_filter_once off;
                sub_filter_last_modified on;
                proxy_set_header Accept-Encoding "";
            }
        }
But the problem is that in the CSS and JS files themselves, the paths are
not changed. I tried to disable gzip in my configuration, but it did not
bring any results. Version nginx1 1.17, and version nginx2 1.16.

P.S. I configure sub_filter to nginx2, since I do not have access to nginx1,
since this is the customer’s server. I recreated a similar situation on test
projects (by analogy with the production environment, let's name nginxes as
nginx1 and nginx2) and set up sub_filter in the nginx1 configuration file (I
didn’t make any additional settings on nginx2). But got the same problem.
The test setup of nginx1 looks like this:

location /test {
        proxy_pass http://192.168.29.32/;
        proxy_redirect / /test;
        sub_filter '"/' '"/test/';
        sub_filter "'/" "'/test/";
        sub_filter_types *;
        sub_filter_once off;
        sub_filter_last_modified on;
        proxy_set_header Accept-Encoding "";

    }

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,286944,286944#msg-286944



More information about the nginx mailing list