You wire up rate limiting in your application, push to production on managed PaaS, and watch your logs. Every visitor shows the same IP — 10.150.0.7, the platform load balancer. The rate limiter bans 10.150.0.7 within an hour because it sees all 50,000 customers as one user. Worse, your security team is asking why GeoIP logs show every login from a single Canadian data centre. The fix is reading the right header, but the trap is reading it from any source — because if you do, an attacker can spoof their IP by setting X-Forwarded-For: 8.8.8.8 themselves.
What’s happening
When a request hits PaaS, the path is roughly:
Client (203.0.113.42) → Platform LB (10.150.0.7) → Your container (10.20.0.11)
Your application listens on the container’s interface and the TCP source it sees is 10.150.0.7. That’s correct — the LB terminated the original TCP connection and opened a new one to your container. The real client IP isn’t lost; it’s been moved into an HTTP header by the LB. On shared and dedicated PaaS LBs the conventions are:
X-Forwarded-For— comma-separated list,<client>, <proxy1>, <proxy2>. The LB appends its view of the client’s IP to whatever the client sent.X-Real-IP— the LB’s single best guess at the client IP. Set by nginx-class LBs.Forwarded(RFC 7239) — the modern unified header. Most PaaS LBs don’t set it; you’ll see X-Forwarded-* instead.- PROXY protocol v1/v2 — for L4 traffic (TCP, non-HTTP), the LB prepends a small header to the TCP stream itself. Used for Postgres, Redis, MQTT, custom protocols. The receiving service must opt in.
The mistake operators make is reading X-Forwarded-For blindly. The header is client-supplied unless your edge proxy overwrites it. If your application reads it directly from any client without validating the trust chain, an attacker sends:
GET /login HTTP/1.1
X-Forwarded-For: 192.168.1.1
…and your audit log records the login as coming from 192.168.1.1. Your rate limiter exempts them. Your WAF rule trusts them. You have a header-injection-based authentication bypass.
The correct pattern is: trust the X-Forwarded-For chain only as far as your known proxy boundary. Walk the comma-separated list right-to-left, skip IPs that belong to your trusted proxies, and the first non-trusted IP you encounter is the real client. Every IP further left in the chain was forged by whatever sent the request.
How to verify
Run curl from a known external IP and inspect what your application receives. A quick diagnostic endpoint to drop into your app:
# Flask example
@app.route('/whoami')
def whoami():
return {
'remote_addr': request.remote_addr,
'x_forwarded_for': request.headers.get('X-Forwarded-For'),
'x_real_ip': request.headers.get('X-Real-IP'),
'forwarded': request.headers.get('Forwarded'),
}
curl -s https://app.example.com/whoami
# {
# "remote_addr": "10.150.0.7",
# "x_forwarded_for": "203.0.113.42",
# "x_real_ip": "203.0.113.42",
# "forwarded": null
# }
Good — the platform LB is rewriting headers. Now test with a spoofed header from outside the platform:
curl -s -H "X-Forwarded-For: 1.2.3.4" https://app.example.com/whoami
# Expected: x_forwarded_for shows "1.2.3.4, 203.0.113.42"
# OR: x_forwarded_for shows just "203.0.113.42" (LB overwrote)
Two outcomes:
- The header is appended (
1.2.3.4, 203.0.113.42): your application must walk the chain and trust only the rightmost entry. If you trust the leftmost, you’ve just been spoofed. - The header is overwritten (
203.0.113.42): the LB is aggressive enough to clobber client-supplied values. Safer, but less common and you should not depend on it without confirming with the platform.
For L4 services (Postgres, Redis), the PROXY protocol test:
# Check what your Postgres sees
psql -h app-db.example.com -U app -c "SELECT inet_client_addr();"
# Without PROXY: 10.150.0.7 (the LB)
# With PROXY enabled on both LB and Postgres: 203.0.113.42 (the real client)
The fix
Three pieces — pick the ones that apply to your stack.
1. Configure your web framework to read X-Forwarded-For safely. Every framework has a “trusted proxies” knob. Don’t skip it.
# Flask + ProxyFix - trust exactly 1 proxy hop (the platform LB)
from werkzeug.middleware.proxy_fix import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1)
# Django settings.py - use the IP of the FAR-RIGHT entry, skipping the LB
USE_X_FORWARDED_HOST = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Then in middleware, walk X-Forwarded-For right-to-left
// Express - trust exactly 1 proxy hop
app.set('trust proxy', 1);
// Now req.ip is the real client, not the LB
# If your container is running nginx in front of an upstream app,
# the real_ip module is the right tool. Whitelist your PaaS LB CIDRs:
set_real_ip_from 10.150.0.0/16;
set_real_ip_from 10.151.0.0/16;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
real_ip_recursive on is the key — nginx walks the chain right-to-left, skipping any IP in the trusted CIDRs, and assigns $remote_addr to the first untrusted entry. That’s the value safe to log, rate-limit on, and use for GeoIP.
2. If you run your own dedicated LB, set the headers explicitly. Don’t append to client-supplied X-Forwarded-For — overwrite it. The dedicated LB is your trust boundary; nothing client-controlled should pass through it unchanged:
# Dedicated LB config — sanitize, don't trust
location / {
proxy_pass http://app_tier;
proxy_set_header X-Forwarded-For $remote_addr; # overwrite, not append
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
}
If you must preserve a chain (e.g., upstream CDN like Cloudflare in front of your LB), keep two layers of trust:
# Trust Cloudflare CIDRs as upstream
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
# (full list at https://www.cloudflare.com/ips-v4)
real_ip_header CF-Connecting-IP;
real_ip_recursive off;
When Cloudflare is the edge, prefer its CF-Connecting-IP header — it’s a single value, not a list, and harder to confuse.
3. For L4 services, enable PROXY protocol on both sides. On HAProxy in front:
backend pg_writer
mode tcp
server pg01 10.20.0.11:5432 send-proxy-v2
server pg02 10.20.0.12:5432 send-proxy-v2
On Postgres:
# postgresql.conf
listen_addresses = '*'
# proxy_protocol_listening_addresses is not in vanilla Postgres;
# use pgbouncer or pgcat in front, which both support PROXY protocol natively.
# pgbouncer.ini
listen_addr = 0.0.0.0
listen_port = 6432
peer_id = 1
;# enable PROXY protocol
peer_proxy_proto_v2 = 1
For Redis (since 7.x):
# redis.conf
proxy-protocol-enabled yes
Both sides have to agree. If the LB sends PROXY headers and the backend doesn’t expect them, the first 100 bytes of every connection look like protocol garbage and you’ll spend an afternoon staring at “invalid startup packet” errors before you find it.
Run the diagnostic endpoint from step 1 after every change. The whoami response is the cheapest way to know whether your trust chain is correct or just lucky.
If you’d like the PaaS side wired with the right headers and the right trust boundary on the LB you don’t operate, our managed PaaS team configures the platform LB’s real-IP behaviour and validates the chain end-to-end as part of environment management — request a networking review.