Let’s say you are running ASP.NET, PHP, JSP, or Python web. If the code is visited via reverse proxy, then the client IP address is the proxy IP with regular detecting routue. To fix the issue, let’s first take a look at 3 HTTP headers added by proxies:
The IP address(s) of the client. If there are multiple proxies, you see multiple IP addresses, separated by comma. For example:
X-Forwarded-For: client1, proxy1, proxy2
The original host requested by the client in the Host HTTP request header.
The hostname of the proxy server.
It’s pretty straightforward. The flow is:
- Check if there is X-Forwarded-For in http headers.
- If yes, get the string, and split it by comma. Then get the first piece.
- If no, fall back on regular way of reading IP address.