Nginx Proxy Headers, Redirects and Cookie Rewriting
Two messages, two directions
The incoming request and the upstream response are different messages. Request controls change what the application receives; response controls change what the client receives. A backend can need an internal virtual host even when the public site uses a different name. The examples below use HTTP/1.1 and a fixed upstream authority.
location /headers/ { proxy_pass http://127.0.0.1:18161/; proxy_http_version 1.1; proxy_set_header Host catalog.test; proxy_set_header Connection close; proxy_set_header X-Lab-Private "";}The backend receives Host catalog.test, independent of the public host. An empty value suppresses X-Lab-Private in the upstream request. Other original request headers are normally forwarded subject to proxy behavior. The fixed host is suitable only when the application is configured for it. $proxy_host identifies the configured upstream host and port; $host reflects request/server name selection, and $http_host reflects the received Host header. They are not interchangeable. Host choice does not set HTTPS upstream certificate verification; that is a separate policy.
Restrict request metadata for a narrow endpoint
location = /status-only { proxy_pass http://127.0.0.1:18161/status; proxy_pass_request_headers off; proxy_pass_request_body off; proxy_set_header Host catalog.test; proxy_set_header Content-Length ""; proxy_set_header Connection close;}This special-purpose endpoint omits original headers and body while Nginx still constructs its own upstream request. The method is not changed by these directives. Clearing Content-Length avoids advertising a body that will not be sent. Do not apply this policy to a general API: it can remove authentication or content negotiation data. The fixture sends a harmless POST to demonstrate that its body is omitted; the toy status handler accepts both methods without side effects.
Control downstream headers
location /response/ { proxy_pass http://127.0.0.1:18161/; proxy_hide_header X-Lab-Internal; add_header X-Lab-Edge chapter15 always;}The application’s X-Lab-Internal field is absent from the client response, and the edge marker is added even on error responses. proxy_pass_header can permit a normally suppressed upstream response field, for example Date if an explicit requirement exists; generally retain defaults. add_header is not a general replacement operation: an upstream field of the same name can coexist with the new one. To make a single authoritative custom field, hide the upstream version before adding the edge version. Header filtering is not a substitute for authorization.
Translate backend redirects and cookies
location /shop/ { proxy_pass http://127.0.0.1:18161/; proxy_redirect http://catalog.test/ http://edge.test:18160/shop/; proxy_cookie_domain catalog.test edge.test; proxy_cookie_path / /shop/; proxy_cookie_flags demo httponly samesite=lax;}A backend redirect to http://catalog.test/receipt is translated to http://edge.test:18160/shop/receipt. A synthetic demo cookie with Domain=catalog.test and Path=/ becomes scoped to edge.test and /shop/, and receives HttpOnly and SameSite=Lax. The fixture inspects header text, not browser cookie acceptance. proxy_cookie_flags requires 1.19.3 or later. This is a loopback HTTP demonstration, not a session security design; production cookies should use a reviewed HTTPS policy and appropriate Secure settings. Narrow paths and domains intentionally; cookie Path is not an authorization boundary. proxy_redirect modifies redirect-related fields, not links embedded in HTML or JSON. Its default mapping is derived from the location and proxy_pass; a variable-based proxy_pass cannot use the default parameter.
Inheritance is per directive family
server { listen 127.0.0.1:18160; proxy_set_header Host catalog.test; proxy_set_header X-Lab-Private ""; location /review/ { proxy_pass http://127.0.0.1:18161/; proxy_set_header Host catalog.test; proxy_set_header X-Lab-Private ""; proxy_set_header X-Lab-Route review; }}A location that defines any proxy_set_header stops inheriting that directive list from its parent; repeat the required list or share a reviewed include. The classic add_header rule is also all-or-nothing for the list, but it affects responses independently. Nginx 1.29.3 introduced add_header_inherit to change that response inheritance policy; it is not supported by the observed local 1.26.3 binary and does not change proxy_set_header inheritance. Test success and error responses separately.
Practice
For a new upstream endpoint, list each header that is retained, replaced, suppressed or added, and whether it is part of the request or response. Then compare a successful reply with an application error. Next, define who may supply forwarding metadata.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nginx Proxy Headers, Redirects and Cookie Rewriting
Apply the chapter concepts to original examples.
Which directive removes a custom field from an upstream request?
Which directive suppresses a backend response field?
What does proxy_redirect modify?
What happens to parent proxy_set_header entries when a location defines one?
Which feature needs Nginx 1.29.3 or newer?