网站挂载CloudFlare后获取用户真实IP的Nginx.conf配置
ChinaSiro/CloudFlare-RealiP-nginx
1 更新于2025-10-13 23:10:20
我每个站点都优选到了CF,但是回来的都是CF后的IP。
也就是我们nginx日志中出现的都不是用户真实IP,而是加速后的。
虽然我做的东西都不限制IP频次,但我也不想无法控制。
避免有人利用攻击导致我封了CF的IP让大量用户无法访问。
写了一段配置放到nginx.conf中的可以获取
# ===== Cloudflare Real IP Support =====
# 2025/10/13 by Huo0.com
# IPv4
set_real_ip_from 173.245.48.0/20;
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 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;
# IPv6
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;
# Use Cloudflare header for real IP
real_ip_header CF-Connecting-IP;但站点太多,手动太麻烦。
就又写到git 直接 命令行一键配置+重载
宝塔云+debian¢os 一键脚本
CF_URL='https://raw.githubusercontent.com/ChinaSiro/CloudFlare-RealiP-nginx/main/realip.conf'; TARGET='/www/server/panel/vhost/nginx/realip.conf'; sudo mkdir -p "$(dirname "$TARGET")" && sudo curl -fsSL "$CF_URL" -o "$TARGET" && sudo nginx -t && sudo /www/server/nginx/sbin/nginx -s reload
评论区(暂无评论)