X-Forwarded-For
Preserving the original client IP address through proxies and load balancers is the function of the X-Forwarded-For unofficial request header, which carries a comma-separated chain of addresses representing each hop in the forwarding path.
Usage
When a client connects directly to an origin server, the server reads the client IP from the TCP connection. Once proxies or load balancers sit between the client and the server, the server only sees the IP address of the last intermediary. The X-Forwarded-For header preserves the original client address and the chain of proxy addresses.
Each intermediary appends the IP address of the node from which the request was received. The result is a comma-separated list where the leftmost address belongs to the original client and each subsequent address represents a proxy in the forwarding chain.
X-Forwarded-For: <client>, <proxy1>, <proxy2>
Because any hop along the path is free to add, modify, or forge entries, the list is only trustworthy from the rightmost entry operated by a trusted proxy inward. A common security practice is to configure the application to skip a known number of trusted proxy entries from the right side of the list and treat the next address as the true client IP.
The standardized replacement for this header is the
Forwarded header, which uses structured
parameters (for, by, host, proto) instead of a
flat IP list.
Values
The X-Forwarded-For value is a comma-separated list of IP addresses. Each address is either an IPv4 dotted-decimal or an IPv6 address.
Single client
A request arriving at the first proxy from a client at
203.0.113.50 produces a single entry.
X-Forwarded-For: 203.0.113.50
Multiple hops
After passing through two proxies, the list contains the client followed by the first proxy address.
X-Forwarded-For: 203.0.113.50, 198.51.100.178
IPv6
IPv6 addresses appear without brackets in the comma-separated list.
X-Forwarded-For: 2001:db8::cafe, 198.51.100.178
Address with port
Some load balancers append the client port to each
address. Azure Application Gateway sends a
comma-separated list of IP:port pairs, and an AWS
Application Load Balancer does the same when its
client port preservation attribute is enabled.
X-Forwarded-For: 203.0.113.50:51234
Example
A web application behind a load balancer and a CDN edge node
receives the following header. The first address
203.0.113.50 is the original client. The second address
198.51.100.178 is the CDN edge node. The load balancer
added the CDN edge address before forwarding to the origin.
X-Forwarded-For: 203.0.113.50, 198.51.100.178
Rate-limiting middleware reading this header counts from the
right. Knowing one trusted hop, the CDN edge, appears in
the list, the middleware skips that entry from the right
and treats 203.0.113.50 as the client IP.
An Authentication gateway logging access events extracts the client IP from the X-Forwarded-For header alongside the User-Agent for audit trails.
X-Forwarded-For: 192.0.2.12
Troubleshooting
Wrong IP extracted from the chain. Applications that count from the left extract a client-controlled value. Count trusted proxy hops from the right instead. In nginx,
set_real_ip_fromdefines trusted proxy addresses andreal_ip_header X-Forwarded-Forextracts the client IP. The directivereal_ip_recursivedefaults tooff, which makes nginx take the last address in the header. The default is correct only with a single trusted hop. Behind a CDN plus a load balancer, setreal_ip_recursive onso nginx walks the chain from the right and picks the last address outside the trusted set. The realip module is an optional build component, so a missingset_real_ip_fromdirective at startup means the binary lacks it.IPv6 parsing failures and port suffixes. IPv6 addresses in X-Forwarded-For are not quoted or bracketed. Parsers expecting only IPv4 break on
::1or2001:db8::1. Azure Application Gateway and an AWS Application Load Balancer with client port preservation append:portto each address, which breaks parsers expecting bare IPs. Use a parser that handles both address families and strips a trailing port.Different header at each vendor. AWS ALB sends X-Forwarded-For, Cloudflare pairs CF-Connecting-IP with X-Forwarded-For, and some proxies use proprietary names like X-Real-IP exclusively. Check proxy documentation for the correct header. An AWS Application Load Balancer also exposes the attribute
routing.http.xff_header_processing.modewith the valuesappend(the default),preserveandremove.preservepasses the incoming header through untouched, andremovestrips it so the target sees only what the load balancer adds. An nginx proxy adds nothing to the chain unlessproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_foris configured.Multiple X-Forwarded-For headers. Some proxies add a new header instead of appending to the existing one. The HTTP specification allows multiple headers with the same name. Concatenate all values in order before parsing.
X-Real-IP vs X-Forwarded-For confusion. X-Real-IP carries a single IP, typically set by nginx. X-Forwarded-For carries the full proxy chain. For applications behind a single proxy, either works. For multi-proxy environments, X-Forwarded-For preserves the full path.
Security
Clients control the leftmost value, so the header
alone never supports a security decision such as rate
limiting or access control. Only the rightmost entries
added by known, trusted proxies are reliable. Configure
the outermost reverse proxy to overwrite the header
rather than append to it when the upstream source is
untrusted, which on an AWS Application Load Balancer is
the remove processing mode. The
Forwarded header is the standardized
replacement but follows the same trust model.