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屋!