Why a Missing '/' in Nginx proxy_pass Can Break Your Site
This article explains how Nginx location matching works, why the presence or absence of a trailing slash in both location blocks and proxy_pass directives changes request routing, and provides concrete configuration examples to avoid common pitfalls.
Introduction
Nginx is a lightweight, high‑performance web server widely adopted by large Chinese internet companies such as Baidu, Alibaba, Tencent, Toutiao, Meituan, and Didi. Because Nginx relies heavily on precise configuration files, a tiny mistake—like omitting a "/"—can cause request failures and complaints.
Location Matching Details
Each location block in Nginx represents a directory match. When a request arrives, Nginx parses the URL from top to bottom, matches the first suitable location, and applies the directives inside its braces.
For example, with the request http://www.wandouduoduo.com/wddd/index.html, the following configuration is used:
location /wddd/ {
proxy_connect_timeout 18000; # half an hour
proxy_send_timeout 18000;
proxy_read_timeout 18000;
proxy_pass http://127.0.0.1:8080;
}If the location path ends with a slash, it matches the exact directory; without the slash, it performs a prefix (fuzzy) match. Thus location /wandou matches /wandoudouduo and any path starting with /wandou, while location /wandou/ matches only the exact /wandou/ directory.
proxy_pass Variations
The presence of a trailing slash in the proxy_pass URL also changes the final upstream request:
With trailing slash :
location /wddd/ {
proxy_pass http://127.0.0.1:8080/;
}Result: http://127.0.0.1:8080/index.html Without trailing slash :
location /wddd/ {
proxy_pass http://127.0.0.1:8080;
}Result: http://127.0.0.1:8080/wddd/index.html Adding a directory with trailing slash :
location /wddd/ {
proxy_pass http://127.0.0.1:8080/sun/;
}Result: http://127.0.0.1:8080/sun/index.html Adding a directory without trailing slash :
location /wddd/ {
proxy_pass http://127.0.0.1:8080/sun;
}Result: http://127.0.0.1:8080/sunindex.html A comprehensive test configuration is provided to experiment with various combinations of location and proxy_pass directives, illustrating how slashes affect URL rewriting.
Conclusion
Appending "/" to a location path forces an exact directory match, while omitting it enables prefix matching. In contrast, the proxy_pass directive concatenates the upstream URL regardless of a trailing slash, which can lead to unexpected routing if not carefully configured.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
