When Nginx sits behind a load balancer, CDN, or ingress proxy, the TCP peer address is usually the address of that intermediary rather than the browser. Operators often configure Nginx to read X-Forwarded-For and then use the resulting address for access logs, rate limits, or abuse investigations. The configuration becomes dangerous when Nginx trusts a header from any peer: a direct client can supply a fake address and make logs and downstream controls tell the wrong story.
The secure design starts with a network trust boundary. Define exactly which proxy addresses may set the client address, ensure untrusted clients cannot reach the origin directly, and understand how each proxy appends or replaces forwarding headers. Nginx's real-IP module can then select a client address from headers sent by those trusted peers. It cannot establish that an arbitrary header is truthful by itself.
Draw the actual request path: client, edge service, load balancer, Nginx, then application. For each hop, record whether it terminates TLS, which forwarding header it writes, whether it preserves a client-supplied value, and the source address range that reaches the next hop. Confirm those facts with the platform's current documentation and packet or access-log evidence. Do not copy an example CIDR from a different provider or environment.
Restrict network access to the origin so only the edge and required operational sources can connect to the web listener. If an attacker can connect directly to Nginx, the attacker may bypass edge controls even if the proxy headers are configured correctly for normal traffic. If the provider's addresses change, maintain the trusted list from its authoritative published ranges and alert when the origin receives requests from unexpected peers.
The Nginx ngx_http_realip_module provides set_real_ip_from, real_ip_header, and real_ip_recursive. The module must be available in the deployed build. set_real_ip_from declares peers whose replacement address information Nginx may trust. Keep these entries limited to the actual proxy network or addresses. Never use set_real_ip_from 0.0.0.0/0; or ::/0 in production: those values make every source trusted.
An illustrative configuration for an internal load balancer using the forwarding chain is:
# Replace these example ranges with the exact, maintained proxy CIDRs.
set_real_ip_from 10.48.0.0/16;
set_real_ip_from 2001:db8:48::/48;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
The documentation explains that recursive mode searches the supplied list from the trusted side and selects the last non-trusted address. That result is meaningful only if each trusted proxy constructs the chain safely and clients cannot inject values that the trusted proxy preserves as authoritative. If the upstream proxy provides a single replacement header with a documented contract, configure that header and test its behavior instead of assuming a multi-hop chain.
Nginx's proxy module also has separate forwarding behavior. A reverse proxy commonly sets Host and X-Real-IP and uses $proxy_add_x_forwarded_for to append the current remote address. That variable can include a client-supplied chain. Decide which hop is trusted and normalize or replace inbound values at the first trusted edge. Do not let application code independently parse a different header using a different rule.
In a test environment, send one request through the real proxy and one directly from an untrusted test host if direct access is possible. Include a deliberately forged X-Forwarded-For value in the direct request. The request should either be blocked at the network boundary or the forged value should not become the trusted client address. Confirm the original peer remains available for diagnostics with $realip_remote_addr where supported.
Temporarily log both the interpreted client address and the real peer address during a controlled validation window. Keep the log format free of cookies, authorization values, and request bodies. Compare the values with edge logs and the application request identifier. Once confidence is established, retain the minimum fields needed for incident response and document their meaning so analysts do not confuse proxy IPs with end users.
When rate limiting is enforced in the application, pass the normalized client address through a trusted server-side mechanism. Do not let an incoming public header directly choose a rate-limit key. For authenticated users, identity or tenant limits are usually more stable than IP alone; IP-based controls can remain a secondary abuse signal. Record whether a request was blocked by the edge, Nginx, or the application so operators can diagnose differences.
Test configuration syntax with nginx -t before reload, then use a staged rollout and monitor 4xx/5xx rates. An overly broad trust range can re-enable spoofing; an overly narrow list can cause all traffic to appear to come from the edge. Recursive parsing with an incorrectly constructed header chain can select an attacker-controlled entry. A stale provider CIDR list can unexpectedly deny users or reduce observability.
Avoid using Referer as an identity signal. It may be absent, shortened by browser policy, or altered by intermediaries. Forwarded IP values are also personal data in many contexts; apply retention and access controls to logs according to the site's privacy policy and applicable law. An IP address is an imperfect indicator of a person because NAT, mobile networks, and VPNs can group many users or change addresses.
Perform spoofing checks only against your own staging origin or an authorized environment. Do not scan or probe another site's proxy configuration. Replace the sample CIDRs with ranges verified for the actual deployment; the ranges above are documentation examples, not a production allowlist. Last updated: 2026-11-09. Change log: Initial publication; configuration guidance checked against Nginx module documentation.