We use nginx for our load balancing, and we need ip_hash to guarantee our server work fine.

When we started using cloudflare, most our requests go for one server as it seems all of them are identified only by cloudflare's IP. We would like to balance it better, and it would be good that the ip_hash used the header CF-Connecting-IP that cloudflare puts on requests.

Anyone knows how to do this?

upstream backend {
    #proxy_next_upstream_timeout 30;
    server localhost:8080 max_fails=2 fail_timeout=180;
    server somethign:8080 ;
    server something2:8080;

2 Answers 2


Cloudflare has these ip ranges, you can use nginx built-in real_ip module and setup headers properly https://www.cloudflare.com/ips

real_ip_header     CF-Connecting-IP;



As alternative to methods suggested in this thread hash directive can be used to build hash table from any variable, in our case $http_cf_connecting_ip header.

upstream backend {
  hash $http_cf_connecting_ip;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.