Nginx Relative Absolute Rewrite Rule URL?

Given the following configuration:

server { listen 80; server_name site.com; location /proxy { proxy_pass http://newsite.com/; } } 

The page hosted on site.com uses the relative path (/main.css) for all the static files, so when I get the proxied page, the browser requests static files with the wrong path:

i.e. .: http://site.com/main.css

How do I want these static files to be requested in the original location (http://newsite.com/main.css)?

+2
source share
1 answer

You can use the rewrite rule in your server section, something like the following (untested):

 server_name site.com; rewrite ^/([^/]+\.css)$ http://newsite.com/$1; 

This should redirect all requests for .css files to the top level directory of site.com at http://newsite.com/ . If necessary, edit the regular expression or add rewriting rules if you need to include other static files (images, scripts, etc.).

+4
source

Source: https://habr.com/ru/post/1208102/


All Articles