Get client's real IP with CDN

Updated at:

Background

After you use the CDN acceleration service, the user access path changes to: Client → Acceleration node → Origin server. Therefore, the origin server obtains the IP address of the CDN node by default, instead of the real IP address of the client. This affects the accuracy of access data statistics.

To resolve this issue, you must configure your origin server to retrieve the real IP address of the client. By default, Alibaba Cloud CDN forwards the client's IP address to the origin server in the ali-cdn-real-ip request header.

Note

The ali-cdn-real-ip header retrieves the IP address of the client that directly connects to a CDN node. If a client accesses CDN through another proxy, this header contains the IP address of the proxy server.

Procedure

This topic shows how to get the client's real IP address on an Nginx origin server in three ways:

  • Record the client's real IP address in the Nginx access log.

  • Add a custom field that contains the client's real IP address to the response header.

  • Return the client's real IP address directly in the response body through a specific API endpoint.

Configure a custom log format

Use the log_format directive to define a new log format named custom_log. This format builds on the default log information by adding the $http_ali_cdn_real_ip variable to record the value of the ali-cdn-real-ip request header.

   log_format custom_log '$remote_addr - $remote_user [$time_local] '
                         '"$request" $status $body_bytes_sent '
                         '"$http_referer" "$http_user_agent" '
                         'ali_cdn_real_ip:"$http_ali_cdn_real_ip"';

Configure a test endpoint

Use the location directive to configure the /api/ip endpoint, which directly returns the client's real IP address.

   location /api/ip {
       # Get the real client IP from the request header.
       set $real_ip $http_ali_cdn_real_ip;
       # Add the real IP to the response header.
       add_header realip $real_ip;
       # Set the response content type.
       add_header Content-Type text/plain;
       # Return the real IP in the response body.
       return 200 $real_ip;
   }

Complete configuration example

The following code shows a complete Nginx configuration. Replace parameters such as server_name to match your requirements.

Important
  • After you modify the Nginx configuration, you must restart the Nginx service.

  • When using this configuration, you must set the default origin host in CDN to match the server_name value.

# /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
    worker_connections 1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    # Add ali_cdn_real_ip
    log_format custom_log '$remote_addr - $remote_user [$time_local] '
                         '"$request" $status $body_bytes_sent '
                         '"$http_referer" "$http_user_agent" '
                         'ali_cdn_real_ip:"$http_ali_cdn_real_ip"';
    sendfile        on;
    keepalive_timeout  65;
    # Gzip Settings
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
    server {
        listen 80;
        server_name localhost; #  Your CDN domain name
        # Custom format
        access_log /var/log/nginx/access.log custom_log;
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
        # The API for getting the real client IP
        location /api/ip {
            # Get the real IP from response header
            set $real_ip $http_ali_cdn_real_ip;
            # Add the IP to header
            add_header realip $real_ip;
            # Response
            add_header Content-Type text/plain;
            return 200 $real_ip;
        }
        # Error page
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
    # HTTPS configurations
    # server {
    #     listen 443 ssl;
    #     server_name localhost; # Your CDN domain name
    #
    #     ssl_certificate /path/to/cert.pem;
    #     ssl_certificate_key /path/to/privkey.pem;
    #
    #     access_log /var/log/nginx/access.log custom_log;
    #
    #     location /api/ip {
    #         set $real_ip $http_ali_cdn_real_ip;
    #         add_header realip $real_ip;
    #         add_header Content-Type text/plain;
    #         return 200 $real_ip;
    #     }
    # }
}

Verification

Verify the response header and response body

Access the /api/ip interface through the CDN accelerated domain name.

  • The response header contains the realip field, which holds the client's real IP address.

    ~ % curl -voa "http://nginx.xxx.com/api/ip"
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
      0     0    0     0    0     0      0       0 --:--:-- --:--:-- --:--:--     0* Host nginx.xxx.com:80 was resolved.
    * IPv6: xxx
    * IPv4:
    *   Trying xxx...
    * Connected to nginx.xxx.com (xxx) port 80
    > GET /api/ip HTTP/1.1
    > Host: nginx.xxx.com
    > User-Agent: curl/8.7.1
    > Accept: */*
    >
    * Request completely sent off
        0     0    0     0    0     0      0       0 --:--:-- --:--:-- --:--:--     0< HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/plain
    < Content-Length: 13
    < Connection: keep-alive
    < Date: Tue, 27 Jan 2026 03:02:56 GMT
    realip: 140.xxx.21
    < xxx ens-cache30.l2cn9026[898,0], cache11.cn3259[944,944,200-0,M], cache14.cn3259[976,0]
    < Ali-Swift-Global-Savetime: 1769482976
    < X-Cache: MISS TCP_MISS dirn:-2:-2
    < X-Swift-SaveTime: Tue, 27 Jan 2026 03:02:56 GMT
    < X-Swift-CacheTime: 0
    < Timing-Allow-Origin: *
    < EagleId: xxx
    <
    { [13 bytes data]
    100    13  100    13    0     0     11      0  0:00:01  0:00:01 --:--:--    11
  • The response body contains the client's real IP address.

    curl "http://nginx.xxx.com/api/ip"
    140.xxx.21x

Verify the access log

On the origin server, check the Nginx access log (default path: /var/log/nginx/access.log). The log will contain the ali_cdn_real_ip field and its corresponding value.

[root@xxx            ~]# tail -1000f /var/log/nginx/access.log
123.xxx.25 - - [27/Jan/2026:11:02:56 +0800] "GET /api/ip HTTP/1.1" 200 13 "-" "curl/8.7.1xxx ali_cdn_real_ip:"140.xxx.21"