gpt4 book ai didi

nginx - 如何从两个代理中识别realip? (nginx)

转载 作者:行者123 更新时间:2023-12-04 18:28:44 28 4
gpt4 key购买 nike

我有网站服务器,它有两个代理(鱿鱼,CF),它们使用不同的 header 来获取真实的 ip。
我猜 nginx 命令

set_real_ip_from ;real_ip_header X-Forwarded-For;


在一个服务器块中只能使用一次。怎么设置两个?有什么办法可以正确地从两者中获取真实的 ip 吗?

set_real_ip_from ip_from_proxy_one
real_ip_header X-Forwarded-For; // if from proxy one, then get ip from X-Forwarded-For

set_real_ip_from ip_from_proxy_two
real_ip_header X-CF-connecting-ip; // if from proxy two, then get ip from X-CF-connecting-ip


非常感谢!

最佳答案

对于任何寻找解决方案的人,我想出了 this approach .

您可以看到可以根据外部服务 IP 地址设置映射以定义定义使用哪个 HTTP header 的变量,然后设置 real_ip_header使用那个变量。但以下不能用 因为变量没有被解析为 real_ip_header :

# DDoS protection service
geo $use_x_real_ip {
default 0;
186.2.160.0/24 1;
}

# Cloudflare
geo $use_x_cf_connecting_ip {
default 0;
103.21.244.0/22 1;
103.22.200.0/22 1;
103.31.4.0/22 1;
# all other Cloudflare's ranges ...
}

map "$use_x_real_ip:$use_x_cf_connecting_ip" $real_ip_header {
default 'X-Forwarded-For';
"1:0" 'X-Real-Ip';
"0:1" 'CF-Connecting-IP';
}

# here, the real IP header will be set to either X-Real-Ip or CF-Connecting-IP
real_ip_header $real_ip_header;

关于nginx - 如何从两个代理中识别realip? (nginx),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24088047/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com