How the Trailing Slash in Nginx proxy_pass Changes Request Routing

This article examines how the presence or absence of a trailing slash in Nginx's location blocks and proxy_pass directives affects URL matching and request forwarding, illustrating four proxy_pass scenarios with examples and providing configuration experiments to help avoid common pitfalls.

Open Source Linux
Open Source Linux
Open Source Linux
How the Trailing Slash in Nginx proxy_pass Changes Request Routing

Why the trailing slash matters in Nginx

Nginx is a lightweight, high‑performance web server widely used by large Chinese internet companies such as Baidu, Alibaba, Tencent, ByteDance, Meituan, and Didi. Because configuration files directly control request routing, a missing character—especially a "/"—can cause serious access errors. This article explores the impact of the slash in proxy_pass and location directives.

Location block matching explained

Each location block defines a URL prefix that Nginx matches from top to bottom. When a request arrives, Nginx parses the request URI and selects the first matching location, then applies the directives inside its braces.

Example configuration for the URL http://www.wandouduoduo.com/wddd/index.html:

location /wddd/ {
    proxy_connect_timeout 18000;   # 5 hours
    proxy_send_timeout    18000;
    proxy_read_timeout    18000;
    proxy_pass http://127.0.0.1:8080;
}

When this location matches, the request is forwarded to the local Tomcat service on port 8080, and the response is returned unchanged.

If a location does not end with "/", the match is fuzzy and will apply to any URI that starts with the given string; if it ends with "/", the match is exact.

Four proxy_pass variations and their effects

1. Adding a trailing slash

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

Result: the request is proxied to http://127.0.0.1:8080/index.html.

2. Omitting the trailing slash

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

Result: the request is proxied to http://127.0.0.1:8080/wddd/index.html.

3. Adding a directory with a trailing slash

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

Result: the request is proxied to http://127.0.0.1:8080/sun/index.html.

4. Adding a directory without a trailing slash

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

Result: the request is proxied to http://127.0.0.1:8080/sunindex.html (note the missing slash).

Key takeaways

Appending "/" to a location makes the match exact, while omitting it allows fuzzy matching. For proxy_pass, the presence or absence of a trailing slash determines whether the upstream URL is concatenated directly with the original request URI or replaces it.

To solidify understanding, try the following experimental configuration:

server {
    listen 80;
    server_name localhost;

    # Example 1: /wddd01/ -> http://localhost:8080/wddd01/xxx
    location /wddd01/ {
        proxy_pass http://localhost:8080;
    }

    # Example 2: /wddd02/ -> http://localhost:8080/xxx
    location /wddd02/ {
        proxy_pass http://localhost:8080/;
    }

    # Example 3: /wddd03 -> http://localhost:8080/wddd03xxx
    location /wddd03 {
        proxy_pass http://localhost:8080;
    }

    # Example 4: /wddd04 -> http://localhost:8080//xxx (double slash)
    location /wddd04 {
        proxy_pass http://localhost:8080/;
    }

    # Example 5: /wddd05/ -> http://localhost:8080/hahaxxx (missing slash)
    location /wddd05/ {
        proxy_pass http://localhost:8080/haha;
    }

    # Example 6: /wddd06/ -> http://localhost:8080/haha/xxx
    location /wddd06/ {
        proxy_pass http://localhost:8080/haha/;
    }

    # Example 7: /wddd07 -> http://localhost:8080/haha/xxx
    location /wddd07 {
        proxy_pass http://localhost:8080/haha;
    }

    # Example 8: /wddd08 -> http://localhost:8080/haha//xxx (double slash)
    location /wddd08 {
        proxy_pass http://localhost:8080/haha/;
    }
}

Experimenting with these configurations helps clarify how Nginx builds the final upstream URL based on the slash usage.

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.

NginxWeb serverlocationproxy_passtrailing-slash
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.