简体   繁体   中英

nginx url rewrite inside location

I have two application running in 8080 & 5999 ports . I want use nginx to proxy two application as /rss & /demo .

But the problem I'm facing is that css, javascript are not loading .

location /rss {
  rewrite ^/rss(.*) /$1 break;
  proxy_pass http://localhost:8080/;
  proxy_redirect off;
}

location /demo {
  rewrite ^/demo(.*)$ /$1 break;
  proxy_pass http://localhost:5999/;
  proxy_redirect off;
}

Can someone please help me correct this one ...

Firstly, I think the below is a bit neater (avoiding double slashes in the proxied request):

location /rss/ {
  rewrite ^/rss(/.*) $1 break;
  proxy_pass http://localhost:8080;
  proxy_redirect off;
}

location /demo/ {
  rewrite ^/demo(/.*)$ $1 break;
  proxy_pass http://localhost:5999;
  proxy_redirect off;
}

Secondly, you need to make sure your CSS, etc. is being referenced in your HTML correctly.

If a CSS file was referenced like so in the HTML, before proxying:

http://localhost:8080/styles/application.css

Then you need to somehow make the reference in the HTML like this to account for the new location (including sub-directory) after proxying:

http://notproxied.com/rss/styles/application.css

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM