启用Cloudflare CDN后获取访客真实IP

May 1, 2022 by Clyde

今晚玩尼尔玩到很晚 看一下博客acces-log发现都是cf的回源Ip。

back ip
通过国外的cloudflare进行加速(真的很慢,但是ipv6就不一样了)
然后看了一下官方文档发现ip的访客信息存在于X-Forwarded-For 和 CF-Connecting-IP 标头中,那么我又行了!

获取官方回源ip

官网提供数据: cloudflare-ips

开始写入网站配置

server
{
listen 443 ssl http2;
listen [::]:80;
server_name kleos.im;
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; 
#第二种方法
#real_ip_header    X-Forwarded-For;
#real_ip_recursive on;

注意set_real_ip写入的是第一步中获取到的cloudflare回源ip

重启nginx服务

service nginx restart

到此问题解决了!
true

2B小姐姐真香

2 comments
  1. 松鼠

    真不错

    松鼠 回复
  2. 卢本伟

    卢本伟牛逼!!!

    卢本伟 回复
如需评论,请填写表单。

提交前先勾选

© 2024 | The Asuna Blog From | X-ways theme