How To Obtain the Client's Real IP?

Last Updated At: 2025-10-21 09:10:00

CLB's layer-4 (TCP/UDP) and layer-7 (HTTP/HTTPS) services both support obtaining the client's real IP address directly on the backend CVM without requiring additional configuration.

  • For layer-4 CLB, the source IP obtained on the backend CVM is the client IP.
  • For layer-7 CLB, you can use X-Forwarded-For or remote_addr field to directly obtain the client IP.

    Note:
    For CLB, no additional configuration is required on the backend CVM to obtain the client IP.
    For other layer-7CLB services that have SNAT, you need to configure it on the backend CVM and then use X-Forwarded-For to obtain the client's real IP address.

The following section introduces common application server configuration solutions.

IIS 6 Configuration Scheme

  1. Download and install the plugin F5XForwardedFor module, copy F5XForwardedFor. dll from x86\ Release or x64\ Release directory to a directory, assuming C:\ISAPIFilters, depending on your server operating system version, and ensure that the IIS process has read access to that directory.
  2. Open IIS Manager, find the currently open website, right-click on the website, and select Attribute to open the attribute page.
  3. On the property page, switch to ISAPI Filter, click Add, and the Add window pops up.
  4. In the Add window, fill in F5XForwardedFor in Filter Name and fill in the full path of F5XForwardedFor.dll in Executable File and click OK.
  5. Restart the IIS server and wait for the configuration to take effect.

IIS 7 Configuration Scheme

  1. Download and install the plugin F5XForwardedFor module, copy F5XFFHttpModule. dll and F5XFFHttpModule. ini in the x86\ Release or x64\ Release directory to a directory, assuming C:\F5XForwardedFor, according to your server operating system version, and ensure that the IIS process has read access to the directory.
  2. Select IIS Server and double-click the Module feature.


3. Click Configure Native Module.


4. Click on Register in the pop-up window.


5. Add the downloaded DLL file as shown below:


6. After addition, check and click OK.


7. Add the two DLLs above in ISAPI and CGI Restrictions and set the restrictions to allow.


8. Restart the IIS server and wait for the configuration to take effect.

Apache Configuration Solution

  1. Install the Apache third-party module mod_rpaf.

    wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
    tar zxvf mod_rpaf-0.6.tar.gz
    cd mod_rpaf-0.6
    /usr/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
    
  2. Modify Apache configuration /etc/httpd/conf/httpd.conf, add at the end:

    LoadModule rpaf_module modules/mod_rpaf-2.0.so
    RPAFenable On
    RPAFsethostname On
    RPAFproxy_ips IP address (This IP address is not the public IP provided by the CLB. You can check the Apache log for the specific IP address. Usually there are two IP addresses that need to be written down.)
    RPAFheader X-Forwarded-For
    
  3. After addition, restart Apache.

    /usr/sbin/apachectl restart
    

Nginx configuration solution

  1. When Nginx is used as a server, http_realip_module is used to obtain the real IP address of the client. The default installed Nginx does not have this module installed. You need to recompile Nginx and add --with-http_realip_module.

    wget  http://nginx.org/download/nginx-1.14.0.tar.gz 
    tar  zxvf nginx-1.14.0.tar.gz 
    cd nginx-1.14.0
    ./configure --user=www --group=www --with-http_stub_status_module --without-http-cache --with-http_ssl_module --with-http_realip_module
    make
    make install
    
  2. Modify nginx.conf.

    vi /etc/nginx/nginx.conf
    

Modify the red part as follows:

  
fastcgi connect_timeout 300;
fastcgi send_timeout 300;
fastcgi read_timeout 300;
fastcgi buffer_size 64k;
fastcgi buffers 4 64k;
fastcgi busy_buffers_size 128k;
fastcgi temp_file_write_size 128k;

set_real_ip_from IP address; (This IP address is not the public IP provided by the CLB. You can check the previous nginx log for the specific IP address. If there are multiple IP addresses, write them down.)
real_ip_header X-Forwarded-For;
 
3. Restart Nginx.
service nginx restart