Subrequests and internal redirects
Three different kinds of request flow
A browser redirect tells the client to make a new request. An internal redirect changes routing within the current request; Nginx can choose another location without a round trip to the browser. A subrequest is a separate internal unit of work associated with its parent, used for a decision or a fragment. A subrequest may invoke an upstream service, but does not inherently create a network connection. The following trace is conceptual; the actual handlers determine the order and whether processing stops.
client → /report → access check → /_decision (subrequest) ↓ allow static report → clientLet the access phase authorize a static file
In the lab the protected file exists in a dedicated web root. The access module consults the internal endpoint. The fixture accepts one invented label, denies unknown values and simulates a service failure. In real systems a trusted verifier authenticates an identity and checks permission; a client-chosen label is not authentication. Build support for auth_request is required. A 2xx subresponse allows access, 401 and 403 deny access, and another status results in an error. A 401 can relay the challenge header. The static content handler runs only after access succeeds. A return in the protected location is a rewrite-phase action and can end processing before access: do not use it as the protected content handler. This snippet is inside server; the decision location uses fixed return codes.
location = /report.txt { auth_request /_decision; root html;}location = /_decision { internal; if ($http_x_lab_role = reader) { return 204; } if ($http_x_lab_role = unavailable) { return 503; } return 403;}Send only the required metadata to an authorization service
If the decision comes from an upstream service, avoid sending a request body when it is not needed. Clear Content-Length when suppressing the body; select which headers can cross the boundary. The following partial location sends an invented lab label, not real credentials, to a fixed local decision service. An actual service is not included in this snippet. The parent request body is a separate concern. auth_request_set can capture decision-response metadata for later trusted use, but it does not verify that metadata by itself.
location = /_remote_decision { internal; proxy_pass http://127.0.0.1:19481/check; proxy_pass_request_body off; proxy_pass_request_headers off; proxy_set_header Content-Length ""; proxy_set_header X-Lab-Role $http_x_lab_role;}Internal redirects and background copies
A missing resource can be routed to an internal error document while preserving the original 404 status. The URI form of error_page changes methods other than GET and HEAD to GET. Do not treat it as a general replay mechanism. A mirror is different: its response is not used as the main response. The lab mirror below has no backend, discards the synthetic request body for mirroring purposes and handles only a fixed toy route. No actual user traffic is copied. If mirroring to a backend, explicitly remove unnecessary headers and bodies and consider method, side effects, rate and privacy.
location = /missing.txt { error_page 404 /_missing; return 404; }location = /_missing { internal; return 404 "Not here\n"; }location = /sample.txt { mirror /_sample_copy; mirror_request_body off; root html; }location = /_sample_copy { internal; return 204; }Practice and next steps
Practice: predict the outcome of an allowed, denied and failed decision. Explain why a direct request to /_decision is rejected and why a static handler is used. Next: nginx-header-and-body-filters.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Subrequests and internal redirects
Distinguish subrequest and route reselection
Which operation asks the browser to send another request?
What happens if the authorization subrequest returns 403?
Why avoid return 200 in the protected location?
What is the expected result of direct access to an internal decision location?
What is done with a mirror subresponse?