【问题标题】:How to identify realip from two proxies? (nginx)如何从两个代理中识别 realip? (nginx)
【发布时间】:2014-06-06 18:02:23
【问题描述】:

我有网站服务器,它有两个代理(squid,CF),它们使用不同的标头来获取真实的 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; // 如果来自代理一,则从 X-Forwarded-For 获取 ip

set_real_ip_from ip_from_proxy_two
real_ip_header X-CF-connecting-ip; // 如果来自代理二,则从 X-CF-connecting-ip 获取 ip

非常感谢!

【问题讨论】:

  • 提升一级并检查应用程序中的 http 标头怎么样?我看不出“真正”需要它的理由......
  • ...或降低一级,让您的代理 (CF) 使用 X-Forwarded-For support.cloudflare.com/hc/en-us/articles/…
  • 你找到解决方案了吗?

标签: nginx proxy


【解决方案1】:

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

您可以看到可以根据外部服务 IP 地址设置映射以定义定义要使用的 HTTP 标头的变量,然后使用该变量设置 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;

【讨论】:

猜你喜欢
  • 2017-09-27
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多