Specifies that a group should use a load balancing method where requests are distributed between servers based on client IP addresses. The first three octets of the client IPv4 address, or the entire IPv6 address, are used as a hashing key. The method ensures that requests from the same client will always be passed to the same server except when this server is unavailable. In the latter case client requests will be passed to another server. Most probably, it will always be the same server as well.
我们翻译过来的意思就是ip_hash用于指定组应使用负载平衡方法,其中请求根据客户端 IP 地址在服务器之间分配。客户端 IPv4 地址或整个 IPv6 地址的前三个八位字节用作散列密钥。该方法确保来自同一客户端的请求始终会传递到同一服务器,除非该服务器不可用。在后一种情况下,客户端请求将被传递到另一台服务器。最有可能的是,它也将始终是同一台服务器。
注意 1:从版本 1.3.2 和 1.2.2 开始支持 IPv6 地址。
如果需要暂时删除其中一台服务器,则应使用 down 参数对其进行标记,客户端请求才会被下一个服务器接收和处理。
注意2:在版本 1.3.1 和 1.2.2 之前, ip_hash 和权重配置不能一起使用。
使用示例
ip_hash 的配置示例如下:
upstream backend { ip_hash;
server backend1.example.com; server backend2.example.com; server backend3.example.com down; server backend4.example.com; }