代理后面的Nginx ip_hash(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; } 

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; 

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

作为此线程中build议的方法的替代方法,可以使用hash指令从任何variables构build哈希表,在本例中$http_cf_connecting_ip头。

 upstream backend { hash $http_cf_connecting_ip; }