Sub topic 4
How `proxy_pass` Rewrites the Request URI
This chapter assumes you know basic Nginx configuration, HTTP requests and responses, status codes, and TCP ports. You should also have a backend service listening on a local port, such as 127.0.0.1:3000.
The two forms of `proxy_pass`
A reverse proxy receives a client request and sends another request to a backend. The proxy_pass directive determines the backend address and the URI that Nginx sends.
The important distinction is whether proxy_pass includes a URI path.
| Configuration | Client request | Backend request |
|---|---|---|
| `proxy_pass http://127.0.0.1:3000;` | `/app/users` | `/app/users` |
| `proxy_pass http://127.0.0.1:3000/;` | `/app/users` | `/users` |
When the directive has no URI, Nginx passes the complete normalized request URI. When it has a URI, Nginx replaces the part that matched the location with the URI from proxy_pass.
Replacing a location prefix
This configuration publishes the backend under /app/, but removes that prefix before forwarding the request:
server {
listen 80;
server_name example.com;
location /app/ {
proxy_pass http://127.0.0.1:3000/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}A request for /app/users?id=42 is sent to the backend as /users?id=42. The query string, which begins with ?, is preserved.
The trailing slash is meaningful. In this example, the location ends with / and the replacement URI also ends with /. This makes the prefix replacement easy to predict.
Keeping the original URI
Use a proxy_pass without a URI when the backend expects the public path:
server {
listen 80;
server_name example.com;
location /app/ {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}Here, /app/users remains /app/users upstream. This is useful when the application itself routes requests under /app/, or when Nginx should only forward traffic without changing its path.
Common matching mistakes
Nginx first selects a server block and then chooses the best matching location. The URI replacement uses the selected location, not whichever path seems visually closest in the configuration.
A missing or extra slash can create an unexpected path. Test these combinations with a backend that logs the received request URI:
location /app/withproxy_pass http://backend/removes/app/.location /app/withproxy_pass http://backendkeeps/app/.location /appcan also match paths such as/application, so uselocation /app/when you need a directory-like boundary.
A URI in proxy_pass is restricted in some location forms. Nginx does not allow it in a regular expression location, named location, or certain if and limit_except blocks. In those cases, omit the URI or redesign the location structure.
Safe testing
After editing a configuration, validate it before reloading:
sudo nginx -t && sudo systemctl reload nginxThen request both a path and a query string:
curl -i 'http://example.com/app/users?id=42'Check the backend's access log or request output to confirm whether it received /users?id=42 or /app/users?id=42.
Exercise
With location /api/ and proxy_pass http://127.0.0.1:8080/;, what URI does the backend receive for /api/orders?page=2?
Show answer
The backend receives /orders?page=2. Nginx replaces the matched /api/ prefix with /.