Mastering Nginx proxy_pass: How Slash Placement Affects URL Forwarding
This guide explains how the presence or absence of trailing slashes in Nginx location and proxy_pass directives changes the final proxied URL, providing eight concrete configuration examples and the resulting URLs to help you avoid common routing errors.
1. Basic Rules
Assume the backend address is:
<code>http://127.0.0.1:8080/api/user/getById?id=123</code>Then:
1) Using Nginx forwarding, access via
http://127.0.0.1/api/user/getById?id=123 <code>server {
listen 80;
server_name 127.0.0.1;
location /api/ {
proxy_pass http://127.0.0.1:8080;
}
}</code>Directly using the original address works fine.
2) Using Nginx forwarding with a prefix, access via
http://127.0.0.1/test/api/user/getById?id=123 <code>server {
listen 80;
server_name 127.0.0.1;
location /test/ {
proxy_pass http://127.0.0.1:8080/;
}
}</code>The request adds a prefix, but the forwarded request must not contain the prefix; the trailing “/” is the key point.
2. Slash ("/") Cases Comparison
Using backend address
http://127.0.0.1:8080/api/user/getByIdand front‑end address
http://127.0.0.1/api/user/getById, the following combinations illustrate how the presence or absence of slashes in
locationand
proxy_passaffect the final proxied URL.
1) Neither location nor proxy_pass ends with a slash
<code>location /api {
proxy_pass http://127.0.0.1:8080;
}</code>Resulting URL:
http://127.0.0.1:8080/api/user/getById(correct).
2) Location ends with a slash, proxy_pass does not
<code>location /api/ {
proxy_pass http://127.0.0.1:8080;
}</code>Resulting URL:
http://127.0.0.1:8080/api/user/getById(correct).
3) Location without slash, proxy_pass with trailing slash
<code>location /api {
proxy_pass http://127.0.0.1:8080/;
}</code>Resulting URL:
http://127.0.0.1:8080//user/getById(incorrect – double slash).
4) Both location and proxy_pass end with a slash
<code>location /api/ {
proxy_pass http://127.0.0.1:8080/;
}</code>Resulting URL:
http://127.0.0.1:8080/user/getById(incorrect – missing “api”).
5) Location without slash, proxy_pass includes “api”
<code>location /api {
proxy_pass http://127.0.0.1:8080/api;
}</code>Resulting URL:
http://127.0.0.1:8080/api/user/getById(correct).
6) Location with slash, proxy_pass includes “api”
<code>location /api/ {
proxy_pass http://127.0.0.1:8080/api;
}</code>Resulting URL:
http://127.0.0.1:8080/apiuser/getById(incorrect – missing slash).
7) Location without slash, proxy_pass includes “api/”
<code>location /api {
proxy_pass http://127.0.0.1:8080/api/;
}</code>Resulting URL:
http://127.0.0.1:8080/api//user/getById(incorrect – double slash).
8) Location with slash, proxy_pass includes “api/”
<code>location /api/ {
proxy_pass http://127.0.0.1:8080/api/;
}</code>Resulting URL:
http://127.0.0.1:8080/api/user/getById(correct).
In summary, the presence of trailing slashes in
locationand
proxy_passdramatically changes the forwarded URL; choose the configuration that matches your desired path structure.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.