前言

我使用的是 LNMP,Typecho 博客系统,宝塔面板,和 Cloudflare CDN。

方法一 Typecho 独特设置方法

在根目录的config.inc.php后面添加代码
/** 获取用户真实IP */
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) 
{
    $list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
    $_SERVER['REMOTE_ADDR'] = $list[0];
 }

方法二 Nginx

在建站根目录的server/nginx/conf下新建一个cf.conf的文件

image.png

并添加内容如下:

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 设置

配置修改

image.png

在18行的位置加入include cf.conf;

保存,然后点服务重载配置

image.png

版权申明

抄袭作品:https://doge.uk/coding/cf-cdn-real-ip.htmlhttps://www.xde.io/devops/bt-cloudflare-ip.html
本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权