how to log client real ip and also CDN ip?

1.8k views Asked by At

I'm using cloudflare for my website.

The issue I got is, after I enabled realip module on nginx, all CDN ips are replaced to user's realip. Then when I check the nginx log, I can't find out whether user is connecting via CDN or connection directly to server.

Below is my configuration:

set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
...
real_ip_header X-Forwarded-For;

....
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '

Is there any way I can keep both realip and CDN IPs?

1

There are 1 answers

0
nbari On

Taken from: Including the original visitor IP in your logs

You can include the variables $http_cf_connecting_ip and $http_x_forwarded_for in the log_format directive.

If you want both the CDN IP and the CF-Connecting-IP don't use the real_ip_header and add $http_cf_connecting_ip to your log_format:

log_format  main  '$remote_addr - $http_cf_connecting_ip ...'