简体   繁体   English

nginx url 重写内部位置

[英]nginx url rewrite inside location

I have two application running in 8080 & 5999 ports .我有两个应用程序在 8080 和 5999 端口上运行。 I want use nginx to proxy two application as /rss & /demo .我想使用 nginx 将两个应用程序代理为/rss/demo

But the problem I'm facing is that css, javascript are not loading .但我面临的问题是 css, javascript 没有加载。

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.其次,您需要确保您的 CSS 等在您的 HTML 中被正确引用。

If a CSS file was referenced like so in the HTML, before proxying:如果在 HTML 中像这样引用 CSS 文件,则在代理之前:

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:然后,您需要以某种方式在 HTML 中进行引用,以在代理后说明新位置(包括子目录):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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