网络结构描述:
1个Nginx(前端)+2个tomcat(后端)
环境:
公司内网(网段:192.168.1.0/24),服务器也是分配的内网ip:192.168.1.4(暂定);后端两个tomcat:192.168.1.31/189
网站有session,所有Nginx启用ip_hash.
现象:
测试组用loadrunner模拟N多内网ip进行压测系统。发现,这些ip统一都转发到一个后端。
后经排查发现问题所在(结论最下面)。
1、请看官方解释:
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 or the entire IPv6-address of the client. IPv6 is supported for ip_hash since 1.3.2 or 1.2.2. 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. (简译:将客户端ip转化成C类网络地址,然后将该网络地址当作hash关键字,来保证这个客户端请求总是被转发到一台服务器上)
2、请看Nginx的ip hash算法(该段代码为转发。原文链接:http://www.cnblogs.com/xiaohuo/archive/2012/08/13/2636468.html):
for ( ;; ) {
for (i = 0; i < 3; i++) {