1.在server节点上面增加以下节点:
引用
upstream backend{
ip_hash;
server 192.168.73.206:8080;
server 192.168.73.207:8080;
}
ip_hash;
server 192.168.73.206:8080;
server 192.168.73.207:8080;
}
2.找到:
引用
location / {
root html;
index index.html index.htm;
}
root html;
index index.html index.htm;
}
改成:
引用
location / {
proxy_pass http://backend;
proxy_redirect default;
proxy_connect_timeout 10;
}
proxy_pass http://backend;
proxy_redirect default;
proxy_connect_timeout 10;
}
3.找到:
引用
server {
listen 80;
server_name localhost;
listen 80;
server_name localhost;
改成:
引用
server {
listen 80;
server_name 192.168.73.208;
listen 80;
server_name 192.168.73.208;
另外:
1.如果要查看Nginx的调试级日志,需要在编译时加上--with-debug选项,然后再在配置文件中指定日志级别。
./configure --sbin-path=/usr/local/nginx/nginx \ --conf-path=/usr/local/nginx/nginx.conf \ --pid-path=/usr/local/nginx/nginx.pid \ --with-http_ssl_module \ --with-pcre=/usr/local/src/pcre-8.21 \ --with-zlib=/usr/local/src/zlib-1.2.8 \ --with-openssl=/usr/local/src/openssl-1.0.1c \ --with-debug
可以使用 debug_connection 指令只调试某些连接。
2.ip_hash均衡算法,只针对IP地址的前三段进行hash,所以在局域网中因为IP前三段都是一样的,所以都转发到同一台Tomcat。
引用
The first three octets of the client IPv4 address, or the entire IPv6 address, are used as a hashing key.
3.关于jvmRoute
http://tomcat.apache.org/tomcat-5.5-doc/config/engine.html
引用
Identifier which must be used in load balancing scenarios to enable session affinity. The identifier, which must be unique across all Tomcat 5 servers which participate in the cluster, will be appended to the generated session identifier, therefore allowing the front end proxy to always forward a particular session to the same Tomcat 5 instance.
http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html
引用
If you are using mod_jk, make sure that jvmRoute attribute is set at your Engine <Engine name="Catalina" jvmRoute="node01" > and that the jvmRoute attribute value matches your worker name in workers.properties
jvmRoute应该是Tomcat使用mod_jk与Apache集成实现粘性会话(sticky session)而出现的,它通过在session id中添加tomcat的jvmRoute来实现粘性会话。
Nginx也有类似的实现方式,如:
https://code.google.com/p/nginx-upstream-jvm-route/
在Nginx中如果使用ip_hash均衡算法,则不需要在Tomcat中配置jvmRoute。
其它算法还有(使用Cookie来实现粘性会话):
https://code.google.com/p/nginx-sticky-module/