X-Forwarded-For


The X-Forwarded-For HTTP header field is a common method for identifying the originating IP address of a client connecting to a web server through an HTTP proxy or load balancer.
The X-Forwarded-For HTTP request header was introduced by the Squid caching proxy server's developers.
X-Forwarded-For is also an email-header indicating that an email-message was forwarded from one or more other accounts.
In this context, the caching servers are most often those of large ISPs who either encourage or force their users to use proxy servers for access to the World Wide Web, something which is often done to reduce external bandwidth through caching. In some cases, these proxy servers are transparent proxies, and the user may be unaware that they are using them.
Without the use of XFF or another similar technique, any connection through the proxy would reveal only the originating IP address of the proxy server, effectively turning the proxy server into an anonymizing service, thus making the detection and prevention of abusive accesses significantly harder than if the originating IP address were available. The usefulness of XFF depends on the proxy server truthfully reporting the original host's IP address; for this reason, effective use of XFF requires knowledge of which proxies are trustworthy, for instance by looking them up in a whitelist of servers whose maintainers can be trusted.

Format

The general format of the field is:
where the value is a comma+space separated list of IP addresses, the left-most being the original client, and each successive proxy that passed the request adding the IP address where it received the request from. In this example, the request passed through proxy1, proxy2, and then proxy3. proxy3 appears as remote address of the request.
Since it is easy to forge an X-Forwarded-For field the given information should be used with care. The right-most IP address is always the IP address that connects to the last proxy, which means it is the most reliable source of information. X-Forwarded-For data can be used in a forward or reverse proxy scenario.
Just logging the X-Forwarded-For field is not always enough as the last proxy IP address in a chain is not contained within the X-Forwarded-For field, it is in the actual IP header. A web server should log BOTH the request's source IP address and the X-Forwarded-For field information for completeness.

Proxy servers and caching engines

The X-Forwarded-For field is supported by most proxy servers, including A10 Networks, aiScaler,
Squid,
Apache mod_proxy,
Pound,
HAProxy,
Varnish,
IronPort Web Security Appliance,
AVANU WebMux,
Array Networks,
Radware's AppDirector, Alteon ADC, ADC-VX, and ADC-VA,
F5 Big-IP,
Blue Coat ProxySG,
Cisco Cache Engine,
McAfee Web Gateway,
Phion Airlock,
Finjan's Vital Security,
NetApp NetCache,
jetNEXUS,
Crescendo Networks' Maestro,
Web Adjuster,
Websense Web Security Gateway,
Microsoft Forefront Threat Management Gateway 2010
and
NGINX.
X-Forwarded-For logging is supported by many web servers including Apache. IIS can also use a HTTP Module for this filtering.
Zscaler will mask an X-Forwarded-For header with Z-Forwarded-For, before adding its own X-Forwarded-For header identifying the originating customer IP address. This prevents internal IP addresses leaking out of Zscaler Enforcement Nodes, and provides third party content providers with the true IP address of the customer. This results in a non-RFC compliant HTTP request.

Load balancers

AVANU WebMux Network Traffic Manager, an application delivery network load balancing solution inserts the X-Forwarded-For header by default in One-Armed Single Network Mode and is available as a farm option in Two-Armed NAT, Two-Armed Transparent, and One-Armed Direct Server Return Modes.
Barracuda Load Balancer from Barracuda Networks supports user-defined headers such as X-Forwarded-For to
insert the client IP address into a client request.
Citrix Systems' NetScaler supports user-defined fields such as X-Forwarded-For to insert the client IP address into a client request.
Cisco ACE Load Balancing Modules can also insert this field, usually implemented when the load balancer is configured to perform source NAT, to allow the load balancer to exist in a one-armed configuration, while providing a mechanism that the real servers can use to account for client source IP address. The reference mentions x-forward, however X-Forwarded-For can be substituted.
F5 Networks load balancers support one-armed and multi-armed configurations. Big-IP may also be configured to delegate trust to proxies more than one hop away, and accept custom X-Forwarded-For headers from other sources.
LineRate virtual load balancers support X-Forwarded-For via command line drive configurations, or via node.js scripts.
KEMP Technologies LoadMaster supports X-Forwarded-For for non-transparent load balancing in both one-armed configuration and multi-armed configurations.
Coyote Point Systems Equalizer supports X-Forwarded-For fields for load balancing in both one-armed configuration and multi-armed configurations.
OpenBSD relays can insert and/or alter this field.
Amazon's Elastic Load Balancing service supports this field.
LBL LoadBalancer supports one-armed and multi-armed configurations.
Radware AppDirector ADC, Alteon ADC, ADC-VX, and ADC-VA support inserting an header for traffic that is Source NAT towards servers, as well, as being capable of providing persistency of traffic based on the X-Forwarded-For header for distributing traffic from a proxied connection to multiple servers while preserving persistency to servers.
Loadbalancer.org Enterprise load balancers support X-Forwarded-For load balancing by default

Alternatives and variations

RFC 7239 standardized a Forwarded HTTP header with similar purpose but more features compared to the X-Forwarded-For HTTP header. An example of a Forwarded header's syntax:
Forwarded: for=192.0.2.60;proto=http;by=203.0.113.43
HAProxy defines the PROXY protocol which can communicate the originating client's IP address without using the X-Forwarded-For or Forwarded header. This protocol can be used on multiple transport protocols and does not require inspecting the inner protocol, so it is not limited to HTTP.