Network & delivery
8 min readUpdatedX-Forwarded-For: how to find the real client IP behind a proxy
Quick answer
X-Forwarded-For is an HTTP request header in which each proxy appends the address of the client that connected to it, giving a comma-separated list with the original client on the left. Any client can send the header itself, so the left-most entry proves nothing. The safe rule is to read the list from the right and take the first address that does not belong to a proxy you control.
01What is the X-Forwarded-For header?
X-Forwarded-For (XFF) is a request header that carries the IP address of the client a proxy received the request from. A reverse proxy, load balancer or CDN opens its own connection to the server behind it, so that server only sees the proxy's address; the header passes the visitor's address along. MDN calls it a de facto standard, and RFC 7239 later defined Forwarded as its standardised replacement.
Each proxy appends rather than replaces. nginx's $proxy_add_x_forwarded_for is the incoming header plus the connecting address after a comma, and Cloudflare and AWS Application Load Balancers append to an existing header in the same way by default, so the list grows by one entry per hop, from left to right.
# visitor 198.51.100.7 -> CDN edge 192.0.2.10 -> load balancer 10.0.0.5 -> app
CDN to load balancer: X-Forwarded-For: 198.51.100.7
load balancer to app: X-Forwarded-For: 198.51.100.7, 192.0.2.10
app socket peer: 10.0.0.5
# the same visitor, sending a forged header of its own
load balancer to app: X-Forwarded-For: 203.0.113.66, 198.51.100.7, 192.0.2.1002Why can't you trust the first IP in X-Forwarded-For?
Because the client wrote it. A proxy appends the address it saw, but keeps whatever the client sent in front of it. A visitor who sends X-Forwarded-For: 203.0.113.66 arrives at the application with that address in the left-most position, and code that takes the first entry believes it.
The test below shows it: an nginx proxy with the usual proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for line passed the forged value through and appended the real address on the right.
MDN lists the consequences as rate-limiter avoidance, access-control bypass and memory exhaustion: a new forged value per request escapes a per-IP rate limit, an allow-listed address opens a protected area, and junk keys fill a limiter's table. If the origin can also be reached directly, no entry can be trusted at all, so the origin should accept connections only from the proxy, as explained in how to hide your origin IP.
# the visitor sends no X-Forwarded-For
xff="127.0.0.9" x-real-ip=127.0.0.9
# the visitor sends X-Forwarded-For: 203.0.113.66
xff="203.0.113.66, 127.0.0.9" x-real-ip=127.0.0.903Which IP in X-Forwarded-For is the real client?
Read from the right. The right-most entry was added by the proxy closest to the application; every entry to its left is only as trustworthy as the proxy that relayed it. The client is the first address, counting from the right, that does not belong to a proxy you trust.
Configure trust as a list of proxy addresses rather than a count of hops. Express's documentation warns that a hop count can be fooled when the application is reachable by paths of different lengths, and a test confirmed it: with trust set to one hop, a client connecting straight to the application port chose its own address.
- Start with the socket address. If it is not one of your proxies, it is the client, and the header is ignored.
- Otherwise walk the header from right to left, skipping every address on your trusted list.
- The first address that is not trusted is the client. Everything to its left is unverified and belongs in logs at most.
- If an entry is not a valid address, stop. When the right-most entry was invalid, nginx 1.22.1 kept the proxy's own address rather than guess.
- Treat repeated X-Forwarded-For lines as one list, in order, as RFC 9110 section 5.3 allows; code that reads only the first line looks at the wrong end.
| Entry, right to left | Added by | Trusted? | Result |
|---|---|---|---|
| 192.0.2.10 | Load balancer, recording the CDN edge | Yes, CDN range | Skip |
| 198.51.100.7 | CDN edge, recording the visitor | No | This is the client |
| 203.0.113.66 | The visitor itself | Never reached | Ignore |
04X-Real-IP, CF-Connecting-IP, Forwarded and other client IP headers
Many providers also send a single-value header. It is easier to parse, but the same rule applies: a header is only as good as the proxy that sets it, and only if that proxy overwrites any value the client sent.
The PROXY protocol works a layer lower. A TCP load balancer sends a short preamble, such as PROXY TCP4 198.51.100.7 203.0.113.10 51234 443, before the first byte of the connection, so it also works for TLS passed through unopened. Its specification says receivers must rely on configuration and never try to detect the preamble, and the port should be reachable only by the load balancer.
| Header | Set by | Format | Trust it when |
|---|---|---|---|
| X-Forwarded-For | Most proxies, CDNs and load balancers; AWS Application Load Balancers append by default | Comma-separated list, client on the left | Only the right-hand entries your own proxies added |
| Forwarded (RFC 7239) | Fewer proxies; standardised in June 2014, and MDN notes it is much less frequently used | for=, by=, proto= and host= parameters; IPv6 in quoted brackets | Same rules; RFC 7239 section 8.1 says it cannot be relied upon to be correct |
| X-Real-IP | A convention, usually an nginx proxy with proxy_set_header X-Real-IP $remote_addr | One address | Your own proxy always sets it, replacing any client value |
| CF-Connecting-IP | Cloudflare, from its edge to the origin | One address | The connection comes from a Cloudflare range |
| True-Client-IP | Cloudflare Enterprise; Akamai when Send True Client IP Header is enabled | One address | On Akamai, Allow Clients To Set True Client IP Header stays off |
| Fastly-Client-IP | Fastly, added when the request has none | One address | Your edge configuration overwrites it, because Fastly keeps a value the client sent |
| CloudFront-Viewer-Address | Amazon CloudFront, when added in an origin request policy | Address and source port | The connection comes from CloudFront |
| PROXY protocol v1 or v2 | TCP load balancers and proxies | A preamble before the first byte of the connection | The listener expects it and only trusted peers can reach the port |
X-Forwarded-For: 192.0.2.43, 2001:db8:cafe::17
Forwarded: for=192.0.2.43, for="[2001:db8:cafe::17]"05How to get the real client IP in nginx, Apache, PHP and Node.js
Fix the address once, in the first server you control behind the proxy, and let everything after it read the corrected value. The nginx real IP guide has a configuration and a log check; the table summarises the setting for each common stack.
| Stack | Setting | What it does | Trap |
|---|---|---|---|
| nginx | set_real_ip_from, real_ip_header, real_ip_recursive on | Replaces $remote_addr with the first untrusted address from the right; the peer stays in $realip_remote_addr | Afterwards $proxy_add_x_forwarded_for appends the restored client, not the hop it came through |
| Apache httpd 2.4 | mod_remoteip: RemoteIPHeader with RemoteIPTrustedProxy or RemoteIPInternalProxy; RemoteIPProxyProtocol from 2.4.31 | Processes the header right to left and stops at the first untrusted address | RemoteIPTrustedProxy will not accept a private address as the client; log %a for the client and %{c}a for the proxy (before 2.4.39, %h still logged the proxy) |
| PHP behind nginx or Apache | Nothing in PHP: REMOTE_ADDR comes from the web server, via fastcgi_param REMOTE_ADDR $remote_addr on Debian | Inherits the corrected address | $_SERVER['HTTP_X_FORWARDED_FOR'] is the raw header, client-controlled |
| Symfony and Laravel | trusted_proxies in Symfony; trustProxies(at: ...) in Laravel | The request object resolves the client from trusted proxies only | Trusting every address ('*' or REMOTE_ADDR) is safe only when the origin accepts nothing but the proxy |
| Node.js with Express | app.set('trust proxy', 'loopback') or a list of proxy addresses | req.ip becomes the first untrusted address from the right | trust proxy true returns the left-most entry |
06How to test whether your client IP can be spoofed
Send a forged header yourself and read what the origin logged, once through the proxy and once directly to the origin, using an address from a documentation range. If the origin only accepts traffic from the proxy, the direct request should not connect at all.
Frameworks fall into the trap easily. With Express's trust proxy set to true, req.ip returned the forged 203.0.113.66 for every request that carried it, through a proxy or not; with 'loopback', behind nginx on the same host, it returned the visitor's address.
| Symptom | Likely cause | Fix |
|---|---|---|
| Every visitor logs the same few addresses | The proxy is not trusted, so nothing is restored | Configure the real-IP setting with the provider's ranges |
| Everyone gets 429 Too Many Requests at once | Per-IP limits count the proxy, so all visitors share one bucket | Restore the client address before the limiter runs |
| Logs show the CDN's edge addresses | Only the last hop is skipped (nginx real_ip_recursive off) | Turn on recursive mode and trust every hop |
| Logs show 127.0.0.1 or a container bridge address | A local hop, such as a TLS terminator or Docker, is missing from the trusted list | Add that hop to the list |
| Unfamiliar addresses from unlikely places | The application reads the left-most entry | Read from the right instead |
# 1. through the proxy: the forged value may appear on the left, never as the client
curl -s -o /dev/null -w '%{http_code}\n' -H 'X-Forwarded-For: 203.0.113.66' https://example.com/
# 2. straight to the origin: should time out, or log your own address
curl -s -o /dev/null -w '%{http_code}\n' --resolve example.com:443:203.0.113.10 \
-H 'X-Forwarded-For: 203.0.113.66' https://example.com/
# 3. what did the origin record for both requests?
grep '203.0.113.66' /var/log/nginx/access.log | tail -n 207Should the first proxy overwrite or append X-Forwarded-For?
The first proxy you control decides what everything behind it believes. With nothing trusted in front of it, it can overwrite the header with the address it saw, for example with proxy_set_header X-Forwarded-For $remote_addr in nginx. Behind a CDN it has to append, and trust the CDN's ranges.
Express's documentation makes the same point for its trust proxy true setting: the last trusted proxy must remove or overwrite X-Forwarded-For, X-Forwarded-Host and X-Forwarded-Proto, or a client can supply any value. Forwarding also exposes addresses to every hop; MDN notes the header is privacy-sensitive by design, and RFC 7239 allows obfuscated identifiers and the value unknown where an address should stay hidden.
Questions
Frequently asked questions
- 01Can the X-Forwarded-For header be spoofed?
- Yes. It is an ordinary request header, so any client can send it with any value, and most proxies append to it rather than replace it. The forged part ends up on the left of the list. Only the entries added by proxies you control are reliable, which is why the client address must be read from the right, stopping at the first address that is not one of your own proxies.
- 02Should I use the first or the last IP in X-Forwarded-For?
- For anything that matters, neither blindly. Walk the list from the right, skip the addresses of your own proxies, and take the first one left over. With exactly one trusted proxy in front of the application, that is the last entry. The first entry is whatever the client claimed, so use it only where a forged value does no harm, such as rough analytics.
- 03What is the difference between X-Forwarded-For and X-Real-IP?
- X-Forwarded-For is a list that grows by one address at every proxy. X-Real-IP is a convention, most often set by an nginx proxy, that carries a single address: the one that proxy saw. Neither is defined in an RFC. X-Real-IP is simpler to read, but it is only trustworthy if your own proxy always sets it and replaces whatever the client sent.
- 04What is CF-Connecting-IP?
- CF-Connecting-IP is a header Cloudflare adds to requests it forwards to an origin server, holding the address of the client that connected to Cloudflare. It contains a single address, which makes it easier to use than X-Forwarded-For. Trust it only on connections that come from Cloudflare's published ranges, because a client that reaches the origin directly can send the same header.
- 05Is the Forwarded header better than X-Forwarded-For?
- It is better specified. RFC 7239 defines named parameters for the client, the proxy, the protocol and the host, plus a syntax for IPv6 and hidden addresses. It is no more trustworthy: the RFC itself says the header can be modified by every node on the way, including the client. Use whichever header your proxies actually send, and apply the same right-to-left rule.
- 06Why does my server log the same IP address for every visitor?
- Because it logs the address of the proxy or load balancer that connects to it. The visitor's address travels in a forwarded header, and the web server has to be told which proxies it may believe. Configure the real-IP setting for your stack with the proxy's address ranges, then check that a forged header sent directly to the origin is ignored.
Sources
Sources and verification
The configuration and commands on this page are examples, not a tested recipe. Adapt them to your system and check them before use (nginx -t for nginx).
- 01X-Forwarded-For header · MDN Web Docs
- 02RFC 7239: Forwarded HTTP Extension · IETF
- 03RFC 9110: HTTP Semantics, section 5.3 Field Order · IETF
- 04Module ngx_http_realip_module · nginx.org
- 05Module ngx_http_proxy_module ($proxy_add_x_forwarded_for) · nginx.org
- 06Apache Module mod_remoteip · Apache HTTP Server Project
- 07Express behind proxies · Express
- 08How to Configure Symfony to Work behind a Load Balancer or a Reverse Proxy · Symfony
- 09HTTP Requests: Configuring Trusted Proxies · Laravel
- 10Cloudflare HTTP headers (CF-Connecting-IP, True-Client-IP, X-Forwarded-For) · Cloudflare docs
- 11Origin Server behavior (True Client IP) · Akamai TechDocs
- 12Fastly-Client-IP · Fastly docs
- 13HTTP headers and Application Load Balancers · AWS docs
- 14Add CloudFront request headers (CloudFront-Viewer-Address) · AWS docs
- 15The PROXY protocol, versions 1 and 2 · HAProxy Technologies
- 16The perils of the "real" client IP · Adam Pritchard
Last updated