下面说明详细的配置步骤:
(1)nginx中配置nginx.conf文件内容:
upstream myserver{
server 192.168.1.10:80 weight=3 max_fails=3 fail_timeout=20s;
server 192.168.1.20:80 weight=1 max_fails=3 fail_timeout=20s;
server 192.168.1.30:80 weight=4 max_fails=3 fail_timeout=20s;
}
server{
listen 80;
server_name www.xxx.com 192.168.1.100;
index index.htm index.html
root /web/root;
location / {
proxy_pass http://myserver;
proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
include /usr/local/nginx/conf/proxy.conf;
}
(2) tomcat 的配置(tomcat 7,jdk1.7):
首先需要将tomcat把session写至redis的jar包(3个)放至tomcat的lib目录中,虽然需要3个,但我从官网下载的源码使用gradle编译后生成了3个,我都放到lib里了,
jedis-2.5.2.jar commons-pool2-2.2.jar tomcat-redis-session-manager-master-2.0.0.jar
tomcat-redis-session-manager-master-2.0.0-sources.jar tomcat-redis-session-manager-master-2.0.0-javadoc.jar
(3) 修改tomcat中context.xml中的内容:
<Context>
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60" />
</Context>
通过上面3步即可完成nginx反向代理的tomcat负载均衡,这样的负载均衡策略,有一大优势:就是session存放在redis中,其中任意一个tomcat挂掉,不会影响正常的用户访问。用户也不需要重新获取session信息.