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/16Figure 1 Content to be added
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
;100.125.0.0/16set_real_ip_from real_ip_header X-Forwarded-For;Figure 2 Content to be added
Add the IP address range of the proxy server after set_real_ip_from.
/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 javaFigure 3 Tomcat running properly
<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" />
Figure 4 Example configuration
cd /usr/tomcat/tomcat8/bin && sh startup.sh
In this command, /usr/tomcat/tomcat8/ is the Tomcat installation path. Change it based on site requirements.
Figure 5 Restarting the Tomcat service
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.
cat localhost_access_log..2020-09-10.txt
In this command, localhost_access_log..2020-09-10.txt indicates the log path of the current day. Change it based on site requirements.
Figure 6 Querying the source IP address
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.
Figure 8 Configure Native Modules
TCP listeners require the TOA plug-in to obtain real IP addresses. For details, see Configuring the TOA Plug-in.
Parent topic: Obtaining Source IP Addresses