App sees your proxy's IP, not the visitor's? Fix X-Forwarded-For
Short answer: If your app logs every visitor with the same IP address, rate-limits everyone at once or blocks the wrong people, it's seeing the address of your proxy, load balancer or CDN instead of the visitor. The real address travels in a header, usually X-Forwarded-For. Configure your web server or framework to read it only from proxies you control, using nginx's real IP module or a setting such as Express's trust proxy, and never trust the header from anyone else, because visitors can fake it.
Why your app sees the wrong address
When a visitor connects directly to your server, the connection's source address is theirs. Put anything in front, such as nginx on the same machine, a cloud load balancer or a CDN, and the app now receives connections from that intermediary. Every request appears to come from the same handful of addresses.
That breaks things quietly:
- Logs and analytics show one IP for all traffic.
- Rate limits treat every visitor as one, so a busy hour locks everyone out.
- Fraud checks, login-attempt limits and geolocation stop working.
- Blocking an abusive address blocks your own load balancer.
- Audit trails can't say who did what.
How the visitor's address is passed along
Proxies add the original address to a request header. MDN's reference explains how X-Forwarded-For works: when a request passes through several proxies, each one's address is appended, so the rightmost address is the most recent proxy and the leftmost is the originating client, assuming well-behaved clients and proxies.
There's also a standardised header, Forwarded, defined in RFC 7239, with entries like for=192.0.2.60;proto=http;by=203.0.113.43. Some CDNs use their own single-value header for the visitor address as well. Whichever you use, the principle is the same.
The part people get wrong: trust
The header is just text. Any visitor can send their own X-Forwarded-For: 1.2.3.4 and, if your app believes it, appear to come from anywhere. MDN warns that any part of the header not added by a trusted proxy may be spoofed, and that if your server can be reached directly from the internet, even while also sitting behind a trusted proxy, you can't trust any of it without checking where the request came from.
So the rule is: only read the forwarded address when the request arrived from a proxy you control, and only take the part of the header your proxies added.
Configuring nginx
nginx's real IP module changes the client address to one sent in a specified header. Its key directives:
set_real_ip_fromlists the addresses or ranges of trusted proxies, such as your load balancer's private network or your CDN's published ranges.real_ip_headernames the header to read, such asX-Forwarded-For.real_ip_recursivecontrols whether nginx walks back through the header, skipping trusted addresses, to find the first untrusted one, which is the visitor.
After that, nginx's logs, access rules and rate limits see the real visitor address. When nginx forwards to your app, pass it on with proxy_set_header X-Forwarded-For and X-Real-IP so the app sees it too.
Configuring your framework
Most frameworks have a setting for this:
- Express has a
trust proxysetting. Its documentation explains thatreq.ipandreq.ipsare populated from the socket address and theX-Forwarded-Forheader, starting at the first untrusted address. Set it to your proxy's address, a subnet or a hop count rather than simplytrue. - Django, Laravel, Rails and others offer trusted-proxy settings or middleware that work the same way.
Configure trust in one place only, either the web server or the app, and make sure the other layer isn't overriding it.
Lock down direct access
Trusting the header only from your proxies is pointless if visitors can bypass the proxy. Make sure:
- The app server only accepts traffic from the load balancer or CDN, using firewall rules or private networking.
- If you use a CDN, the origin rejects requests that don't come from the CDN's ranges, or carry a secret header the CDN adds.
Don't forget the protocol
Proxies also pass the original scheme in X-Forwarded-Proto. If your app thinks every request is plain HTTP because TLS ends at the load balancer, it may build wrong links or loop on redirects to HTTPS. The same trust settings usually cover this header too.
Checking it works
- Visit a test page that prints the address your app sees, from your phone on mobile data and from your office.
- Send a request with a fake
X-Forwarded-Forvalue and confirm the app ignores it. - Check your logs show varied visitor addresses and that rate limits apply per visitor.
A worked example
Picture a hypothetical Atlanta event-ticketing site that put a load balancer in front of two app servers. On the next on-sale, the login rate limiter locked out thousands of fans within minutes: every request appeared to come from the load balancer's two addresses. The developer configured nginx's real IP module with the load balancer's subnet as trusted and recursive lookup on, set the app's trusted proxy setting to match, and restricted the app servers to accept traffic only from the load balancer. A test with a forged header confirmed it was ignored.
FAQ
Can I just take the first IP in X-Forwarded-For?
Not safely. The first entry is whatever the client claimed. Walk back from the right, skipping your trusted proxies.
What if I use several proxy layers?
List all of them as trusted, so the recursive lookup skips each and lands on the visitor.
Does this affect privacy?
You'll store real visitor addresses in logs, as you would without a proxy. Keep logs only as long as you need them.
Why does my CDN have its own header?
Some CDNs provide a single, verified visitor address header. If you only accept traffic from that CDN, it's often the simplest choice.
If your app is blocking the wrong people or logging one address for everyone, tell us how it's hosted and we'll help fix it.