The general format of the field is:2
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 (not shown in the header). proxy3 appears as remote address of the request.
Examples:3
Because the X-Forwarded-For header is not formally standardized, some variations to the IP address format exist. For example, some implementations[which?] include the port number of clients, or enclose IPv6 addresses in square brackets even without the port number, similar to the format in the newer Forwarded header. Examples:
The X-Forwarded-For header is added or edited by HTTP proxies when forwarding a request. The server appends the address of the client to an existing X-Forwarded-For header separated by a comma, or creates a new X-Forwarded-For header with the client address as the value.
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. If the server is behind a trusted reverse proxy and only allows connections from that proxy, the header value can usually be assumed to be trustworthy.
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.
RFC 7239 standardized a Forwarded HTTP header with similar purpose but more features compared to the X-Forwarded-For HTTP header.4 An example of a Forwarded header's syntax:
HAProxy defines the PROXY protocol which can communicate the originating client's IP address without using the X-Forwarded-For or Forwarded header.5 This protocol can be used on multiple transport protocols and does not require inspecting the inner protocol, so it is not limited to HTTP.
"Overview of parsed mail headers". Archived from the original on 2014-09-20. Retrieved 2014-05-05. https://web.archive.org/web/20140920220804/http://mailheader.mattiasgeniar.be/headers.php ↩
"squid : follow_x_forwarded_for configuration directive". Squid-cache.org. Retrieved 12 November 2017. http://www.squid-cache.org/Doc/config/follow_x_forwarded_for/ ↩
"X-Forwarded-For". MDN Web Docs. Retrieved 2020-11-06. https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For ↩
Petersson, A; Nilsson, M (June 2014). Forwarded HTTP Extension. IETF. doi:10.17487/RFC7239. RFC 7239. Retrieved February 20, 2020. https://datatracker.ietf.org/doc/html/rfc7239 ↩
Willy Tarreau: The PROXY protocol. haproxy.1wt.eu. Retrieved on 2012-12-24. http://haproxy.1wt.eu/download/1.5/doc/proxy-protocol.txt ↩