代理背后的 Nginx ip_hash (Cloudflare)

dig*_*_mb 3 nginx reverse-proxy cloudflare

我们使用 nginx 进行负载平衡,我们需要 ip_hash 来保证我们的服务器正常工作。

当我们开始使用 cloudflare 时,我们的大多数请求都针对一台服务器,因为似乎所有请求都仅由 cloudflare 的 IP 标识。我们想更好地平衡它,最好 ip_hash 使用 cloudflare 放在请求上的标头 CF-Connecting-IP。

任何人都知道如何做到这一点?

upstream backend {
    ip_hash;
    #proxy_next_upstream_timeout 30;
    server localhost:8080 max_fails=2 fail_timeout=180;
    server somethign:8080 ;
    server something2:8080;
}
Run Code Online (Sandbox Code Playgroud)

小智 6

Cloudflare 有这些 ip 范围,您可以使用 nginx 内置的real_ip 模块并正确设置标头 https://www.cloudflare.com/ips

set_real_ip_from   204.93.240.0/24;
real_ip_header     CF-Connecting-IP;
Run Code Online (Sandbox Code Playgroud)

https://support.cloudflare.com/hc/en-us/articles/200170706-Does-CloudFlare-have-an-IP-module-for-Nginx-