NginX trailing slash in proxy proxy

I know this question has been asked several times, but after trying many solutions, I am still stuck.

I use NginX to proxy access the NodeJs application. Am I trying to make the proxy url https://example.com pass the request http://example.com:8080/?

Requests (from a mobile application) request https://example.com// , which nginx creates http://example.com:8080//? which does not work.

Things I tried that didn't work:

  • merge_slashes on; (which is enabled by default)
  • rewrite ^/(.*)/$ /$1 permanent;
  • port_in_redirect off;

My nginx config:

 location = /a { proxy_pass http://127.0.0.1:8080; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_read_timeout 300; } location ^~ /a/ { proxy_pass http://127.0.0.1:8080; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_read_timeout 300; } 
+8
proxy nginx proxypass
source share
1 answer

I assume that you are using proxy_pass without a URI. In this case, nginx passes the original URI as it was, not normalized. Therefore, you should use proxy_pass http://backend/;

UPDATE

So, I was right. Just add the URI part to proxy_pass as follows:

 location = /a { proxy_pass http://127.0.0.1:8080/a; ... } location ^~ /a/ { proxy_pass http://127.0.0.1:8080/a/; ... } 

As indicated in the nginx documentation , if proxy_pass used without a URI (i.e. without a path after the server: port), nginx will put the URI from the original request in the same way as with all double slashes, ../ etc.

On the other hand, the URI in proxy_pass acts like alias , meaning that nginx will replace the part that matches the location prefix (in (I intentionally did the same as the location prefix), so the URI will be the same as the requested one, but normalized (without collages of shares and all this staff). Be careful with the final slashes. Nginx literally replaces the part, and you can get some strange url.

Here is an example with a trailing slash in location , but don't scroll in proxy_pass .

 location /one/ { proxy_pass http://127.0.0.1:8080/two; ... } 

if you go to the address http://yourserver.com/one/path/here?param=1 nginx will ask for a proxy server for http://127.0.0.1/twopath/here?param=1 . See How two and path combine.

+11
source share

All Articles