三二互联专业提供速度最快最稳定的美国服务器、香港服务器。中美直连,亚洲优化![ 代理登陆 ] [ 付款方式 ] [ 找回密码 ][ 电子协议责任书 ]
硬件资源保障

采用高配品牌服务器

主流强悍CPU配置

确保服务高速稳定运行

中美直连线路

中美直连亚洲优化

采用中国CN2骨干网络

保证速度飞快稳定高效

如何使用Nginx配置CF-Connecting-IP头?

美国、香港服务器

如何使用Nginx配置CF-Connecting-IP头?

10-13   来源:

 ### 方案 A:写在 nginx.conf 全局(推荐,全部站点生效)

 
宝塔 → Nginx → 配置修改,在`http{}`大括号里面添加下面代码
 
# Cloudflare 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;
 
# 核心:读取CF真实IP头
real_ip_header CF-Connecting-IP;
real_ip_recursive on;
 
### 方案 B:仅单个网站生效(站点配置文件)
 
宝塔→网站→对应站点→配置文件,在`server {}`内粘贴
 
 注意:只适合单站,多个网站要每个站点都加
 
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;
 
real_ip_header CF-Connecting-IP;
real_ip_recursive on;
 
## 二、配置后校验(必做)
 
1. 检测语法
 
nginx -t
 
输出 `test is successful` 才代表配置语法没问题。
2. 重载 Nginx 生效
 
nginx -s reload
 
## 三、日志格式优化(查看真实访客 IP)
 
宝塔 Nginx 默认日志要修改 log_format,才能记录 CF-Connecting-IP。
在 nginx.conf 的 http 块增加日志格式:
 
log_format cf_log "$remote_addr [$time_local] "$request" $status "$http_referer" "$http_user_agent" CF_IP:$http_cf_connecting_ip";
 
站点 server 里指定使用这个日志格式:
 
access_log /www/wwwlogs/你的域名.log cf_log;
 
然后访问网站,查看日志验证:
 
tail -f /www/wwwlogs/你的域名.log
 
## 四、配套 Nginx 防源 IP 直接访问(一起加上)
 
放在站点 server 配置:
 
# 拦截直接用源IP访问
server {
    listen 80 default_server;
    server_name _;
    return 444;
}

三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快

上一篇:配置CF-Connecting-IP头对网站性能有影响吗? 下一篇:如何隐藏源 普通IP?

美国GIA服务器三二互联版权所有 WWW.250.cc 2008-2015 All Rights Reserved
三二互联 - 专业的美国C3服务器、香港vps、抗DOOS流量清洗、云备份系统、网站加速系统、美国GIA服务器和香港云服务器产品提供商
三二互联24小时在线工单系统为您提供全面、专业、周到的技术支持与服务
咨询热线:400-679-9994(免长话费)