Nginx proxy_pass: URI Replacement and Trailing Slashes
Follow a request through a prefix location
An upstream is the server to which Nginx forwards a request. The public route and upstream route need not be identical. First decide what path the application expects, then configure the mapping. These server-context fragments assume a local backend at 127.0.0.1:18161; the chapter lab provides that backend. They are static examples, not browser-executable code.
location /keep/ { proxy_pass http://127.0.0.1:18161;}location /catalog/ { proxy_pass http://127.0.0.1:18161/items/;}With an unchanged original request, /keep/book?lang=en arrives as /keep/book?lang=en. The second route converts /catalog/book?lang=en into /items/book?lang=en. The path /items/ in the upstream URL tells Nginx which text to use in place of the matched location prefix. The query stays in these examples. Without a URI after the host and port, the original request URI is forwarded when it has not been changed internally.
A slash is part of the upstream path
location /strip/ { proxy_pass http://127.0.0.1:18161/;}# Intentionally misleading mapping for comparison only:location /wrong/ { proxy_pass http://127.0.0.1:18161/items;}/strip/book becomes /book. /wrong/book would become /itemsbook, not /items/book: the remaining text is book, and the replacement lacks a trailing slash. Do not deploy the intentionally misleading route. A useful review table lists the public path, replacement prefix, expected upstream path and expected status; include the bare directory and the directory with a slash as distinct cases. A slash-ended proxy location can generate an automatic redirect for a request without the slash; make that policy explicit when needed.
Original versus normalized URI
Nginx selects locations using its normalized path. $uri is the current normalized URI without the query; $request_uri records the original URI including arguments. Internal redirects and rewrites can change $uri. For a normal prefix replacement, matching uses the normalized URI. Do not assume that preserving the original request target and replacing the normalized prefix have identical results for every encoding or internal redirect. Test ordinary percent-encoded data with a known application contract, and avoid using inconsistent path representations for access decisions.
location /legacy/ { rewrite ^/legacy/(.*)$ /items/$1 break; proxy_pass http://127.0.0.1:18161;}For /legacy/book?lang=en, expect /items/book?lang=en. break keeps this location and the changed URI is forwarded. In this pattern keep proxy_pass free of a replacement URI. In a regex or named location, a fixed prefix to replace is not generally available; use the no-URI form.
location ~ ^/reports/[0-9]+$ { proxy_pass http://127.0.0.1:18161;}For /reports/7, the upstream sees /reports/7 when no rewrite changes it. Regex matching is not a request-target transformation by itself.
Variables change the mapping rules
location /variable/ { proxy_pass http://127.0.0.1:18161$request_uri;}This sends /variable/book?lang=en as the explicitly assembled URI. When the directive uses variables and supplies a URI, do not apply the normal prefix-replacement rule. A variable-based domain name may require Nginx’s configured DNS resolver unless it identifies a declared upstream group. Keep destinations under administrator control, never derive the upstream authority from arbitrary client input. The fixture uses an IP literal and does not depend on DNS.
Practice
Predict the upstream target before making a change. Then use the chapter lab to compare your prediction against the backend response. The lab tests HTTP/1.1 upstream explicitly; its live checks are not pre-executed. Next, decide which headers and body should cross the proxy boundary.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nginx proxy_pass: URI Replacement and Trailing Slashes
Apply the chapter concepts to original examples.
What reaches the backend for /catalog/book?lang=en in the first example?
Which form keeps an unchanged original request target?
What does $uri represent?
What happens to /wrong/book in the misleading fragment?
Which proxy_pass form fits a named location?