How to Get the Real Client IP behind a Proxy

When a web server sits behind a reverse proxy or CDN, the connection it receives usually comes from the last proxy. The server records that proxy as its connection peer and sees its source IP address. A proxy can pass an earlier client address in X-Forwarded-For or another supported request header. Before using that address for rate limits or access rules, the server must verify which proxy sent the request and which proxies supplied the accepted header values.
Even a correctly selected address does not identify a particular person or device. Several devices may share one public address, and a VPN or another proxy may hide the address used on the original network. Preserve the connection peer separately so an investigation can distinguish what the server directly observed from what it derived from forwarding metadata.
Map the Trusted Proxy Hops
Draw the actual path through any CDN, load balancers, or reverse proxies to the application server. Record which component accepts public traffic, which adds or overwrites forwarding headers, and which network addresses each trusted proxy may use to connect to the next hop. Include IPv4 and IPv6 ranges and a process for updating those ranges when the CDN or proxy provider changes them.
Confirm whether the origin can be reached directly from the internet. If a requester can bypass the trusted proxy and send an arbitrary X-Forwarded-For header to the origin, the application must not trust that header for access control or rate limits. Restrict direct access to known proxy sources where possible and treat untrusted peers as direct clients. Check that firewall and routing rules enforce the intended path. Protect connections between trusted proxies and the application server against tampering, because a source address allowlist alone does not protect header contents in transit.
Do not assume every proxy appends headers the same way. One may overwrite the incoming value, another may append to it, and a gateway may use Forwarded or a header specific to the proxy service. Verify the chosen behavior in the current product documentation and in safe tests. If the application mistakes a client-supplied value for one added by a trusted proxy, an attacker may control the recorded client address.
Configure Trusted Sources Explicitly
Configure the server or framework to accept forwarded address information only when the immediate connection peer is in an allowlist of trusted proxy addresses or ranges. In NGINX, the real IP module provides set_real_ip_from for trusted senders and real_ip_header for the selected header. With real_ip_recursive on;, NGINX checks a trusted sender's header from right to left and uses the first address that is not a trusted proxy. With recursion off, NGINX instead uses the last header address when the peer is trusted. For X-Forwarded-For, configure real_ip_header X-Forwarded-For; and ensure the trusted addresses match the actual route.
A blanket trust rule for every source makes spoofing easy. A client can send a forged leftmost address in X-Forwarded-For when the edge does not remove or control incoming values. Even if an edge appends a real address, blindly taking the first list entry can select the attacker's claim. The selection must stop before it reaches values supplied by an untrusted sender.
Overwriting untrusted incoming headers at the public entry point can simplify parsing later in the chain. Record whether the proxies actually do this, and still verify the connection peer before accepting their headers. If a proxy starts using an address outside the allowlist, the application may report that proxy address instead of the client; an overly broad allowlist can allow untrusted senders to supply the client address.
Select the Address from the Full Chain
Several X-Forwarded-For header fields must be treated as one ordered list. After verifying that the connection peer is trusted, read the list from right to left and skip addresses belonging to trusted proxies. The first remaining address is the client address for this trust configuration; it may belong to another proxy rather than the original device.
Use the server or framework's supported parser for the selected header format, including its rules for IPv6 and permitted whitespace. If the part of the chain needed for selection cannot be parsed, do not skip the invalid value and select an earlier client-supplied value instead. Reject the request or record that the client address is unavailable, according to the application's policy, while retaining the connection peer for diagnosis.
For example, suppose the application receives a request from trusted proxy 10.0.0.8, which is behind trusted edge proxy 192.0.2.10. The following X-Forwarded-For value illustrates the selection; the addresses are for documentation, not a real request:
X-Forwarded-For: 203.0.113.77, 198.51.100.24, 192.0.2.10
In this example, 198.51.100.24 connected to the edge, while 203.0.113.77 was an earlier value supplied by that client. Skip the trusted 192.0.2.10 entry and select 198.51.100.24. Continuing left would accept the forged claim.
Do not select an address just because it is public or because geolocation data labels it as a visitor location. Private addresses can legitimately occur in trusted internal hops, and public addresses can be forged in untrusted header positions. Trust comes from the observed peer and configured proxy chain, not from the apparent geography of a string.
Log the Peer and Derived Address Separately
Record the connection peer observed by the server and the selected derived client address in separate fields. In NGINX, $realip_remote_addr retains the original address, while $remote_addr reflects the replacement after real IP processing. Include a request identifier and the proxy route where useful, but avoid logging more personal data than the operational purpose requires. Limit retention and access for raw forwarding headers.
Separate fields prevent a misleading audit trail. If the derived address changes after a proxy configuration update, the peer field can show which component sent the request. If an untrusted requester supplied a forged header, logs should still reveal the direct peer and that the forwarded field was ignored. Do not silently overwrite the only record of the transport connection.
Use one configured framework or proxy mechanism to select the address, and pass that result to the application components that need it. Separate parsers in logging, rate limiting, and access control can produce inconsistent decisions. Keep authorization based on accounts or tokens where appropriate; IP-based rules are coarse and can affect shared networks.
Test Forged and Direct Requests
In an authorized test environment, send a normal request through the expected proxy path and verify the selected address and preserved peer. Then send a request with a forged leftmost X-Forwarded-For value. The fabricated value must not become the client address used by rate limits or access rules. Test multiple forwarded fields, IPv6, malformed elements, and a change in the number of trusted hops.
Also attempt a direct-origin request if the architecture permits a safe test. The origin should reject it at the network layer or treat its socket peer as the only trusted address source. Confirm that rate limits and access rules cannot be bypassed by adding a fabricated header. Use documentation-only addresses in recorded examples and anonymize real logs. Repeat the checks after changing the proxy route or its trust settings.







