Nginx获取 Cloudflare CDN 访问用户真实IP 作者: Hogwarts 发布于: 2023-06-29 更新于: 2023-07-18 分类: 默认分类 #方法一 server中加入以下代码 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/12; 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 2c0f:f248::/32; set_real_ip_from 2a06:98c0::/29; real_ip_header CF-Connecting-IP; #与下面两行二选一 #real_ip_header X-Forwarded-For; #real_ip_recursive on; **重启 NGINX** IP段不全可自行补齐。 [Cloudflare IP 范围](https://www.cloudflare.com/zh-cn/ips/ "Cloudflare IP 范围") #方法二 在nginx伪静态中添加如下代码 set_real_ip_from 0.0.0.0/0; set_real_ip_from ::/0; real_ip_header X-Forwarded-For; 方法二还未充分测试 本文抄自:[Cloudflare CDN - 获取访问用户真实IP(而不是CDN节点IP)](https://limbopro.com/archives/15342.html "Cloudflare CDN - 获取访问用户真实IP(而不是CDN节点IP)") 更多设置可参考:[启用CDN后网站获取用户真实IP:Cloudflare CDN真实IP地址(Nginx,Apache)](https://wzfou.com/cdn-real-ip/ "启用CDN后网站获取用户真实IP:Cloudflare CDN真实IP地址(Nginx,Apache)") 标签: apache, nginx, cdn, cloudflare