GCP CLB:如何在 Nginx.conf 中获取 X-Forwarded-For IP?

GCP CLB: how to get X-Forwarded-For IP in Nginx.conf?

根据 GCP 文档,https://cloud.google.com/load-balancing/docs/https#target-proxiesX-Forwarded-For 包含 多个 IP,例如 1.2.3.4, 5.6.7.8,其中 1.2.3.4 是真实的客户端IP,5.6.7.8为CLB IP。

如何在 nginx.conf 中获得 1.2.3.4?我想和geoip一起使用。

您可以使用 real IP module 分解 X-Forwarded-For 并为 $remote_addr 变量设置适当的值。

例如:

set_real_ip_from  5.6.7.8;
real_ip_header    X-Forwarded-For;
real_ip_recursive on;

$remote_addr 的值由 5.6.7.8 更改为 1.2.3.4

在许多 Nginx 发行版中,这个模块已经存在。使用 nginx -V 确认存在哪些模块。