X-Forwarded-For explained: how proxies pass client IPs
When a reverse proxy sits in front of an application, the application sees the proxy's address instead of the visitor's. The X-Forwarded-For header is how that information is passed along, and it is easy to get wrong.
The basic header
A proxy appends the address it received the connection from to the X-Forwarded-For (XFF) header. With one proxy in front of an app, a request from a client at 203.0.113.9 looks like this to the app:
X-Forwarded-For: 203.0.113.9
With a CDN and then a reverse proxy, each hop appends, so the header becomes a comma-separated list:
X-Forwarded-For: 203.0.113.9, 198.51.100.7
The leftmost value is the original client as reported by the first proxy. The rightmost value was added by the proxy closest to your application. The middle entries are the proxies in between.
Why order matters, and why the rightmost hop is usually the safe one
Clients can send their own X-Forwarded-For header. A proxy normally appends to whatever it receives rather than replacing it, so an attacker can send a forged left-hand value and have it preserved. If your application trusts the leftmost entry, it can be told the client is any address the attacker likes.
The value added by the last proxy you control is the one that proxy actually observed, so it cannot be forged by the client. That is why a common rule is: trust only your own proxies, and take the rightmost hop that was added by the last trusted proxy. If the chain is entirely behind a single proxy you control, that is simply the rightmost entry.
This site follows that rule by default: in proxy mode it takes the rightmost X-Forwarded-For entry. Behind Cloudflare it uses CF-Connecting-IP instead, because Cloudflare guarantees that header and appends its own edge to XFF.
Spoofing in practice
An attacker sending X-Forwarded-For: 127.0.0.1 does not become localhost. Your proxy will append the real address, and your application must ignore the forged left-hand side. Problems appear when code takes the first entry, trusts any proxy, or accepts X-Forwarded-For directly from the internet without a proxy in front.
- Never read XFF on a server that is directly reachable.
- Configure your proxy to overwrite or append correctly, and restrict it to trusted upstream ranges.
- Parse from the right, not the left, and validate that each value is an IP address.
Related headers
X-Real-IP: a single address set by some proxies. Convenient, but only trustworthy when the proxy sets it.CF-Connecting-IP: Cloudflare's header for the visitor address. Ignore XFF when this is present and you are behind Cloudflare.Forwarded: the standardised header (RFC 7239) withfor=parameters. Less widely used than XFF, and more awkward to parse because of quoting and IPv6 brackets.True-Client-IP: used by some CDNs and proxies. Same trust caveat.
Configuration examples
nginx as the only proxy:
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
Traefik sets X-Forwarded-For automatically. If another proxy is in front, list its ranges under forwardedHeaders.trustedIPs so Traefik trusts and appends correctly rather than treating the upstream as the client.
Application pseudocode for a single trusted proxy:
chain = parse_ips(header("X-Forwarded-For"))
client = chain[-1] if chain else socket_address
Parsing pitfalls
- IPv6 with ports: values may appear as
[2001:db8::1]:443. Strip brackets and ports before validating. - Empty entries:
a,,bis common after a misconfigured hop; drop empties rather than failing. - Length limits: cap the number of hops you parse to avoid pathological headers.
- Private addresses: internal hops can legitimately appear; they are not client addresses.
Logging and privacy
An IP address is personal data under UK and EU GDPR. If you log X-Forwarded-For, log the minimum you need, restrict access, set a retention period and say so in your privacy notice. Many teams keep a truncated address (for example, the first three octets of IPv4) for abuse analysis and discard the rest.
You can see exactly which forwarding headers your browser and proxies sent with the header inspector.
Check your own connection
See your public IP address, inspect the headers your browser sends, or work out a network range with the subnet calculator. More background in the guides index.