我的网站使用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 ...'