前不久写了一篇如何在使用 Cloudflare 加速时获取用户的真实 IP


由于本博客域名已经备案,于是我使用的是 Cloudflare 国内合作商——百度云加速,为网站提供加速服务,整体使用感觉还不错。

百度云加速免费版套餐每天 10G 流量,完全够使用,并且支持 cname 接入,很方便。不过仍然存在着和使用 Cloudflare 一样的问题,即网站的访问日志中都是百度云加速的 IP 地址,并不是客户的真实 IP 地址,这里给出解决办法。

解决办法

服务器使用的是宝塔面板编译安装的 nginx,默认编译了 gx_http_realip_module 模块,所以只需要到 /www/server/nginx/conf 目录下新建一个名为 baidusu.conf的文件,并将以下内容写入:

set_real_ip_from 119.188.9.0/24;
set_real_ip_from 42.236.93.0/24;
set_real_ip_from 220.170.185.0/24;
set_real_ip_from 115.231.186.0/25;
set_real_ip_from 183.232.51.0/24;
set_real_ip_from 61.182.137.0/25;
set_real_ip_from 112.25.90.0/24;
set_real_ip_from 59.51.81.128/26;
set_real_ip_from 222.216.190.0/24;
set_real_ip_from 122.190.2.0/24;
set_real_ip_from 125.39.239.0/24;
set_real_ip_from 183.61.177.0/24;
set_real_ip_from 183.61.236.0/24;
set_real_ip_from 124.95.168.128/26;
set_real_ip_from 124.95.191.0/24;
set_real_ip_from 58.211.2.0/24;
set_real_ip_from 117.34.13.0/24;
set_real_ip_from 150.138.150.0/24;
set_real_ip_from 150.138.149.128/25;
set_real_ip_from 157.255.25.0/24;
set_real_ip_from 113.207.101.0/25;
set_real_ip_from 111.32.135.0/25;
set_real_ip_from 42.81.6.0/25;

real_ip_header X-Forwarded-For;

然后到 nginx 的配置文件中,在 http 模块下添加一行 include baidusu.conf 即可。

最后修改:2020 年 11 月 07 日
如果觉得我的文章对你有用,请随意赞赏