nginx配置CloudFlare节点ip的方法

作者:袖梨 2022-06-30

系统:centos 5.x

1.安装ngx_http_realip_module

只需要在编译nginx的时候,加上--with-http_realip_module这项参数就可以了.

2.配置CloudFlare节点ip

代码如下 复制代码

vi /etc/nginx/nginx.conf //写在http区域里
ipv4:
set_real_ip_from 199.27.128.0/21;
set_real_ip_from 173.245.48.0/20;
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 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
ipv6:
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;
real_ip_header CF-Connecting-IP;

检查是否有错误,如果没有错误就可以重启nginx了.

ps:
如果CloudFlare节点ip变更,大家可以去下面地址获取最新的节点ip.

https://www.*cl*oudfla*re.com/ips

好了,这样就不怕使用CloudFlare的cdn,无法获取到客户端的真实ip了.

另外还有一个办法

1,设置nginx转发时加上http header


2,在node中取出新加的header

nginx配置

代码如下 复制代码

location /svc/ {
proxy_pass http://192.16*8**.1.111:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}


以上增加了3个http header,其中X-Real-IP代表真实IP。

node节点的代码:

代码如下 复制代码

var real_ip = req.get("X-Real-IP") || req.get("X-Forwarded-For") || req.ip;

相关文章

精彩推荐