Trailing Slash in nginx has been giving me some sleepless nights lately. Requesting some help with this
Question : Strange trailing slash behavior in proxy_pass
.
So why would this work :
location /myapi/ {
proxy_pass http://node_server8/;
}
and this won't
location /myapi/ {
proxy_pass http://node_server8;
}
Notice the missing trailing slash at the end of http://node_server8
in second code block. This is specially strange as I have a few other configurations where I don't have a trailing slash on the backend and all works fine.
They are totally different.
In the first proxy_pass
statement you have included a URI parameter with a value of /
. In the second you haven't.
When you give proxy_pass
a URI parameter (within a prefix location
), it transforms the requested URI similarly to the alias
function, whereby the value of the location
directive is substituted for the value of the URI parameter. For example /myapi/foo
becomes /foo
before being passed upstream.
If you do not provide proxy_pass
with a URI parameter, no transformation takes place, and the request /myapi/foo
is passed upstream unchanged.
See this document for details.