Advanced request phases
Follow a request through Nginx
An HTTP request is not processed by reading nginx.conf from top to bottom. Nginx selects the virtual server and runs handlers through ordered processing phases. Rewrite handlers may change the URI, access handlers may reject a request, a content handler generates or obtains a response, and output filters transform headers or body. Some requests finish early. Internal redirects can restart parts of processing. In release 1.26.3, ngx_http_core_run_phases dispatches registered phase checkers; this is an implementation detail, not a public module ABI. The timeline is simplified, not a guarantee every phase executes.
Read request -> select server -> rewrite/find location-> preaccess/access -> content -> response filters -> logInternal redirect: location selection can run againCompare original and normalized URI
The original request target includes its query string in $request_uri; $uri is the current normalized path and can change on an internal redirect. For location matching, Nginx normalizes the path. Do not equate normalization with application authorization. Agree on path semantics at every proxy and application boundary, and keep authorization attached to the resource being served. This disposable location shows an ordinary percent-encoded space; production logs should avoid raw sensitive query strings.
location /inspect/ { default_type text/plain; return 200 "$request_uri|$uri";}Understand prefix replacement
In a simple prefix location, a proxy_pass URI replaces the matched part of the normalized path. Without a URI, forwarding the original target is typical for an unchanged request. Internal rewrites, regex locations and variable-based proxy_pass have additional rules. For this prefix fragment, /api/items?sort=name becomes /v1/items?sort=name at the backend. Align routing and authorization policies before deployment; test normal requests against both layers rather than injecting ambiguous or malformed traffic.
location /api/ { proxy_pass http://127.0.0.1:19081/v1/; proxy_http_version 1.1;}Observe the response and control plane separately
Header filters work on responses rather than selecting the upstream. add_header with always can add a diagnostic marker even to error responses; review inherited headers in nested contexts. Logging occurs after processing and can describe the final URI and response. Never treat a diagnostic header as an authorization control. Configuration validation and reload are control-plane actions; handling a request is a data-plane action. A syntax check cannot prove the selected path or final headers.
location /diagnostic/ { add_header X-Lab-Stage edge always; return 404 "not found";}Apply what you learned
Practice: Trace GET /api/items?sort=name through the prefix location and upstream /v1/. Explain which path the backend receives, where access checks belong, and how to observe the final response. The manual exercise and quiz are staged separately. Related references and the next lesson are listed in the chapter authored map.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Advanced request phases
Assess HTTP processing, framing and retry reasoning.
Why can location selection run more than once?
A request for /docs/ is internally redirected by the index handler to /docs/index.html. Which path can $uri expose after that redirect?
What target does the illustrated prefix replacement produce?
What does add_header always change?
What is a data-plane action?