Your app sees the load balancer's IP instead of the real client IP. How do you get the real one?
BeginnerScenarioLoad Balancing, Proxies & CDN
Answer
Situation read → Proxies replace the source address, so the original goes in a header or the PROXY protocol.
What I'd do →
- Read
X-Forwarded-For(the left-most is the original client, but only trust entries added by your own proxies) - In Nginx use
set_real_ip_from 10.0.0.0/16; real_ip_header X-Forwarded-For; - On NLB enable client IP preservation or PROXY protocol v2
- Log
$http_x_forwarded_forand verify withcurl -H 'X-Forwarded-For: 1.2.3.4'to confirm spoofing is handled
Why → A client can send any XFF value, so trust only the hops you control.
Interview tip → Say that rate limiting and allow lists on a spoofable header are a security bug.
More on Load Balancing, Proxies & CDN
What is the difference between a Layer 4 and a Layer 7 load balancer?BeginnerWhen do you choose an ALB and when an NLB in AWS?BeginnerForward proxy versus reverse proxy: what is the difference?BeginnerHow do health checks work on a load balancer and what makes a good one?BeginnerWhat are sticky sessions and why would you avoid them?BeginnerWhat does a CDN do and when does it help?Beginner