Handle nginx proxy_pass server paths

Consider the following nginx config blocks

location / {
    proxy_pass http://127.0.0.1:8080;
}

and

location / {
    proxy_pass http://127.0.0.1:8080/;
}

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

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

location /app {
    proxy_pass http://127.0.0.1:8080;
}

The above config is not same as below

location /app/ {
    proxy_pass http://127.0.0.1:8080/;
}

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 http://127.0.0.1:8080 /test1/foo/test /test1/foo/test
2 /test2 http://127.0.0.1:8080/ /test2/foo/test //foo/test
3 /test3/ http://127.0.0.1:8080 /test3/foo/test /test3/foo/test
4 /test4/ http://127.0.0.1:8080/ /test4/foo/test /foo/test
5 /test5 http://127.0.0.1:8080/app1 /test5/foo/test /app1/foo/test
6 /test6 http://127.0.0.1:8080/app1/ /test6/foo/test /app1//foo/test
7 /test7/ http://127.0.0.1:8080/app1 /test7/foo/test /app1foo/test
8 /test8/ http://127.0.0.1:8080/app1/ /test8/foo/test /app1/foo/test
9 / http://127.0.0.1:8080 /test9/foo/test /test9/foo/test
10 / http://127.0.0.1:8080/ /test10/foo/test /test10/foo/test
11 / http://127.0.0.1:8080/app1 /test11/foo/test /app1test11/foo/test
12 / http://127.0.0.1:8080/app2/ /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