Authentication subrequests
Ask a trusted service before proxying
The optional ngx_http_auth_request_module (available since 1.5.4) delegates an access decision to a subrequest. Verify the build using nginx -V and the --with-http_auth_request_module flag. The authorization endpoint is not a public route. It returns 2xx to allow, 401 for a missing or unacceptable credential, or 403 for denied permission. Other statuses are unexpected errors, normally yielding 500 at the gateway. A 401 can relay the WWW-Authenticate challenge. A successful authorization subrequest is not the final API response.
location = /api/report { auth_request /_check; auth_request_set $verified_user $upstream_http_x_verified_user; proxy_set_header X-Verified-User $verified_user; proxy_set_header Authorization ""; proxy_pass http://127.0.0.1:19041;}The auth service supplies X-Verified-User only for a valid identity; auth_request_set reads that result after the subrequest. The gateway replaces any client value rather than passing it through. In this example the API does not need the original credential, so Authorization is removed. The backend must be isolated from direct untrusted access, and reject missing or unacceptable identities.
location = /_check { internal; proxy_pass http://127.0.0.1:19041/check; proxy_pass_request_headers off; proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_set_header Authorization $http_authorization; proxy_set_header X-Original-URI $request_uri; proxy_connect_timeout 1s; proxy_read_timeout 1s;}Only selected request metadata is sent to the auth service; the original body is unnecessary for this decision. Clearing Content-Length avoids claiming a body that was not forwarded. An internal location rejects an ordinary external request with 404. The fixed upstream prevents caller-selected authorization destinations. The read timeout measures gaps between reads, not a total deadline. Do not retry indefinitely or permit traffic when the authorization system is unavailable. Use an application-level deadline and monitor failures separately.
Predict the outcome
No token -> auth 401 -> client 401Reader label -> auth 403 -> client 403Editor label -> auth 204 -> API decidesFault label -> auth 503 -> client 500Slow auth result -> subrequest timeout -> client 500These are expected outcomes for the supplied lab, without custom error handling. The editor label is only a fixture string; it is not a signed token. The API can still reject the allowed caller if the requested object is outside their permissions. Avoid auth caching unless the design proves that identity, method, resource, revocation and expiry are properly represented.
Practice
Use the decision table to predict the result of missing credentials, insufficient role, valid role and a timeout. Then explain why returning 200 directly from the protected location is not a good way to test auth_request: a rewrite-phase return can finish the request before access handlers run.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Authentication subrequests
Configure internal authentication subrequests and predict successful, denied and failed authorization outcomes.
What does an auth subrequest status 204 mean?
What does a subrequest 403 mean?
What should happen if the auth service unexpectedly returns 503?
Why clear Content-Length when skipping the auth request body?
Where should X-Verified-User come from?