Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
Help Center/ Content Delivery Network/ Best Practices/ Obtaining Real IP Addresses of Users

Obtaining Real IP Addresses of Users

Updated on 2024-11-14 GMT+08:00

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. Obtain real IP addresses of users from the value of the X-Forwarded-For field in the localhost_access_log file.

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. Obtain real IP addresses of users from the value of the X-Forwarded-For field in the access.log file.

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). 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. Obtain real IP addresses of users from the value of the X-Forwarded-For field in 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).

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. Obtain real IP addresses of users from the value of the X-Forwarded-For field in 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).

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback