Skip to content
How to Configure Nginx proxy_pass Slashes Correctly to Avoid Double‑Slash Errors

How to Configure Nginx proxy_pass Slashes Correctly to Avoid Double‑Slash Errors

This guide explains the impact of trailing slashes in Nginx location and proxy_pass directives, shows common configuration patterns, and provides concrete examples to ensure URLs are forwarded correctly without duplicate slashes or missing path segments.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations

1. Basic Rules

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

1) Use Nginx forwarding and 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;
    }
}

Accessing the original address directly is very simple.

2) Use Nginx forwarding with a prefix and 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/;
    }
}

Here a prefix is added to the request, but the forwarded request must not contain the prefix, so the trailing "/" after the address needs to be removed.

2. Slash Cases Comparison

Using the service address http://127.0.0.1:8080/api/user/getById, the access address is http://127.0.0.1/api/user/getById. The following cases illustrate how the presence or absence of slashes in location and proxy_pass affect the final URL.

1) Neither location nor proxy_pass has a trailing slash

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

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

2) location has a trailing slash, proxy_pass does not

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

Resulting proxy 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 proxy URL: http://127.0.0.1:8080//user/getById (incorrect – double slash).

4) Both location and proxy_pass have trailing slashes

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

Resulting proxy URL: http://127.0.0.1:8080/user/getById (incorrect – missing "api" segment).

5) location without slash, proxy_pass adds "api"

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

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

6) location with slash, proxy_pass adds "api"

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

Resulting proxy URL: http://127.0.0.1:8080/apiuser/getById (incorrect – missing slash between "api" and "user").

7) location without slash, proxy_pass adds "api/"

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

Resulting proxy URL: http://127.0.0.1:8080/api//user/getById (incorrect – double slash causes backend authentication failures).

8) location with slash, proxy_pass adds "api/"

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

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

In summary, the presence or absence of trailing slashes in location and proxy_pass significantly 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.

Backendlocationproxy_passTrailing Slash
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.