Nginx - 在 proxy_pass 中添加变量时无限重载 [英] Nginx - Infinite reload when adding variable in proxy_pass
问题描述
我正在 Docker 上使用 Nginx,我想将每个用户分配到不同的端口.
I am working with Nginx on Docker and I want to assign each user to a different port.
首先,没有添加任何东西,我的代码工作正常:
First, without adding anything, my code works fine:
location /viewer/ {
proxy_pass http://xx.xxx.xxx.xxx:18080/Road/;
}
转到 URL 中的/viewer/"将代理到端口 18080,正如预期的那样.
Going to "/viewer/" in URL will proxy to the port 18080, just as expected.
但是如果我向 proxy_pass 添加任何变量,例如:
But if I add any variable to the proxy_pass like:
set $test 1;
proxy_pass http://xx.xxx.xxx.xxx:18080/Road/?$test;
然后,首先,静态文件不再加载,我必须添加如下行:
then, first of all, the static files do not load anymore and I have to add lines like these:
location ~ .css {
add_header Content-Type text/css;
}
location ~ .js {
add_header Content-Type application/x-javascript;
}
此后,静态文件再次工作,但页面开始无限重新加载.
After this, the static files work again but the page starts to reload infinitely.
之前我认为这是因为我用 proxy_pass 中的变量替换了端口,但正如我向您展示的那样,当我在那里添加任何变量时就会发生这种情况.
Before I was thinking it was because I replaced the port by a variable in proxy_pass, but as I showed you it happens when I add any variable there.
你认为我做错了什么?感谢您的帮助!
What do you think I could do wrong? Thank you for your help!
推荐答案
向 proxy_pass
添加一个变量会改变它的行为.您将需要构建整个 URI.
Adding a variable to proxy_pass
changes it's behaviour. You will need to construct the entire URI.
在您的原始配置中,URI /viewer/foo
在传递到上游之前被转换为 /Road/foo
.
In your original configuration, the URI /viewer/foo
is translated to /Road/foo
before passing upstream.
在您的新配置中,URI /viewer/foo
被转换为 /Road/?1
并且原始 URI 的尾部丢失.
In your new configuration, the URI /viewer/foo
is translated to /Road/?1
and the tail of the original URI is lost.
使用 rewrite...break
修改 URI 可能会取得更大的成功.
You may have more success using rewrite...break
to modify the URI.
例如:
location /viewer/ {
rewrite ^/viewer(.*)$ /road$1?something break;
proxy_pass http://xx.xxx.xxx.xxx:18080;
}
有关详细信息,请参阅本文档.
See this document for details.
根据您的评论,您希望更改目标端口.
According to your comment, you wish to change the destination port.
例如:
location /viewer/ {
rewrite ^/viewer(.*)$ /road$1 break;
proxy_pass http://xx.xxx.xxx.xxx:$myport;
}
如果您通过 IP 地址指定上游服务器,则不需要 resolver
语句.但是,如果您按名称指定上游,则需要定义一个 resolver
.有关详细信息,请参阅本文档.
If you specify the upstream server by IP address, a resolver
statement will not be required. But if you specify the upstream by name, you will need to define a resolver
. See this document for details.
这篇关于Nginx - 在 proxy_pass 中添加变量时无限重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!