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

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

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

teward
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 <a href="http://172.22.3.16:8000;">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

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|

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

Francis Daly
On Wed, Feb 05, 2020 at 07:04:09AM -0500, !QVAS! wrote:

Hi there,

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

>      server {
>             listen       80;
>             server_name  172.22.3.15;
>
>             location / {
>                 proxy_pass <a href="http://172.22.3.16:8000;">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.

When I use your config, it seems to work for me.

Can you show one request and the matching incorrect response?

Small is good; something like

  curl -v http://examplesite.com/new/a.js

should work -- the matching file a.js in the 172.22.3.16:8000 document
root should have suitable content.

  curl -v http://172.22.3.15/a.js
  curl -v http://172.22.3.16:8000/a.js

might also be useful, to compare.

        f
--
Francis Daly        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx