Nginx - Infinite reload when adding variable in proxy_pass

让人想犯罪 __ 提交于 2021-02-15 03:01:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!