Why the Trailing Slash Matters in Nginx proxy_pass and location Directives
Understanding how the presence or absence of a trailing slash in Nginx’s location and proxy_pass directives changes request routing, with detailed examples and four test cases, helps prevent misconfigurations that can cause broken links or unexpected redirects in web applications.
Background
Nginx is a lightweight, high‑performance web server widely used by large Chinese internet companies such as Baidu, Alibaba, Tencent, Toutiao, Meituan and Didi. Because its configuration is critical, a missing “/” in a proxy_pass directive can cause access errors, which this article investigates.
Detailed Explanation of location Matching
Each location block matches a request URI. Nginx parses the request path from top to bottom and applies the first matching block.
Example configuration for the request http://www.wandouduoduo.com/wddd/index.html:
location /wddd/ {
proxy_connect_timeout 18000;
proxy_send_timeout 18000;
proxy_read_timeout 18000;
proxy_pass http://127.0.0.1:8080;
}When this location matches, the request is proxied to the local Tomcat service on port 8080, which returns the response.
Summary: If a location ends with “/”, it matches the exact directory; without “/”, it performs a prefix (fuzzy) match.
For example, location /wandou matches /wandou, /wandouduoduo, etc., while location /wandou/ matches only the exact /wandou/ path.
Four Cases of proxy_pass with and without “/”
All requests use the same base URL http://www.wandouduoduo.com/wddd/index.html and match the /wddd/ location.
Case 1: With “/”
location /wddd/ {
proxy_pass http://127.0.0.1:8080/;
}Result: request is proxied to http://127.0.0.1:8080/index.html.
Case 2: Without “/”
location /wddd/ {
proxy_pass http://127.0.0.1:8080;
}Result: request is proxied to http://127.0.0.1:8080/wddd/index.html.
Case 3: Adding a directory with “/”
location /wddd/ {
proxy_pass http://127.0.0.1:8080/sun/;
}Result: request is proxied to http://127.0.0.1:8080/sun/index.html.
Case 4: Adding a directory without “/”
location /wddd/ {
proxy_pass http://127.0.0.1:8080/sun;
}Result: request is proxied to http://127.0.0.1:8080/sunindex.html (missing slash).
Conclusion
Appending “/” to a location makes it match only the exact directory, while omitting it allows prefix matching. For proxy_pass, the presence or absence of a trailing slash determines how the upstream URL is concatenated.
Try the following configuration to experiment:
server {
listen 80;
server_name localhost;
location /wddd01/ {
proxy_pass http://localhost:8080;
}
location /wddd02/ {
proxy_pass http://localhost:8080/;
}
location /wddd03 {
proxy_pass http://localhost:8080;
}
location /wddd04 {
proxy_pass http://localhost:8080/;
}
location /wddd05/ {
proxy_pass http://localhost:8080/haha;
}
location /wddd06/ {
proxy_pass http://localhost:8080/haha/;
}
location /wddd07 {
proxy_pass http://localhost:8080/haha;
}
location /wddd08 {
proxy_pass http://localhost:8080/haha/;
}
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
