Real IP and trusted proxies
Identify who can assert an address
A proxy opens its own connection to Nginx. The socket peer is therefore usually the proxy, not the browser. The optional HTTP Real IP module can substitute an address derived from trusted metadata. Merely receiving X-Forwarded-For does not make its contents trustworthy. Trust only verified proxy endpoints under your administrative control, and ensure the edge overwrites untrusted metadata or establishes an authoritative chain.
set_real_ip_from 192.0.2.10;real_ip_header X-Forwarded-For;real_ip_recursive on;Walk a chain from the receiving side
Suppose the socket peer is an internal proxy at 192.0.2.11, with the edge at 192.0.2.10. Both are explicitly trusted. With recursion off, Nginx uses the last supplied address when the peer is trusted. With recursion on, it works backward through the trusted suffix and selects the nearest address outside that trusted set, assuming a valid list containing such an address. This is not necessarily the ultimate human user; an untrusted intermediary stops the trust walk. The leftmost value is not automatically authoritative.
Peer: 192.0.2.11Trusted: 192.0.2.10, 192.0.2.11X-Forwarded-For: 198.51.100.7, 192.0.2.10Recursive off: 192.0.2.10Recursive on: 198.51.100.7Separate replacement from authorization
The set_real_ip_from list identifies which peers may supply replacement data; it does not itself permit or block HTTP requests. After address processing, access rules and address-based rate keys use the effective address. In the example below only a fictional support subnet is admitted, regardless of which trusted proxy delivers it. Restrict direct access to the origin at the network layer as well. If the origin is reachable through an unintended path, the address policy and application controls must still remain correct.
set_real_ip_from 192.0.2.10;set_real_ip_from 192.0.2.11;real_ip_header X-Forwarded-For;real_ip_recursive on;location /support/ { allow 198.51.100.0/24; deny all; root /srv/demo;}Audit the effective address and peer
The effective address can be read from $remote_addr; $realip_remote_addr retains the pre-replacement peer. The latter appeared in 1.9.7, while $realip_remote_port appeared in 1.11.0. The optional port carried with a valid address may also be used from 1.11.0. The default header is X-Real-IP, not X-Forwarded-For, so choose the header explicitly. Confirm build support with nginx -V. Avoid logging full header chains or query strings when the two addresses and status meet the audit purpose; even addresses may be personal data and require controlled retention.
log_format ip_audit 'effective=$remote_addr peer=$realip_remote_addr status=$status';access_log logs/ip-audit.log ip_audit;Practice and next steps
Practice: trace a request from an untrusted direct peer and one from a trusted edge. Explain why only the second can supply a replacement address. Never trust every source to make logging look correct.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Real IP and trusted proxies
Distinguish the socket peer from the reconstructed address, trace trusted forwarding chains and preserve useful audit evidence.
What does set_real_ip_from establish?
An untrusted direct peer sends a forwarding header. What address should Real IP retain?
A trusted peer supplies client, trusted-edge in a valid header. With recursion on, which address is selected?
Which variable retains the original peer after replacement?
What does an accepted replacement address prove about user identity?