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.

Raymond Ops
Raymond Ops
Raymond Ops
Mastering Nginx proxy_pass: How Slash Placement Affects URL Forwarding

1. Basic Rules

Assume the backend address is: http://127.0.0.1:8080/api/user/getById?id=123 Then:

1) Using Nginx forwarding, access via

http://127.0.0.1/api/user/getById?id=123
server {
    listen 80;
    server_name 127.0.0.1;

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

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
server {
    listen 80;
    server_name 127.0.0.1;

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

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/getById and front‑end address http://127.0.0.1/api/user/getById, the following combinations illustrate how the presence or absence of slashes in location and proxy_pass affect the final proxied URL.

1) Neither location nor proxy_pass ends with a slash

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

Resulting URL: http://127.0.0.1:8080/api/user/getById (correct).

2) Location ends with a slash, proxy_pass does not

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

Resulting URL: http://127.0.0.1:8080/api/user/getById (correct).

3) Location without slash, proxy_pass with trailing slash

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

Resulting URL: http://127.0.0.1:8080//user/getById (incorrect – double slash).

4) Both location and proxy_pass end with a slash

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

Resulting URL: http://127.0.0.1:8080/user/getById (incorrect – missing “api”).

5) Location without slash, proxy_pass includes “api”

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

Resulting URL: http://127.0.0.1:8080/api/user/getById (correct).

6) Location with slash, proxy_pass includes “api”

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

Resulting URL: http://127.0.0.1:8080/apiuser/getById (incorrect – missing slash).

7) Location without slash, proxy_pass includes “api/”

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

Resulting URL: http://127.0.0.1:8080/api//user/getById (incorrect – double slash).

8) Location with slash, proxy_pass includes “api/”

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

Resulting URL: http://127.0.0.1:8080/api/user/getById (correct).

In summary, the presence of trailing slashes in location and proxy_pass dramatically changes the forwarded URL; choose the configuration that matches your desired path structure.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendConfigurationHTTPNginxServerURL Rewriteproxy_pass
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.