Help Center> Web Application Firewall> Best Practices> Obtaining the Client IP Address

Obtaining the Client IP Address

A client IP address refers to an IP address of a visitor (or the device a visitor uses to initiate the request). Sometimes, a web application needs to require the client IP address. For example, a voting system needs to obtain the client IP addresses to ensure that each client casts only once.

You can obtain the client IP address directly from WAF when your website is connected to WAF. You can also obtain the client IP address by configuring the website server.

The following describes how to obtain the client IP address from WAF and how to configure different types of web application servers, including Tomcat, Apache, Nginx, IIS 6, and IIS 7, to obtain the client IP address.

Background

Generally, a browser request does not directly reach the web server. Proxy servers, such as CDN, WAF, and advanced anti-DDoS, may be deployed between the browser and the origin server. Using WAF as an example, see Figure 1.

Figure 1 WAF deployment diagram
  • DNS resolves your domain name to the origin server IP address before your website is connected to WAF. Therefore, web visitors can directly access the server.
  • After your website is connected to WAF, DNS resolves your domain name to the CNAME record of WAF. In this way, the traffic passes through WAF. WAF then filters out illegitimate traffic and only routes legitimate traffic back to the origin server.

In this case, the access request may be forwarded by multiple layers of security or acceleration proxies before reaching the origin server. So, how does the server obtain the real IP address of the client that initiates the request?

When forwarding HTTP requests to the downstream server, the transparent proxy server adds an X-Forwarded-For field to the HTTP header to identify the client IP address in the format of X-Forwarded-For: client IP address, proxy 1-IP address, proxy 2-IP address, proxy 3-IP address, ........->....

Then, you can obtain the client IP address from the X-Forwarded-For field, the first IP address in which is the client IP address.

Obtaining the Client IP Address from WAF

After a website is connected to WAF, WAF is deployed between the client and server as a reverse proxy to protect the website. You can use either of the following methods to obtain the client IP address:

  • Using the X-Forwarded-For field to obtain the client IP address

    The client IP address is placed in the X-Forwarded-For HTTP header field. The format is as follows:

    1
    X-Forwarded-For: Client IP address,Proxy 1-IP address,Proxy 2-IP address,...
    

    The first IP address included in the X-Forwarded-For field is the client IP address.

    The methods to obtain the X-Forwarded-For field by invoking the SDK interface in different programming languages are as follows:
    • ASP
      Request.ServerVariables("HTTP_X_FORWARDED_FOR")
    • ASP.NET(C#)
      Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
    • PHP
      $_SERVER["HTTP_X_FORWARDED_FOR"]
    • JSP
      request.getHeader("HTTP_X_FORWARDED_FOR")
  • Using the X-Real-IP field to obtain the client IP address (modifications caused by reverse proxies is considered)
    The methods to obtain the X-Real-IP field by invoking the SDK interface in different programming languages are as follows:
    • ASP
      Request.ServerVariables("HTTP_X_REAL_IP")
    • ASP.NET(C#)
      Request.ServerVariables["HTTP_X_REAL_IP"]
    • PHP
      $_SERVER["HTTP_X_REAL_IP"]
    • JSP
      request.getHeader("HTTP_X_REAL_IP")

How Does Tomcat Obtain the Client IP Address from Access Logs?

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

  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 the client IP address from the X-Forwarded-For field.

How Does Apache Obtain the Client IP Address from Access Logs?

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

  1. Run the following commands to install 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 customized 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 the client IP address from the X-Forwarded-For field.

How Does Nginx Obtain the Client IP Address from Access Logs?

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 the client IP address

  1. Configure the following information in the corresponding location of the Nginx reverse proxy to obtain the information about the client IP address:

    1
    2
    3
    4
    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 the client IP address.

    1
    request.getAttribute("X-Forwarded-For") 
    

How Does IIS 6 Obtain the Client IP Address from Access Logs?

If you have deployed an IIS 6 server on your origin server, you can install the F5XForwardedFor.dll plug-in and obtain the client IP address from the 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 to a specified directory (for example, C:\ISAPIFilters) based on the operating system version of your server. Ensure that the IIS process has the read permission on the directory.
  3. Open the IIS manager, right-click the website that is currently open, and choose Attribute from the shortcut menu. The Attribute page is displayed.
  4. On the Attribute page, switch to ISAPI filter and click Add. In the dialog box that is displayed, configure the following information:

    • Filter Name: Set this parameter to F5XForwardedFor.
    • Executable file: Set this parameter to the full path of F5XForwardedFor.dll, 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). You can obtain client IP address from the X-Forwarded-For field.

How Does IIS 7 Obtain the Client IP Address from Access Logs?

If you have deployed an IIS 7 server on your origin server, you can install the F5XForwardedFor.dll module and obtain the client IP address from the 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 to a specified directory (for example, C:\x_forwarded_for\x86 or C:\x_forwarded_for\x64) based on the operating system version of your server. Ensure that the IIS process has the read permission for the directory.
  3. On the server home page, double-click Modules to go to the Modules page.
  4. Click Configure Native Module. In the dialog box displayed, click Register.
  5. In the displayed dialog box, register the downloaded DLL file according to the operating system, and then click OK.

    • x86 operating system: registration module x_forwarded_for_x86
      • Name: x_forwarded_for_x86
      • Path: C:\x_forwarded_for\x86\F5XFFHttpModule.dll
    • x86 operating system: registration module x_forwarded_for_x64
      • Name: x_forwarded_for_x64
      • Path: 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 restriction, add the registered DLL files by operating system and change Restriction to Permitting.

    • x86 operating system:
      • ISAPI or CGI path: C:\x_forwarded_for\x86\F5XFFHttpModule.dll
      • Description: x86
    • x64 operating system:
      • ISAPI or CGI path: C:\x_forwarded_for\x64\F5XFFHttpModule.dll
      • Description: 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). You can obtain the client IP address from the X-Forwarded-For field.