Nginx Forwarded Headers and Trusted Proxy Boundaries
Start with a topology, not a header
A forwarding header is a claim carried inside an HTTP request. It is not proof of the sender’s identity. At a direct public edge, Nginx sees a transport peer; in a multi-proxy system that peer may be a load balancer instead of the original client. Draw the chain and identify which component is authorized to supply each value. The lab is a single loopback HTTP edge with a local backend; it does not simulate the trust configuration of an actual CDN.
location /edge/ { proxy_pass http://127.0.0.1:18161/; proxy_http_version 1.1; proxy_set_header Host catalog.test; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Host edge.test:18160; proxy_set_header Forwarded "";}For a client connecting locally to this HTTP edge, the controlled values are 127.0.0.1, http and the configured public host. The fixture rejects unknown public hosts at a separate default server. Suppress other forwarding aliases that the backend understands or configure it to ignore them; the example covers only its declared contract. This address is useful diagnostic metadata, not a user account identity. The backend must be reachable only through approved paths and must trust only the configured edge.
Appending is not the same as verification
$proxy_add_x_forwarded_for produces the incoming X-Forwarded-For value with the current $remote_addr appended, or just that address if the incoming field is absent. This can preserve chain history between controlled proxies, but it does not certify earlier entries. In the direct-edge fixture we overwrite instead. Do not use the first address of an arbitrary list as an authentication or access-control decision. In a deployment where Real IP already changed $remote_addr, appending it can duplicate an address; choose a documented chain convention and record the transport peer separately.
When an approved load balancer supplies client metadata
# http or server context; documentation addresses onlyset_real_ip_from 192.0.2.10/32;real_ip_header X-Forwarded-For;real_ip_recursive on;This requires the HTTP Real IP module, which is an optional build feature. Substitute the exact approved load balancer addresses in a real reviewed deployment; never trust every source. With recursion enabled and a trusted immediate peer, Nginx examines the supplied chain from the trusted side and selects the last address outside the trusted set. Without recursion it selects the final supplied address when the peer is trusted. If the immediate peer is not trusted, the configured replacement does not apply. This is an address interpretation rule, not a validation of application identity. Enforce network reachability and ensure the load balancer sanitizes incoming claims.
# http context; requires the Real IP modulelog_format hop_review 'peer=$realip_remote_addr client=$remote_addr status=$status';$realip_remote_addr retains the address before replacement. The diagnostic log separates it from the effective address. IP addresses can be personal data: keep access and retention limited. Do not log full credentials, cookies or arbitrary forwarding chains. The chapter fixture disables access logging and returns only selected benign metadata; its toy response must not be deployed as a public header-debug service.
Scheme and host are independent of address handling
$scheme describes the connection as seen by this Nginx instance, not necessarily the browser-facing scheme when an earlier proxy terminates TLS. In a multi-hop design, configure which upstream is allowed to supply the public scheme and host, validate the values against the deployment contract, and reject unexpected hostnames. The Real IP module changes client address/port, not all forwarding headers. Secure cookies, redirects and absolute links should use a verified public-origin contract. Network isolation and TLS on each required hop are distinct from header rewriting.
server { listen 127.0.0.1:18160 default_server; server_name _; return 404;}The default listener rejects names not matched by the separate edge.test virtual server in the fixture. The underscore is just an unused server name, not special catch-all syntax. Combine this with upstream routing and application-level authorization. Next chapter: upstream groups and load balancing.
Practice
Draw a client, edge proxy, load balancer and application. For each hop label the actual peer, trusted source of metadata, and who overwrites or appends headers. Explain how you will verify the policy using synthetic local inputs.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nginx Forwarded Headers and Trusted Proxy Boundaries
Apply the chapter concepts to original examples.
What does a forwarding header prove on its own?
What is the direct-edge policy in the lab?
What does real_ip_recursive on select for a trusted peer?
What is $realip_remote_addr used for?
Does $scheme always represent the browser-facing protocol?