Help Center> Content Delivery Network> Best Practices> Obtaining Real IP Addresses of Users
Updated on 2022-10-26 GMT+08:00

Obtaining Real IP Addresses of Users

This section describes how to obtain real IP addresses of users on different types of web servers (including Tomcat, Apache, Nginx, IIS 6, and IIS 7).

Background

After a website is connected to CDN for acceleration, IP addresses obtained by the origin server from IP address headers are not the real IP addresses of users. You can configure your web server to obtain the real IP addresses of users.

When a proxy server (such as CDN and WAF) forwards an HTTP, WebSocket, or WSS request to the next server, the proxy server adds the X-Forwarded-For field to the request header to record the real IP address of the user. The format of the record is X-Forwarded-For: Real IP address of the user, IP address of the proxy server 1, IP address of the proxy server 2, IP address of the proxy server 3, ..., IP address of the proxy server N.

You can obtain the real IP address of the user by obtaining the first IP address from the X-Forwarded-For field.

Nginx

If an Nginx reverse proxy is deployed on your origin server, you can configure location information on the Nginx reverse proxy so that the backend web server can use similar functions to obtain real IP addresses of users.

  1. Configure the following information in the corresponding location of the Nginx reverse proxy to obtain the information about real IP addresses of users:
    Location ^ /<uri> {
        proxy_pass  ....;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  2. Enable the backend web server to use similar functions to obtain real IP addresses of users.
    request.getAttribute("X-Forwarded-For")

Tomcat

If Tomcat is deployed on your origin server, you can enable the X-Forwarded-For function of Tomcat to obtain real IP addresses of users.

  1. Open the server.xml file in the tomcat/conf/ directory. Partial information about the AccessLogValve logging function is as follows:
    <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
            <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                   prefix="localhost_access_log." suffix=".txt"
                   pattern="%h %l %u %t "%r" %s %b"/>
  2. Add %{X-Forwarded-For}i to pattern. Part of the modified server.xml file is as follows:
    <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
            <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                   prefix="localhost_access_log." suffix=".txt"
                   pattern="%{X-Forwarded-For}i %h %l %u %t "%r" %s %b" />
    </Host>
  3. View the localhost_access_log file to obtain real IP addresses of users from the value of the X-Forwarded-For field.

Apache

If Apache is deployed on your origin server, you can run commands to install the third-party module mod_rpaf of Apache, and modify the http.conf file to obtain real IP addresses of users.

  1. Run the following commands to install the third-party module mod_rpaf of Apache:
    wget https://github.com/gnif/mod_rpaf/archive/v0.6.0.tar.gz
    tar xvfz mod_rpaf-0.6.tar.gz
    cd mod_rpaf-0.6
    /usr/local/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
  2. Open the httpd.conf configuration file and modify the file content as follows:

    LoadModule rpaf_module   modules/mod_rpaf-2.0.so ##Load module mod_rpaf.
    <IfModule mod_rpaf.c>
    RPAFenable On
    RPAFsethostname On
    RPAFproxy_ips 127.0.0.1 <Reverse proxy IP address>
    RPAFheader X-Forwarded-For 
    </IfModule>
  3. Define the log format.
    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" common
  4. Enable custom logs.
    CustomLog"[Apache server directory]/logs/$access.log"common
  5. Restart the Apache server for the configuration to take effect.
    /[Apache server directory]/httpd/bin/apachectl restart
  6. View the access.log file to obtain real IP addresses of users from the value of the X-Forwarded-For field.

IIS 6

If IIS 6 is deployed on your origin server, you can install the F5XForwardedFor.dll plug-in to obtain real IP addresses of users from access logs recorded by the IIS 6 server.

  1. Download the F5XForwardedFor module.
  2. Copy the F5XForwardedFor.dll file in the x86\Release or x64\Release directory based on the OS version of your server to a specific directory (for example, C:\ISAPIFilters) and ensure that the IIS process has the read permission on the destination directory.
  3. Open the Internet Information Services (IIS) Manager, right-click the website that is currently open, and choose Properties from the shortcut menu.
  4. In the Properties dialog box, click the ISAPI Filters tab, and click Add. In the dialog box that is displayed, configure the following information:
    • Filter name: Enter F5XForwardedFor.
    • Executable: Enter the full path of the F5XForwardedFor.dll file, for example, C:\ISAPIFilters\F5XForwardedFor.dll.
  5. Click OK to restart the IIS 6 server.
  6. View the access logs recorded by the IIS 6 server (the default log path is C:\WINDOWS\system32\LogFiles\, and the IIS log file name extension is .log) to obtain real IP addresses of users from the value of the X-Forwarded-For field.

IIS 7

If IIS 7 is deployed on your origin server, you can install the F5XForwardedFor module to obtain real IP addresses of users from access logs recorded by the IIS 7 server.

  1. Download the F5XForwardedFor module.
  2. Copy the F5XFFHttpModule.dll and F5XFFHttpModule.ini files in the x86\Release or x64\Release directory based on the OS version of the server to the specific directory (for example, C:\x_forwarded_for\x86 or C:\x_forwarded_for\x64) and ensure that the IIS process has the read permission on the destination directory.
  3. Double-click Modules in the IIS server option.
  4. Click Configure Native Modules. In the dialog box that is displayed, click Register.

  5. In the dialog box that is displayed, register the downloaded DLL file based on the OS and click OK.
    • x86: Register the module x_forwarded_for_x86.
      • Name: Enter x_forwarded_for_x86.
      • Path: Enter C:\x_forwarded_for\x86\F5XFFHttpModule.dll.

    • x64: Register the module x_forwarded_for_x64.
      • Name: Enter x_forwarded_for_x64.
      • Path: Enter C:\x_forwarded_for\x64\F5XFFHttpModule.dll.

  6. After the registration is complete, select the newly registered module (x_forwarded_for_x86 or x_forwarded_for_x64) and click OK.
  7. In ISAPI and CGI Restrictions, add the registered DLL file based on OS and set Restriction to Allowed.
    • x86
      • ISAPI or CGI path: Enter C:\x_forwarded_for\x86\F5XFFHttpModule.dll.
      • Description: Enter x86.
    • x64
      • ISAPI or CGI path: Enter C:\x_forwarded_for\x64\F5XFFHttpModule.dll.
      • Description: Enter x64.
  8. Restart the IIS 7 server and wait for the configuration to take effect.
  9. View the access logs recorded by the IIS 7 server (the default log path is C:\WINDOWS\system32\LogFiles\, and the IIS log file name extension is .log) to obtain real IP addresses of users from the value of the X-Forwarded-For field.