When you use ELB to route requests to backend servers, IP addresses of the clients will be translated by the ELB system. This FAQ provides the operations for obtaining the IP addresses of the clients.
Configure the application server and obtain the IP address of a client from the HTTP header.
The real IP address is placed in the X-Forwarded-For header field by the load balancer in the following format:
X-Forwarded-For: IP address of the client,Proxy server 1-IP address,Proxy server 2-IP address,...
If you use this method, the first IP address obtained is the IP address of the client.
Apache Server
For example, if CentOS 7.5 is used as the OS, run the following command to install the software:
yum install httpd
LoadModule remoteip_module modules/mod_remoteip.so RemoteIPHeader X-Forwarded-For RemoteIPInternalProxy 100.125.0.0/16
Add the IP address range of the proxy server after RemoteIPInternalProxy.
LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
systemctl restart httpd
Nginx Server
For example, if CentOS 7.5 is used as the OS, run the following command to install the software:
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel wget http://nginx.org/download/nginx-1.17.0.tar.gz tar zxvf nginx-1.17.0.tar.gz cd nginx-1.17.0 ./configure --prefix=/path/server/nginx --with-http_stub_status_module --without-http-cache --with-http_ssl_module --with-http_realip_module make make install
vi /path/server/nginx/conf/nginx.conf
Add the following information under http or server:
set_real_ip_from 100.125.0.0/16; real_ip_header X-Forwarded-For;
Add the IP address range of the proxy server after set_real_ip_from <IP_address>.
/path/server/nginx/sbin/nginx
cat /path/server/nginx/logs/access.log
Tomcat Servers
In the following operations, the Tomcat installation path is /usr/tomcat/tomcat8/.
ps -ef|grep tomcat netstat -anpt|grep java
vim /usr/tomcat/tomcat8/conf/server.xml
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%{X-FORWARDED-FOR}i %l %u %t %r %s %b %D %q %{User-Agent}i %T" resolveHosts="false" />
cd /usr/tomcat/tomcat8/bin && sh shutdown.sh && sh startup.sh
/usr/tomcat/tomcat8/ is where Tomcat is installed. Change it based on site requirements.
As highlighted in the following figure, IP addresses that are not in the IP address range starting with 100.125 are the source IP addresses.
cd /usr/tomcat/tomcat8/logs/ cat localhost_access_log..2021-11-29.txt
In this command, localhost_access_log..2021-11-29.txt indicates the log path of the current day. Change it based on site requirements.
Windows Server with IIS Deployed
The following uses Windows Server 2012 with IIS7 as an example to describe how to obtain the source IP address.
For load balancing at Layer 4 (TCP or UDP listeners), use either of the following methods to obtain the real IP address of a client:
If you enable this function, a server cannot be used as both the client and the backend server. If the client and the backend server use the same server and the Obtain Client IP Address option is enabled, the backend server will think the packet from the client is sent by itself and will not return a response packet to the load balancer. As a result, the return traffic will be interrupted.
TCP listeners require the TOA plug-in to obtain real IP addresses. For details, see Configuring the TOA Module.