Keywords: Nginx | proxy_pass | 404 error
Abstract: This paper delves into the semantic differences of slashes in Nginx proxy configuration, particularly in the proxy_pass directive. Through analysis of a typical 404 error case, it explains why location /api versus location /api/ and proxy_pass http://backend versus proxy_pass http://backend/ lead to different request forwarding behaviors. Combining code examples and HTTP request tests, the article clarifies path matching and URI transmission mechanisms, offering best practices to help developers avoid common proxy configuration errors.
Introduction
In Nginx reverse proxy configuration, the proxy_pass directive is a core component for request forwarding, but its behavioral nuances often cause confusion due to slash handling, leading to common issues like 404 errors. This paper systematically analyzes the impact of configuration differences on proxy behavior based on a real-world case.
Problem Description and Case Analysis
A user configured Nginx to proxy requests from the /api path to a backend service, but accessing it returned a 404 error, while direct access to the backend port succeeded. The original configuration is as follows:
upstream backend{
server localhost:8080;
}
server {
location /api {
proxy_pass http://backend;
}
location / {
root /html/dir;
}
}Testing with curl, accessing localhost/api/authentication/check/user/email returned 404, whereas localhost:8080/authentication/check/user/email returned 200 OK. This indicates incorrect proxy path mapping.
Core Knowledge: Semantic Differences of Slashes
According to the best answer, the root cause lies in the use of slashes in the location and proxy_pass directives. The configuration was modified to:
location /api/ {
proxy_pass http://backend/;
}This change resolved the 404 error. Key points include:
location /apivs.location /api/: The former matches URIs starting with/api, while the latter matches those starting with/api/, affecting path truncation behavior.proxy_pass http://backendvs.proxy_pass http://backend/: The trailing slash in the latter instructs Nginx to remove the matched portion from thelocationduring forwarding.
For example, accessing http://abc.xyz/api/endpoint:
- If configured as
location /api/ { proxy_pass http://backend; }, the request is forwarded tohttp://backend/api/endpoint. - If configured as
location /api/ { proxy_pass http://backend/; }, the request is forwarded tohttp://backend/endpoint, removing the/api/prefix.
In the original case, location /api lacked a trailing slash, leading to imprecise matching; proxy_pass http://backend lacked a trailing slash, causing the /api part to be retained in the backend request, which the backend service might not handle, resulting in a 404.
Solution and Best Practices
To ensure correct proxy path mapping, it is recommended to:
- Use trailing slashes in
location(e.g.,/api/) to explicitly match directory-style paths. - Use trailing slashes in
proxy_pass(e.g.,http://backend/) to remove the matched prefix, unless retention is needed. - Test configurations with tools like
curlto verify request forwarding paths.
An example of the corrected configuration is:
location /api/ {
proxy_pass http://backend/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}This ensures /api/authentication/check/user/email is forwarded as http://backend/authentication/check/user/email, aligning with the backend service's expected path.
Conclusion
The semantic differences of slashes in Nginx proxy_pass configuration are a common cause of 404 errors. By understanding path matching and URI transmission mechanisms, developers can avoid such issues, enhancing the accuracy and reliability of proxy setups. The case analysis in this paper provides concrete guidance for related practices.