如何配置宝塔Nginx的CF‑Connecting‑IP?
美国、香港服务器
如何配置宝塔Nginx的CF‑Connecting‑IP?
10-14 来源:
## 式一:自动拉取 CF 官方网段(推荐,自动更新,避免手动复制网段过期)
SSH 执行下面脚本,自动下载 Cloudflare IPv4+IPv6 网段,生成独立配置文件`/www/server/nginx/conf/cloudflare-realip.conf`
#下载ipv4
curl -fsS https://www.cloudflare.com/ips-v4 -o /tmp/cf_v4.txt
awk "!seen[$0]++ {print "set_real_ip_from " $1 ";"}" /tmp/cf_v4.txt > /www/server/nginx/conf/cloudflare-realip.conf
#下载ipv6并追加
curl -fsS https://www.cloudflare.com/ips-v6 -o /tmp/cf_v6.txt
awk "!seen[$0]++ {print "set_real_ip_from " $1 ";"}" /tmp/cf_v6.txt >> /www/server/nginx/conf/cloudflare-realip.conf
#追加核心realip指令
cat >> /www/server/nginx/conf/cloudflare-realip.conf <<"EOF"
real_ip_header CF-Connecting-IP;
real_ip_recursive on;
EOF
打开宝塔面板 →【软件商店】→Nginx→【配置修改】
找到最外层 `http {` 大括号,在内部加入一行引入配置文件:
include /www/server/nginx/conf/cloudflare-realip.conf;
写在`http{}`全局,**不要写在单个 server {} 内**,保证所有站点、宝塔 Nginx 防火墙全部生效宝塔面板。
保存,先校验语法,再重载 Nginx
nginx -t
nginx -s reload
方式二:手动写入网段(临时测试,不建议长期,CF 网段会新增)
Nginx 配置修改,`http{}`内粘贴
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;
real_ip_header CF-Connecting-IP;
real_ip_recursive on;
严禁写 `set_real_ip_from 0.0.0.0/0;`,攻击者可以伪造`CF‑Connecting‑IP`篡改访客 IP,高危漏洞宝塔面板。
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快