Nginx Nested Locations and Internal Redirects
Nested locations are a tree
A nested location sits inside another location. Its full prefix is still written from the URI root, not relative to the parent. Nginx may descend into the best matching prefix and evaluate regex rules at that level before returning to an outer level. Do not apply a single flat priority table to a tree. The following example illustrates conventional Linux matching; the nested search behavior was reviewed against upstream 1.26.3 source and is staged for runtime verification.
location /library/ { location ^~ /library/print/ { return 200 "print\n"; } location ~ [.]csv$ { return 200 "library csv\n"; } return 200 "library\n";}location ~ [.]csv$ { return 200 "outer csv\n"; }location / { return 200 "general\n"; }For /library/data.csv, the regex inside /library/ wins and the marker is library csv. For /library/print/page, the nested prefix yields print. A request for /library/print/data.csv skips the regex at the inner level due to its ^~ modifier, but the enclosing /library/ prefix lacks that modifier, allowing the outer regex to win: outer csv. This is why a deeply nested ^~ is not a global override. Prefer flat, nonoverlapping rules where possible; include each boundary in regression tests. Exact and named locations cannot contain nested locations, and named locations cannot themselves be nested.
A request can change its current URI
location = /old-guide { rewrite ^ /new-guide last;}location = /new-guide { return 200 "current=$uri original=$request_uri\n";}rewrite from the HTTP rewrite module matches a path and substitutes a new one. The last flag requests location selection again. The original request /old-guide?edition=2 reaches /new-guide, and the client receives status 200 without making a second request. $uri now contains /new-guide, whereas $request_uri still contains /old-guide?edition=2. The old arguments are retained in this example. These variables serve different purposes; avoid reflecting or logging raw request targets containing sensitive query data in real services.
current=/new-guide original=/old-guide?edition=2Internal routing versus client redirect
location = /old-guide { return 302 /new-guide; }An HTTP 302 tells the client to make a new request to the Location destination if it chooses to follow it. The earlier internal rewrite instead continues on the server. rewrite ... break stops the current rewrite sequence without asking to search a new location in the way last does. Avoid cycles: repeatedly changing a URI back to a previously selected location can end with status 500 after Nginx reaches its internal limit. Do not introduce a loop merely to test on a live server.
Named locations are internal destinations
location = /missing-note { error_page 404 = @note; return 404;}location @note { return 200 "note fallback\n"; }The error_page rule here transfers a 404 to @note and adopts the fallback response status (200). The named block is not selected by a normal client path such as /@note. In a real site, keep genuine missing-resource status codes unless the fallback is intentionally successful. More file-based fallbacks and error-page details follow in the next chapter. Compare all predicted outcomes in the private Chapter 8 fixture.
Practice: trace both paths
Write the initial path, chosen location, any internal transition, final handler, response status and body marker. Use the same approach when debugging a route that unexpectedly reaches a different handler. Verify configuration syntax and then HTTP behavior in an approved lab; a syntax check alone cannot prove routing.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nested routing and URI changes
Predict the result and explain why the selected route wins.
What is the expected marker for /library/data.csv in the nested example?
What can happen for /library/print/data.csv in the shown nested example?
After the internal rewrite of /old-guide?edition=2, what is $uri?
Which action asks the client to make another request?
How is @note selected in the shown example?