问题
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/;
}
Going to "/viewer/" in URL will proxy to the port 18080, just as expected.
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.
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!
回答1:
Adding a variable to proxy_pass
changes it's behaviour. You will need to construct the entire URI.
In your original configuration, the URI /viewer/foo
is translated to /Road/foo
before passing upstream.
In your new configuration, the URI /viewer/foo
is translated to /Road/?1
and the tail of the original URI is lost.
You may have more success using rewrite...break
to modify the URI.
For example:
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.
For example:
location /viewer/ {
rewrite ^/viewer(.*)$ /road$1 break;
proxy_pass http://xx.xxx.xxx.xxx:$myport;
}
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.
来源:https://stackoverflow.com/questions/58163580/nginx-infinite-reload-when-adding-variable-in-proxy-pass