Backend Development 7 min read

Understanding Nginx location Matching and the Effect of Trailing Slash in proxy_pass

This article explains how Nginx location directives match request paths and demonstrates the four different behaviors of proxy_pass when the trailing slash is present or omitted, using concrete configuration examples and test results to clarify the impact on URL rewriting and request forwarding.

Top Architect
Top Architect
Top Architect
Understanding Nginx location Matching and the Effect of Trailing Slash in proxy_pass

Nginx is a lightweight, high‑performance web server widely adopted by large internet companies because of its low memory footprint and strong concurrency. Proper configuration of its location blocks and proxy_pass directives is crucial, as a missing or extra slash can lead to unexpected routing errors.

Each location block matches request URIs from top to bottom; a trailing slash in the location path forces an exact match, while omitting the slash allows a prefix (fuzzy) match. The article uses the example URL http://www.wandouduoduo.com/wddd/index.html with the following basic configuration:

location /wddd/ {
    proxy_connect_timeout 18000;  ## modify to half an hour
    proxy_send_timeout    18000;
    proxy_read_timeout    18000;
    proxy_pass            http://127.0.0.1:8080;
}

Four variations of proxy_pass are then tested:

1. With a trailing slash in proxy_pass :

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

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

2. Without a trailing slash:

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

Result: request is forwarded 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: request is forwarded 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: request is forwarded to http://127.0.0.1:8080/sunindex.html (notice the missing slash).

The summary states that a trailing slash in the location path restricts matching to that exact directory, while omitting it enables prefix matching; however, the presence or absence of a slash in proxy_pass determines how the upstream URL is concatenated.

To deepen understanding, the article provides a comprehensive test configuration with multiple location blocks illustrating various combinations of slashes and their effects:

server {
  listen 80;
  server_name localhost;

  # /wddd01/ -> /wddd01/ (no slash in proxy_pass)
  location /wddd01/ {
    proxy_pass http://localhost:8080;
  }

  # /wddd02/ -> / (slash in proxy_pass)
  location /wddd02/ {
    proxy_pass http://localhost:8080/;
  }

  # /wddd03 (no trailing slash in location) -> prefix match
  location /wddd03 {
    proxy_pass http://localhost:8080;
  }

  # /wddd04 (no slash in location, slash in proxy_pass) -> double slash in result
  location /wddd04 {
    proxy_pass http://localhost:8080/;
  }

  # /wddd05/ (slash in location, no slash in proxy_pass) -> missing slash between path parts
  location /wddd05/ {
    proxy_pass http://localhost:8080/haha;
  }

  # /wddd06/ (slash in location, slash after path in proxy_pass) -> proper concatenation
  location /wddd06/ {
    proxy_pass http://localhost:8080/haha/;
  }

  # /wddd07 (no slash in location, no slash after path) -> similar to previous case
  location /wddd07 {
    proxy_pass http://localhost:8080/haha;
  }

  # /wddd08 (no slash in location, slash after path) -> double slash in result
  location /wddd08 {
    proxy_pass http://localhost:8080/haha/;
  }
}

Readers are encouraged to experiment with these configurations to observe how subtle differences in slash placement affect request routing.

backendConfigurationNginxweb serverlocationproxy_pass
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login 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.