使用Cloudflare CDN后,NGNIX下查看网站访问者真实ip的方法 2025-06-03 Website News 暂无评论 40 次阅读 ### 检查Ngnix是否有realip模块 首先确保nginx是否启用了http_realip模块。我这里是在Ubuntu 22.04中使用apt安装的nginx,已经包含了该模块。可以使用如下指令检查nginx -V输出的信息是否包含--with-http_realip_module。 ```csharp nginx -V 2>&1 | grep "realip" ``` 看到结果: ```csharp nginx: ngx_http_realip_module (static) ``` 说明默认已经安装了相关模块。 ### 配置CF IP地址块 然后修改nginx的配置文件/etc/nginx/nginx.conf,在http块中添加类似如下内容: ```csharp set_real_ip_from 103.21.244.0/22; set_real_ip_from 103.22.200.0/22; set_real_ip_from 103.31.4.0/22; set_real_ip_from 104.16.0.0/13; set_real_ip_from 104.24.0.0/14; set_real_ip_from 108.162.192.0/18; set_real_ip_from 131.0.72.0/22; set_real_ip_from 141.101.64.0/18; set_real_ip_from 162.158.0.0/15; set_real_ip_from 172.64.0.0/13; set_real_ip_from 173.245.48.0/20; set_real_ip_from 188.114.96.0/20; set_real_ip_from 190.93.240.0/20; set_real_ip_from 197.234.240.0/22; set_real_ip_from 198.41.128.0/17; set_real_ip_from 2400:cb00::/32; set_real_ip_from 2606:4700::/32; set_real_ip_from 2803:f800::/32; set_real_ip_from 2405:b500::/32; set_real_ip_from 2405:8100::/32; set_real_ip_from 2a06:98c0::/29; set_real_ip_from 2c0f:f248::/32; real_ip_header CF-Connecting-IP; ``` CF的IP地址列表:https://www.cloudflare.com/zh-cn/ips/ 最后使用 ```csharp nginx -t ``` 验证配置文件, ```csharp nginx -s reload ``` 重新加载配置即可。 其他web服务的配置方法可以参看CloudFlare官方的说明文档,链接如下: ```csharp https://developers.cloudflare.com/support/troubleshooting/restoring-visitor-ips/restoring-original-visitor-ips/#nginx-1 ``` 标签: cloudflare, nginx, CF, realip 本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭