Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

Nginx trailing slash in proxy pass url

Writer Andrew Mclaughlin

I am using nginx to proxy pass to a NodeJS application. I'm trying to make the URL proxy pass the request to

Requests (from a mobile app) are requesting which nginx is making which is not working.

Things I have tried which haven't worked:

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

My nginx config:

location = /a { proxy_pass 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 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_read_timeout 300;
}
1

2 Answers

I guess you use proxy_pass without URI. In that case nginx pass original URI as it was, not normalized one. So you should use proxy_pass

UPDATE

So I was right. Just add URI part to proxy_pass like this:

location = /a { proxy_pass ...
}
location ^~ /a/ { proxy_pass ...
}

As stated in nginx documentation if proxy_pass used without URI (i.e. without path after server:port) nginx will put URI from original request exactly as it was with all double slashes, ../ and so on.

On the other side, URI in proxy_pass acts like alias directive, means nginx will replace part that matches location prefix (in our case it's /a in first location and /a/ is second) with URI in proxy_pass directive (which I intentionally made the same as location prefix) so URI will be the same as requested but normalized (without double slashes and all that staff). Be careful with trailing slashes. Nginx replaces part literally and you could end up with some strange url.

Here is an example with trailing slash in location, but no trailing slash in proxy_pass.

location /one/ { proxy_pass ...
}

If you go to address nginx will proxy request to . See how two and path concatenates.

6

You might add these lines:

proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Example, I proxy public port 80 to local port 880

location / { proxy_redirect off; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 16k; proxy_buffers 32 8k; proxy_busy_buffers_size 64k; proxy_pass }

Note: Make sure to restart nginx. You might need to use another browser to test the new configuration (avoid cache)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.