Skip to main content
When your service handles an incoming HTTP request, the source IP on the TCP connection is rarely the real client. Requests traverse load balancers, SSL terminators, and (often) a CDN before they reach your container. Your environment ingress populates standard headers so your code can recover the real client IP — but which header to trust depends on how your service is fronted.

Headers populated by your environment ingress

Behavior across setups

Different fronting setups leave these headers populated differently: In the first two setups, both headers resolve to the real client and either is safe to use. When a CDN like Cloudflare, Fastly, or CloudFront sits in front of your service, your environment ingress sees the CDN edge IP on the connection, so X-Real-IP reflects that edge IP, not the real client. What happens to X-Forwarded-For depends on the cloud:
  • AWS — the chain is preserved. The real client IP is the left-most entry, which the CDN populates before forwarding the request.
  • GCP — the chain is not preserved. X-Forwarded-For contains only the CDN edge IP, so neither header carries the real client.

Recommendation

If your service sits behind a CDN, read your CDN’s own header to get the real client IP — CF-Connecting-IP for Cloudflare, True-Client-IP for Akamai / Cloudflare Enterprise, or CloudFront-Viewer-Address for CloudFront. On GCP this is the only reliable option. On AWS, X-Forwarded-For[0] (the left-most entry) also works as a fallback.
A CDN header is only trustworthy if your service can’t be reached around the CDN — a client that hits your service directly can set CF-Connecting-IP or True-Client-IP to anything. Restrict the service’s allowed source IPs to your CDN’s published IP ranges with the per-service IP allowlist (AWS, GCP).
If you are not behind a CDN, X-Real-IP is the simplest and safest header to read.

Examples

Node.js (Express)

Spoofing and trust

X-Forwarded-For is a request header — any client can set it. Your environment ingress strips and rewrites it on the hop it controls, so for traffic arriving directly from the public internet to your service the header is trustworthy. When a CDN is in front, the CDN appends the original client IP to X-Forwarded-For before forwarding. On AWS the left-most entry is the real client only if every upstream hop between the client and your ingress is trusted. On GCP that chain doesn’t reach your service, so use the CDN’s own header instead. If you accept arbitrary X-Forwarded-For values from untrusted upstreams, attackers can spoof the client IP for rate-limiting, audit logs, or geo-IP checks.
Configuration of environment-specific trusted CDN CIDR ranges is on the roadmap. Once available, you will be able to list your CDN’s published IP ranges so that X-Real-IP also resolves to the real client when traffic arrives from those ranges. Until then, CDN customers should prefer X-Forwarded-For[0] or the CDN’s own header.

See also