This directive causes requests to be distributed between upstreams based on the IP-address of the client.
The key for the hash is the class-C network address of the client. This
method guarantees that the client request will always be transferred to
the same server. But if this server is considered inoperative, then the
request of this client will be transferred to another server. This
gives a high probability clients will always connect to the same server.
It is not possible to combine ip_hash and weight methods for
connection distribution. If one of the servers must be removed for some
time, you must mark that server as *down*.
由这段英文解说知道,
客户端只要来自同一网段的ip的request都会转发到相同的后端服务器上。这里的所谓的
class-C network这里作者并没有很详细地解释,我只能说,写这句话的人不懂网络。我个人的理解是:以ip地址的点分十进制格式的前3个字节进行hash。其实这不是真正意义上的ip address hash,而只是network address hash。真正的ip address hash方式有不?其实可以通过下面介绍的url_hash来实现。关键指令:hash $remote_addr;
不过这里有个前提,
$remote_addr必须是client的real ip address。为什么这里能够实现真正意义上的
ip address hash?很简单,就是这里整个ip address被当作一个字符串来对待,故只要ip地址(key)不同,hash必然也是不同的。