← Back to blog

X-Forwarded-For Header: Format, Trust and Client IP

December 1, 2026
X-Forwarded-For Header: Format, Trust and Client IP

X-Forwarded-For (XFF) is a request header that carries the IP address of the original client through every proxy and load balancer between the browser and your server. Without it, your application sees only the address of the last proxy. With it, you can recover the client, but only if you decide which entries in the list you trust. The header is plain text that anyone can send, so reading it correctly matters more than reading it at all.

If you want to see what a request or response actually carries, our Response Headers Inspector shows the headers a URL returns, and What's My IP shows the address a server sees for you. Below: the format, a worked example, the rules for safe parsing, how it compares with X-Real-IP and Forwarded, and a short section on X-Content-Type-Options, since the two headers are often searched together.

Pingfloat
Inspect the Headers Your Server Returns
Pingfloat’s Response Headers Inspector shows the headers returned by a URL, helping you examine responses while troubleshooting proxy behavior.
Visit

TL;DR

  • X-Forwarded-For is a de-facto standard request header: a comma-separated list where the leftmost address is the claimed original client and each proxy appends the address it received the request from.
  • The server's own socket address is the last hop and is not in the list, so log both.
  • Any client can send a fake XFF value. For security uses, count only entries added by proxies you control, working from the right.
  • Use one header for one purpose. Mixing X-Forwarded-For, X-Real-IP and X-Client-IP gives indeterminate results.
  • X-Content-Type-Options: nosniff is a different header, a response header that stops browsers from guessing a file's MIME type.

Table of Contents

What the X-Forwarded-For header does

When a browser connects through a proxy, CDN or load balancer, your server opens a TCP connection with that proxy, not with the user. The source address on the connection is the proxy's. According to MDN, X-Forwarded-For is a de-facto standard header for identifying the originating IP address of a client connecting through a proxy, and the leftmost address is the originating client, assuming well-behaved clients and proxies.

"De-facto" means it was never finalized as an internet standard. It spread because every major proxy implemented it. That is why you will find small differences between products, which F5 notes in its documentation on trusted XFF: the implementation is non-standardized, so servers do not always use it consistently.

It is a request header only. You set or read it on the way to the origin, and it should never be copied into responses.

X-Forwarded-For format, with a worked example

The syntax from MDN is:

X-Forwarded-For: <client>, <proxy1>, <proxy2>, ..., <proxyN>

Entries are separated by commas, and each proxy usually adds the address of the peer it received the request from to the right-hand end. Here is the MDN example:

X-Forwarded-For: 203.0.113.195,2001:db8:85a3:8d3:1319:8a2e:370:7348,198.51.100.178

Reading it left to right:

  1. 203.0.113.195 is the originating client.
  2. 2001:db8:85a3:8d3:1319:8a2e:370:7348 is the first proxy in the path (an IPv6 address).
  3. 198.51.100.178 is the most recent proxy that appended an entry.

The proxy closest to you is not in the list at all. Microsoft's ASP.NET Core documentation says the last proxy in the chain is absent from the header, and its address is available as the transport-layer remote address. So for the example above, the TCP peer your server sees would be a fourth address, the proxy that connected to you directly.

A second example from a CDN

Fastly's documentation walks through a request from 1.1.1.1 that passes a non-Fastly proxy at 2.2.2.2, then two Fastly points of presence, and reaches the origin carrying X-Forwarded-For: 1.1.1.1, 2.2.2.2, 3.3.3.3. Notice that 4.4.4.4, the shield location, does not appear: the final connection to the origin supplies it as the socket address. The same pattern holds everywhere: the list ends one hop before the machine reading it.

Format variations you will meet

Some systems add ports or brackets. Wikipedia notes variations such as 203.0.113.195:41237, 198.51.100.100:38523, and AWS says that when client port preservation is enabled on an Application Load Balancer, the entry takes the form client-ip:client-port, with IPv6 addresses in brackets. A parser that splits on commas and assumes bare addresses will choke on these, so strip the port and brackets before comparing.

Two more details:

  • Multiple X-Forwarded-For headers can appear in one request. MDN says they must be treated as a single list, from the first IP of the first header to the last IP of the last header. Reading only one is not enough.
  • Whitespace after commas is optional. Trim every entry.

Why you cannot trust the whole list

The header is just text. A client can send X-Forwarded-For: 127.0.0.1 in its own request, and the first proxy will usually append the real address after it. The list then starts with a lie and ends with the truth.

The mod_wsgi guide gives the classic consequence: a spoofed 127.0.0.1 can make the remote address look local, which is an authentication bypass if your code trusts local traffic. Its defense is to name the proxies allowed to supply forwarded headers and to trust none by default, and it warns never to trust 0.0.0.0/0 or public address ranges.

MDN states the rule directly. For anything security-related, use only addresses added by a trusted proxy. If the server can be reached directly from the internet, no part of the list can be considered trustworthy. The header can also be wrong if any proxy in the path is malicious or misconfigured. RFC 7239, which defines the newer Forwarded header, says the same about its own value: it cannot be relied upon to be correct, since every node on the path, including the client, may alter it.

Why you cannot trust the whole list

How to get the real client IP safely

MDN describes two ways to pick the right entry. Both start from the right, because the right side is the part your own infrastructure wrote.

Option 1: trusted proxy count

If you know exactly how many proxies sit in front of your app, take the entry that many positions from the right. MDN's wording is to search from the rightmost by the count minus one, since the final proxy is your socket peer. Example: with one CDN in front of one load balancer, and the load balancer is the socket peer, the CDN's appended entry is the rightmost value, and it contains the client address the CDN saw. That entry is your client IP.

Trusted proxy count selects the client IP

This breaks when the topology changes. Add a proxy or bypass the CDN for some traffic and the count is wrong.

Option 2: trusted proxy list

Keep a list of proxy addresses or ranges you control. Walk from the right, skipping every address on the list. The first address that does not match is the client. This survives topology changes better, as long as the list is current. Use your provider's published ranges and update them.

A short procedure

  1. Take the socket peer address of the connection.
  2. If the peer is not on your trusted list, stop. Use the peer as the client and ignore XFF entirely.
  3. If it is trusted, join all X-Forwarded-For headers into one list and strip ports and brackets.
  4. Walk from the right, dropping trusted entries. The first untrusted entry is the client.
  5. If nothing is left, the client is the socket peer.
  6. Log both the derived client IP and the raw socket peer, as Wikipedia recommends, so you can reconstruct what happened.

The client-ip library documentation lists the same families of strategy and adds a practical warning: topology changes can silently invalidate a rule that used to be right, and you should test spoofing attempts with curl before shipping anything that depends on the IP. A quick test is to send your own fake header and see what your app reports:

curl -H "X-Forwarded-For: 1.2.3.4" https://your-site.example/whoami

If the app reports 1.2.3.4, your parsing trusts the client. The same documentation notes that vendor headers such as Akamai's true-client-ip and Fastly's fastly-client-ip can be spoofed by default and need extra configuration, so check how each is populated before relying on it.

Where it goes wrong in practice

Rate limits are the most common casualty. If you key a limit on the leftmost XFF entry, an attacker rotates the value on each request and never hits the limit. If you key it on the socket peer behind a CDN, every user shares one bucket and legitimate visitors are throttled together. Our notes on enterprise API rate limits cover the algorithms, and the client-IP question underneath them is the same one this article answers. For background on the attack itself, see this explainer on IP spoofing.

Cloud firewalls make the choice explicit. AWS WAF by default uses the origin IP of the request, which behind a proxy is the last proxy. You can configure it to use a forwarded header instead: for geo match, ASN match and rate-based rules it uses the first address, while IP set rules can match the first, last or any address. If the header is missing, the rule is not applied at all, which is worth remembering when you design fallbacks.

X-Forwarded-For vs X-Real-IP, Forwarded and friends

Several headers try to convey the same fact.

HeaderWhat it holdsNotes
X-Forwarded-ForList of client and proxy addressesDe-facto standard, widely supported, append-style
X-Real-IPA single client addressSet by one proxy, no chain, no formal definition
X-Client-IPA single client addressSame idea, used by some proxies
ForwardedParameters such as for=, by=, proto=Standardized in RFC 7239

The mod_wsgi documentation for trusted proxy headers lists X-Forwarded-For, X-Client-IP and X-Real-IP as the headers it can accept, recommends X-Forwarded-For as the de-facto standard, and says to select only one per purpose, because choosing several makes the outcome indeterminate. It also says the application should read the resulting remote address variable, never the original header.

The single-value headers are simpler to read, but they carry no chain, so you cannot see the path or apply a count. X-Forwarded-For gives you the chain, which is why it is the better starting point for debugging.

The standardized alternative

RFC 7239 (June 2014) defines the Forwarded header with parameter pairs, for example for=192.0.2.60;proto=http;by=203.0.113.43. IPv6 addresses are written as quoted strings. The RFC describes it as optional and says it should be off by default, and it warns that the header must never be copied into response messages, because it can reveal the whole proxy chain to the client. In practice, most deployments still send X-Forwarded-For, so your code will often need to read both.

X-Forwarded-Proto carries the original scheme, HTTP or HTTPS, and Microsoft's documentation notes it can be a list after multiple proxies. Apps that build redirect URLs need it, or they will send users back to http after the TLS-terminating proxy. If a proxy returns errors, see our guide to fixing 502 Bad Gateway, and for the difference between the addresses in these lists, public vs private IP is a useful refresher. If you need background on the address format itself, see what IPv4 is.

X-Content-Type-Options and the nosniff header

This header is searched alongside XFF because both show up in header audits, but they have nothing in common technically. X-Forwarded-For is a request header about the client's address. X-Content-Type-Options is a response header about how a browser interprets content.

Per MDN, it indicates that the MIME types announced in Content-Type must be followed and not changed, which turns off MIME sniffing. The only defined value is nosniff. With it, a request is blocked when the destination is a script and the type is not a JavaScript type, or when the destination is a stylesheet and the type is not text/css. For other responses, the browser uses the declared type without inspecting the content.

The protection matters for uploads. GitLab's documentation describes an attacker uploading a file that a browser interprets as HTML and runs as cross-site scripting, a MIME confusion attack, and recommends setting the header server-wide.

To set it:

  • Apache with mod_headers: Header always set X-Content-Type-Options nosniff, as shown in the webhint documentation, which also covers IIS.
  • nginx: add_header X-Content-Type-Options "nosniff" always;, as in the CIS NGINX benchmark item.

Before enabling it, make sure your Content-Type values are correct. The CIS item notes that older applications that depended on browsers correcting wrong types, such as JavaScript served as text/plain, will stop working, and that a correct mime.types file is a prerequisite. Fix the types, then turn it on.

To confirm it is live, load the URL in the Response Headers Inspector and look for the header on HTML, scripts and uploads alike. Our security headers checklist puts it in order alongside the others, and X-Frame-Options is the closest sibling to set at the same time. For how to read headers in general, see HTTP headers explained.

Debugging a proxy chain

When the IP your app reports looks wrong, work through it in order:

  1. Print the socket peer and the raw header values at the app. Do not guess from logs that have already been rewritten.
  2. Check whether a proxy replaces the header or appends to it. F5 notes that BIG-IP 13.1.0 and later represents the source IP in one header, while earlier versions appended a new header, which is exactly the multiple-header case above.
  3. Confirm your trusted list matches the real hops. A single internal load balancer added later is a common cause of an off-by-one error.
  4. If paths differ between request and response, our article on asymmetric routing explains how to diagnose it, and the traceroute tool shows the path your packets take.
  5. Run the curl spoof test again after every infrastructure change.

Perspective

We build network and header tools for people who spend their days asking why a request looks the way it does, and the X-Forwarded-For question comes up more than almost any other. The mistake is nearly always the same: treating a convenient string as a verified fact.

Our view is that you should be able to see the evidence yourself in seconds. Look at the headers a URL returns, check the address a server sees, trace the path, and only then write the parsing rule. A rule built on observed behavior survives a change of CDN far better than one copied from a snippet.

That is why we keep these tools free and instant, with no installation and no account for the core functions. Checking a header should take less time than arguing about it.

Andreas

Check your headers with Pingfloat

Pingfloat is a free suite of web tools for developers and IT professionals. For this topic, the Response Headers Inspector shows what your server sends, What's My IP shows the address a server sees, and Traceroute shows the network path. Core tools need no installation or account, and the Pingfloat Public API is there if you want to script checks. The Pingfloat home page lists every tool.

Pingfloat

If the proxy path is part of an uptime problem, the Website Uptime Monitor is the next stop.

FAQ

What is my X-Forwarded-For?

It is whatever the request carried to the server: the original client address first, then each proxy that handled it. You can see your own public address with a lookup tool, but the header seen by a server depends on the proxies in front of it. Print the raw header at your app to see the exact value.

Is the leftmost or rightmost address the real client?

The leftmost is the claimed client, but it is also the easiest to forge. The rightmost entry was added by the proxy closest to you. For security decisions, walk from the right and skip your own trusted proxies. The first address that is not yours is the one to use.

Should I use X-Real-IP or X-Forwarded-For?

Pick one and configure your trusted proxies to match. X-Forwarded-For gives you the full chain and is the most widely supported, while X-Real-IP holds one value. Using several at once gives indeterminate results, so choose a single header per purpose.

Does nosniff stop all content-type attacks?

It stops browsers from overriding the declared Content-Type, which closes the MIME confusion route for scripts, stylesheets and other responses. It works together with correct Content-Type values and your other security headers. Set it server-wide and verify it on every response type.

Can X-Forwarded-For contain ports or IPv6?

Yes. IPv6 addresses can appear, and some proxies add ports, with IPv6 in brackets. Normalize each entry before comparing it to a list of trusted addresses.

Sources