可说欢喜
DevOps

宝塔面板网站开启CloudFlare 后获取用户真实IP

by 温柔, 2020-07-30


之前就有这个问题了但是搜索了一下都是又这又那的有些一块配置就感觉不对头后面就只是一直没有搞,今天调试评论回复插件的时候看日志感觉不爽就想给解决掉。

网上搜索了一大堆宝塔官方论坛找了半天没一个给出明确回复的,不是防火墙插件开启cdn 就是改程序IP获取方式要么回复就是加QQ然后就没了?还好有个帖子回复还是比较靠谱的给了一个 cloudflare 官方文档
无奈开始看cf的官方文档跟着文档配置走。

配置如下:

bt-nginx-cf.png

在路径/www/server/nginx/conf 目录新建cf.conf文件填入内容如下:

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;

nginx-cf-conf.png

然后宝塔nginx管理里面 重载配置

bt-nginx-reload.png

刷新博客网页看了下日志已经完美显示用户真实IP,还是CloudFlare靠谱。

typechoCloudflare宝塔
温柔

作者: 温柔

1 条评论
    天线宝宝 回复
    天线宝宝2023-06-11 16:07

    ip有更新了:https://www.cloudflare.com/zh-cn/ips/

2024 © typecho & elise