Nginx配置多个Tomcat实现负载均衡

1.下载Nginx:http://nginx.org/en/download.html

   例如:1.12.2

nginx/Windows-1.12.2

2.启动Nginx两种方式

(1)解压文件,运行nginx.exe

(2)cmd打开nginx解压文件路径,执行start nginx命令(重载:nginx -s reload,退出:nginx -s quit)

3.浏览器访问:http://localhost,显示Welcome to nginx欢迎页面,则启动成功

4.修改nginx.conf配置文件分发到多个Tomcat

(1)server标签外添加:

upstream tomcats {  
    ip_hash;
    server localhost:82 weight=2;
    server localhost:8080 weight=1;		
}

        PS:localhost:82 和 localhost:8080 为已启动的两个应用,weight设置越高,代表权重越大,例如:有3个请求,有2个会发送到 localhost:82 应用上,有1个会发送到 localhost:8080 应用上。配置ip_hash可解决session共享问题,只会请求localhost:82上的session

(2)server标签内修改:

location / {
    proxy_pass http://tomcats;
    proxy_connect_timeout 36000s;
    proxy_send_timeout 36000s;
    proxy_read_timeout 36000s;
}

        PS:proxy_connect_timeout 为连接应用服务器的超时时间,单位为秒

                proxy_send_timeout 为发送请求到应用服务器的超时时间,单位为秒

                proxy_read_timeout 为等待应用服务器响应的超时时间,单位为秒

猜你喜欢

转载自blog.csdn.net/rexueqingchun/article/details/79738270