如何记录客户端真实ip和CDN ip



我的网站使用cloudflare。

我遇到的问题是,在nginx上启用realip模块后,所有CDN ip都被替换为用户的realip。然后当我查看nginx日志时,我无法发现用户是通过CDN连接还是直接连接到服务器。

以下是我的配置:

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" '

有什么办法可以同时保留realip和CDN IP吗?

取自:在日志中包括原始访问者IP

您可以在log_format指令中包含变量$http_cf_connecting_ip$http_x_forwarded_for

如果您同时想要CDN IP和CF-Connecting-IP,请不要使用real_ip_header并将$http_cf_connecting_ip添加到您的log_format:

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

最新更新