简体   繁体   English

使用nginx代理/重写,我可以将原始URL保留在浏览器的位置字段中吗?

[英]With nginx proxy/rewrite can I keep the original URL in the browser's Location field?

Using nginx.conf features like proxy-pass / rewrite , can I keep the original URL in the browser's Location field? 使用像proxy-pass / rewrite这样的nginx.conf功能,我可以将原始 URL保存在浏览器的Location字段中吗?

I have several PlayFramework apps running on different ports (9001, 9002, ...) with proxy forwarding set up via nginx.conf . 我有几个PlayFramework应用程序在不同的端口(9001,9002,...)上运行,并通过nginx.conf设置代理转发。 People browse to them as: 人们浏览他们:

  • http://domain.name/App1/
  • http://domain.name/App2/
  • etc . 等等

My nginx.conf entries look like this: 我的nginx.conf条目如下所示:

location /App1/ {
    proxy_pass http://localhost:9001/;
    rewrite ^/App1/(.*) http://domain.name:9001/$1;
}

If I ask for http://domain.name/App1/ , what I see in the browser's Location field is http://domain.name:9001 . 如果我要求http://domain.name/App1/ ,我在浏览器的位置字段中看到的是http://domain.name:9001 What I wish I saw was http://domain.name/App1/ , that is, I want the name App1 to remain in the URI, and I'd rather not expose the port number. 我希望我看到的是http://domain.name/App1/ ,也就是说,我希望名称 App1保留在URI中,而我宁愿不公开端口号。

Let's say App1 has a link /location/ABC . 假设App1有一个链接/location/ABC When I click on it I see http://domain.name:9001/location/ABC when I wish I saw http://domain.name/App1/location/ABC . 当我点击它时,当我希望看到http://domain.name:9001/location/ABC时,我会看到http://domain.name/App1/location/ABC

Can I achieve this with nginx.conf ? 我可以用nginx.conf实现这个目的吗?

PS I put http://domain.name explicitly in the rewrite rule because without it I was getting localhost in the browser, and my browser's localhost is not the same as the server's. PS我在重写规则中明确地放了http://domain.name ,因为没有它我在浏览器中得到localhost ,而我的浏览器的localhost与服务器的不同。

Rewrites issue redirects for browser. 重写浏览器的问题重定向。

If you just want to mount several locations from upstreams - you do not need rewrites, just use: 如果您只想从上游安装多个位置 - 您不需要重写,只需使用:

location /App1/ {
    proxy_pass http://localhost:9001/;
}

But apps should use relative links or account for their absolute location. 但应用程序应使用相对链接或帐户作为其绝对位置。

For more complex url manipulation you can use break -rewrites: 对于更复杂的url操作,您可以使用break -rewrites:

location /App1/ {
    rewrite ^/App1/(.*) /$1 break;
    proxy_pass http://localhost:9001;
}

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

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