Handle nginx proxy_pass server paths

Consider the following nginx config blocks

location / {


location / {

They are both same in terms of final result. You if call /foo/bar on the main server it passes the request to

But as soon as you have a location block with a non-root path like below

location /app {

The above config is not same as below

location /app/ {

Below table shows different combinations of the location and the proxy_pass url and the final path that the backend server will receive

Proxy Pass and Paths

Case # Nginx location proxy_pass URL Test URL Path received
1 /test1 /test1/foo/test /test1/foo/test
2 /test2 /test2/foo/test //foo/test
3 /test3/ /test3/foo/test /test3/foo/test
4 /test4/ /test4/foo/test /foo/test
5 /test5 /test5/foo/test /app1/foo/test
6 /test6 /test6/foo/test /app1//foo/test
7 /test7/ /test7/foo/test /app1foo/test
8 /test8/ /test8/foo/test /app1/foo/test
9 / /test9/foo/test /test9/foo/test
10 / /test10/foo/test /test10/foo/test
11 / /test11/foo/test /app1test11/foo/test
12 / /test12/foo/test /app2/test12/foo/test

Note: When you have a trailing / in the proxy_pass url then the url mentioned in the location block is removed from the actual url starting and rest of the pass is sent to the proxied server as seen in Case #4