Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

loops - Nginx - Infinite reload when adding variable in proxy_pass

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...