Featured image of post 使用Cloudflare CDN后,nginx如何获取网站访客的真实ip

使用Cloudflare CDN后,nginx如何获取网站访客的真实ip

查看网站日志中发现ip全是Cloudflare CDN的ip而不是访客的真实IP,于是有了这篇文章。

  1. 在nginx的配置文件目录下编辑cf.conf文件
1
vim cf.conf

内容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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/13;
set_real_ip_from 104.24.0.0/14;
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 2a06:98c0::/29;
set_real_ip_from 2c0f:f248::/32;
real_ip_header CF-Connecting-IP;
  1. 编辑nginx的配置文件

加入以下内容:

1
include cf.conf;
  1. 重启nginx即可
Licensed under CC BY-NC-SA 4.0
最后更新于 Apr 04, 2022 07:17 CST
comments powered by Disqus
Built with Hugo
主题 StackJimmy 设计