从负载均衡器获取原始 IP
Getting Orgin IP From Load Balancer
有没有办法从 GCloud 的 HTTP 负载平衡中获取用户的原始 IP?我们目前仅使用网络负载平衡,并且需要转移到跨区域平衡器,尽管我们需要用户的 IP 以进行合规性和日志记录。
它是否传递了 header 或类似的东西?
谢谢~Z
好的,所以在挖掘 headers 和其他东西之后,我发现以下 header 正在为用户传递原始 IP 和你的 IP。
$_SERVER['HTTP_X_FORWARDED_FOR']
您需要用“,”将其拆分,然后取字符串的第一部分。这是用户 IP,由 Google Cloud HTTP Balancer 推送。
文档 (https://cloud.google.com/compute/docs/load-balancing/http/) 说这是 X-Forwarded-For header 的第一个 IP 地址。
X-Forwarded-For: <client IP(s)>, <global forwarding rule external IP>
如果您确定在 Google Cloud Balancing 后面没有 运行 任何其他代理(将额外的 IP 添加到 X-Forwarded-For),您可以获得倒数第二个 IP来自 X-Forwarded-For 作为直接客户端 IP。或者即使您有一些代理但知道将附加的额外 IP 的确切数量,您也可以将它们添加到帐户中。
来自https://cloud.google.com/compute/docs/load-balancing/http/#components:
X-Forwarded-For: <unverified IP(s)>, <immediate client IP>, <global forwarding rule external IP>, <proxies running in GCP> (requests only)
Only the <immediate client IP>
and <global forwarding rule external IP>
entries are provided by the load balancer. All other entries in
the list are passed along without verification.
位于直接客户端 IP 之前的 IP 可能是欺骗性 IP 或来自客户端代理的 IP。即使客户端欺骗 X-Forwarded-For header,负载均衡器仍然附加命中负载均衡器的实际 IP。
基于 HTTP_X_FORWARDED_FOR header,一个很好的 Nginx 规则来拆分 IP 链:
set $realip $remote_addr;
if ($http_x_forwarded_for ~ "^(\d+\.\d+\.\d+\.\d+)") {
set $realip ;
}
fastcgi_param REMOTE_ADDR $realip;
粘贴在include fastcgi_params; 指令后生效。
如果您使用的是 Cloudflare,您可以从 HTTP_CF_CONNECTING_IP.
获取原始客户端 IP
我找到了这篇文章
https://geko.cloud/forward-real-ip-to-a-nginx-behind-a-gcp-load-balancer/
您可以whitelist/ignore GCP 已知的 IP,例如注册负载均衡器所需的静态 IP
set_real_ip_from 36.129.221.25/32; // LB Public IP address
set_real_ip_from 130.211.0.0/22; // Private IP range for GCP Load Balancers
set_real_ip_from 35.191.0.0/16; //Private IP range for GCP Load Balancers
real_ip_header X-Forwarded-For;
real_ip_recursive on;
有没有办法从 GCloud 的 HTTP 负载平衡中获取用户的原始 IP?我们目前仅使用网络负载平衡,并且需要转移到跨区域平衡器,尽管我们需要用户的 IP 以进行合规性和日志记录。
它是否传递了 header 或类似的东西?
谢谢~Z
好的,所以在挖掘 headers 和其他东西之后,我发现以下 header 正在为用户传递原始 IP 和你的 IP。
$_SERVER['HTTP_X_FORWARDED_FOR']
您需要用“,”将其拆分,然后取字符串的第一部分。这是用户 IP,由 Google Cloud HTTP Balancer 推送。
文档 (https://cloud.google.com/compute/docs/load-balancing/http/) 说这是 X-Forwarded-For header 的第一个 IP 地址。
X-Forwarded-For: <client IP(s)>, <global forwarding rule external IP>
如果您确定在 Google Cloud Balancing 后面没有 运行 任何其他代理(将额外的 IP 添加到 X-Forwarded-For),您可以获得倒数第二个 IP来自 X-Forwarded-For 作为直接客户端 IP。或者即使您有一些代理但知道将附加的额外 IP 的确切数量,您也可以将它们添加到帐户中。
来自https://cloud.google.com/compute/docs/load-balancing/http/#components:
X-Forwarded-For: <unverified IP(s)>, <immediate client IP>, <global forwarding rule external IP>, <proxies running in GCP> (requests only)
Only the
<immediate client IP>
and<global forwarding rule external IP>
entries are provided by the load balancer. All other entries in the list are passed along without verification.
位于直接客户端 IP 之前的 IP 可能是欺骗性 IP 或来自客户端代理的 IP。即使客户端欺骗 X-Forwarded-For header,负载均衡器仍然附加命中负载均衡器的实际 IP。
基于 HTTP_X_FORWARDED_FOR header,一个很好的 Nginx 规则来拆分 IP 链:
set $realip $remote_addr;
if ($http_x_forwarded_for ~ "^(\d+\.\d+\.\d+\.\d+)") {
set $realip ;
}
fastcgi_param REMOTE_ADDR $realip;
粘贴在include fastcgi_params; 指令后生效。
如果您使用的是 Cloudflare,您可以从 HTTP_CF_CONNECTING_IP.
获取原始客户端 IP我找到了这篇文章 https://geko.cloud/forward-real-ip-to-a-nginx-behind-a-gcp-load-balancer/
您可以whitelist/ignore GCP 已知的 IP,例如注册负载均衡器所需的静态 IP
set_real_ip_from 36.129.221.25/32; // LB Public IP address
set_real_ip_from 130.211.0.0/22; // Private IP range for GCP Load Balancers
set_real_ip_from 35.191.0.0/16; //Private IP range for GCP Load Balancers
real_ip_header X-Forwarded-For;
real_ip_recursive on;