Nginx proxy_pass: Preserve or Replace the Request Path
The small slash that changes the path
In an ordinary prefix location, a proxy_pass target can include just a server address or also a path. Compare these partial location blocks in a server. They are alternatives for the same route, not two blocks to paste together.
A request for /shop/books?page=2 reaches the app as /shop/books?page=2 in this simple unchanged-request case.
location /shop/ { proxy_pass http://127.0.0.1:18122/;}The same request becomes /books?page=2. The slash after the address is a URI component, not decoration. The query stays attached in these examples.
location /shop/ { proxy_pass http://127.0.0.1:18122/v1/;}Now the app receives /v1/books?page=2. Choose the form matching the app’s actual routes. The replacement uses the matched portion of the normalized path; a no-URI target can preserve the original request form when it has not been rewritten.
Special locations and explicit variables
A regex match or a named location does not supply an ordinary replaceable prefix. Use a target without a URI there. The named route is reached through internal routing, not by visiting its name in a browser.
location ~ ^/reports/[0-9]+$ { proxy_pass http://127.0.0.1:18123;}location @fallback { proxy_pass http://127.0.0.1:18123;}For /reports/42 the reports fixture receives /reports/42. The chapter lab also routes a synthetic GET /named to a named location using an internal error handler. When a rewrite with break changes the path inside a proxied location, the changed URI is sent; do not use the ordinary prefix shortcut to predict it.
location /raw/ { proxy_pass http://127.0.0.1:18122$request_uri; proxy_redirect off;}Here the variable supplies the full original request URI; /raw/item?x=1 stays /raw/item?x=1. A variable URI is not an ordinary prefix replacement. The destination is fixed and local; do not construct the backend address from user input. Dynamic host resolution is a later topic.
Verify what the application sees
/shop/books?page=2 + no URI → /shop/books?page=2/shop/books?page=2 + / → /books?page=2/shop/books?page=2 + /v1/ → /v1/books?page=2All results are for straightforward paths without further rewrites. A successful response may still come from the wrong route. Inspect the echoed path, backend marker and query in the lab. Keep static files and API routes distinct so an app error is not hidden by a page fallback.
Practice: select a proxy_pass target for an app that expects /v1/books when a visitor requests /shop/books. Explain why adding or deleting the URI component changes behavior. Next: choose the request headers and timeouts.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Predict upstream paths
Choose the best answer based on the lesson and local fixture.
With location /shop/ and target http://127.0.0.1:18122, what reaches the app for /shop/books?
What changes if the target ends in /v1/?
What happens to ?page=2 in the simple prefix replacement example?
Which target form is suitable for the shown named location?
What does the fixed target ending in $request_uri use?