使用nginx代理/重写,我可以将原始URL保留在浏览器的“位置"字段中吗? [英] With nginx proxy/rewrite can I keep the original URL in the browser's Location field?
问题描述
使用proxy-pass
/rewrite
之类的nginx.conf
功能,我可以在浏览器的位置"字段中保留原始 URL吗?
Using nginx.conf
features like proxy-pass
/rewrite
, can I keep the original URL in the browser's Location field?
我有几个在不同端口(9001、9002等)上运行的PlayFramework应用程序,并通过nginx.conf
设置了代理转发.人们以以下方式浏览他们:
I have several PlayFramework apps running on different ports (9001, 9002, ...) with proxy forwarding set up via nginx.conf
. People browse to them as:
-
http://domain.name/App1/
-
http://domain.name/App2/
- 等.
http://domain.name/App1/
http://domain.name/App2/
- etc.
我的nginx.conf
条目如下所示:
location /App1/ {
proxy_pass http://localhost:9001/;
rewrite ^/App1/(.*) http://domain.name:9001/$1;
}
如果我要求输入http://domain.name/App1/
,则在浏览器的位置"字段中看到的是http://domain.name:9001
.我希望看到的是http://domain.name/App1/
,也就是说,我希望名称 App1保留在URI中,而我不想公开端口号.
If I ask for http://domain.name/App1/
, what I see in the browser's Location field is 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.
比方说App1有一个链接/location/ABC
.当我单击它时,我希望看到http://domain.name/App1/location/ABC
.
Let's say App1 has a link /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
.
我可以用nginx.conf
实现吗?
P.S.我将http://domain.name
明确地放在了重写规则中,因为没有它,我将无法在浏览器中获得localhost
,并且我的浏览器的本地主机与服务器的本地主机不同.
P.S. 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.
推荐答案
重写浏览器的问题重定向.
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.
对于更复杂的url操作,您可以使用break
-rewrites:
For more complex url manipulation you can use break
-rewrites:
location /App1/ {
rewrite ^/App1/(.*) /$1 break;
proxy_pass http://localhost:9001;
}
这篇关于使用nginx代理/重写,我可以将原始URL保留在浏览器的“位置"字段中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!