Read the header from the trusted side
When a server sits behind a CDN or load balancer, the TCP connection comes from the proxy. The visitor’s address arrives in X-Forwarded-For, which is a list rather than a single address. Each proxy appends the address it received the connection from. The leftmost entry is the original client only if every client and proxy behaves well, and nothing stops a visitor from sending the header themselves.
X-Forwarded-For: 198.51.100.99, 203.0.113.50, 192.0.2.10In this chain the visitor at 203.0.113.50 sent 198.51.100.99 as a forged value. The CDN appended the visitor’s real address, and the load balancer next to the app appended the CDN’s address, 192.0.2.10. The load balancer is the TCP peer, so it never appears in the list. Reading the leftmost entry returns the forged value, and reading the rightmost returns the CDN.
The reliable method is to start at the right, skip every address that belongs to a proxy you operate or trust, and take the first address that is not one of them. Use that address for rate limits, allowlists and abuse checks. Treat anything further left as claimed by someone else. That address can still belong to an untrusted proxy on the visitor’s side rather than to the visitor’s device, so it is the best address available for security decisions, not proof of who sent the request.
Test which entry your app trusts
Do not rely on the setting’s name. Send a forged chain and see which address the application records as the client. This Express server returns what it resolved, with the number of proxies set through an environment variable.
import express from "express";
const app = express();
app.set("trust proxy", Number(process.env.HOPS ?? 2)); // number of proxies in front of the app
app.get("/whoami", (req, res) => res.json({ ip: req.ip, ips: req.ips }));
app.listen(3000, "127.0.0.1");curl -sS http://127.0.0.1:3000/whoami \
-H 'X-Forwarded-For: 198.51.100.99, 203.0.113.50, 192.0.2.10'
# {"ip":"203.0.113.50","ips":["203.0.113.50","192.0.2.10"]}
curl -sS http://127.0.0.1:3000/whoami
# {"ip":"127.0.0.1","ips":[]}The same forged chain was run against Express with different trust proxy values. The local connection stood in for the load balancer, so a correct setting must trust two proxies: the load balancer and the CDN.
| trust proxy value | req.ip returned | What happened |
|---|---|---|
| false (the default) | 127.0.0.1 | The header was ignored and the connection’s peer was logged, which is the proxy. |
| 1 | 192.0.2.10 | Stopped one hop early and logged the CDN. |
| 2 | 203.0.113.50 | Correct for this chain: both proxies were skipped. |
| 3 | 198.51.100.99 | Counted one proxy too many and returned the forged entry. |
| true | 198.51.100.99 | Returned the leftmost entry, which the visitor controls. |
| loopback | 192.0.2.10 | Trusted only the local proxy, so the CDN was treated as the client. |
| loopback, 192.0.2.0/24 | 203.0.113.50 | Trusted both proxies by address and took the first address that was not one of them. |
These results come from one machine on 1 December 2026: Express 5.2.1, proxy-addr 2.0.8 and Node 23.7.0, with the header values written by hand. No real CDN or load balancer was in the path, so the table shows how Express reads this header, not how a particular CDN builds it. Express documents the same behaviour: true reads the left-most entry, a number counts hops from the connection, and a list skips trusted addresses from the right.
A lone forged 198.51.100.99 was also sent straight to the server, as a visitor who skips your proxies would send it. With 1, 2, 3 or true, req.ip was the forged value, because a number or true trusts the connection whoever it comes from. With the single list 192.0.2.0/24, which did not include the test connection’s address, Express ignored the header and returned 127.0.0.1. Express warns that a client reaching the app in fewer hops than the configured number can spoof values.
base=$(curl -sS https://ipcheckhq.com/ip)
forged=$(curl -sS -H 'X-Forwarded-For: 198.51.100.7' https://ipcheckhq.com/ip)
[ "$base" = "$forged" ] && echo "header ignored" || echo "header honored: $forged"On 1 December 2026, from one network, the IPCheckHQ /ip endpoint was requested with 198.51.100.7 supplied as X-Forwarded-For (with one entry and with two), X-Real-IP, CF-Connecting-IP, Forwarded and True-Client-IP. Every response matched the request without the header, and the script above printed header ignored. That is a single observation about one service. Run it against your own endpoint to learn what yours does.
Set the trust boundary at each layer
Each layer handles a client-supplied header differently, so the count or list you configure must follow the documented behaviour of the layer in front of you. The table summarizes vendor documentation; these products were not run for this article.
| Layer | Documented behaviour | What to configure |
|---|---|---|
| AWS Application Load Balancer | Appends the connecting client’s address to an existing header by default. Preserve and remove modes exist. | Keep append unless you need another mode, and count the load balancer as a trusted hop. |
| Cloudflare | Appends the connecting proxy’s address to an existing header and sends the visitor address in CF-Connecting-IP. | Log CF-Connecting-IP, and make sure only Cloudflare can reach the origin. |
| nginx realip module | Replaces the client address only for connections from trusted addresses. With real_ip_recursive off it takes the last entry; with it on, the last non-trusted entry. | List your proxies with set_real_ip_from and turn recursion on when more than one proxy is trusted. |
| Express | Takes a boolean, a hop count, a list of addresses or a function. | Prefer a list of proxy addresses. Use a count only when every path has the same length. |
set_real_ip_from 192.0.2.0/24;
real_ip_header X-Forwarded-For;
real_ip_recursive on;Cloudflare sends the visitor’s address to your origin in CF-Connecting-IP, on traffic from its edge. Its documentation points to Authenticated Origin Pulls and Cloudflare Tunnel for confirming that traffic comes from your own zone, so a request that reaches the origin another way carries no such guarantee.
The standardized replacement is the Forwarded header from RFC 7239. Its for parameter quotes IPv6 addresses and may carry the word unknown or an obfuscated identifier instead of an address. The RFC also states that the header cannot be fully trusted, since any node on the path may change it. Many stacks still emit X-Forwarded-For, so check which one your proxy sends.
Normalize and check the address you extracted
Resolving the right entry is not enough. An Application Load Balancer with client port preservation enabled writes entries such as an address followed by a colon and port, and wraps IPv6 addresses in square brackets. In the Express runs above, req.ip returned 203.0.113.50:8080 and [2001:db8::1]:8080 unchanged for those header values, so a string comparison against an allowlist would never match. A non-address entry such as unknown also came back as the client when the count reached past the real visitor.
import { isIP } from "node:net";
function clientAddress(entry) {
let value = entry.trim();
const bracketed = value.match(/^\[([^\]]+)\](?::\d+)?$/);
if (bracketed) value = bracketed[1];
else if (/^[^:]+:\d+$/.test(value)) value = value.slice(0, value.lastIndexOf(":"));
return isIP(value) ? value : null;
}| Entry | Result |
|---|---|
| 203.0.113.50:8080 | 203.0.113.50 |
| [2001:db8::1]:8080 | 2001:db8::1 |
| 2001:db8::1 | 2001:db8::1 |
| unknown | null |
| _hidden | null |
Look up the address you extracted before you trust it. The IPCheckHQ lookup returns the network that owns an address, and the ASN and network type show whether your logs recorded a visitor or a proxy. Cloudflare’s 104.16.0.1 is a public address used here as an infrastructure example, not a visitor.
curl -sS "https://ipcheckhq.com/api/v1/ip/104.16.0.1?format=agent"
# {"ip":"104.16.0.1","geo":"CA-Ontario-Toronto","asn":"AS13335","isp":"Cloudflare, Inc.","network_type":"hosting","proxy":null,"vpn":null,"tor":false}A response that names a hosting network or a CDN as the client suggests the setting stopped one hop early. Read the autonomous system number (ASN) as a signal: it describes who operates the address range, not who sent the request, and some real visitors also connect from hosting or business networks. The full response labels this result as likely, not known.
A crawler user agent such as Googlebot or Bingbot is a claim that is separate from the address. Once you have the client IP, you can confirm the claim with reverse DNS and the crawler operator’s published IP ranges.
The full response for an address can add a special_range field. On 1 December 2026 the API returned documentation for 203.0.113.50 and private for 10.0.0.5, with no network data for either. A private address recorded as the client means your count stopped inside your own network. For a proxy address, the geo field describes the proxy and not your visitor.
Put the forged-header test in your deploy checks, and repeat it whenever you change the CDN, add a load balancer or move the origin. Write the proxy count or address list next to the configuration so the next change updates both. The raw header contains visitor addresses, so keep it in restricted logs next to the address you chose.
