哈特中尉's Blog

不会写代码的司机不是好厨师!

nginx+tomcat负载均衡

tomcat准备

本地成功启动两个tomcat
tomcatA 8080 webapps/cms/index.html文件的内容tomcatA
tomcatB 8081 webapps/cms/index.html文件的内容tomcatA

tomcatA http://localhost:8080/cms/index.html
tomcatB http://localhost:8081/cms/index.html

配置nginx

nginx.conf 配置内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
http{
upstream cmsCluster{
server localhost:8080 weight=1;
server localhost:8081 weight=1;
}
server{
listen 8090;
server_name localhost;
location /{
proxy_pass http://cmsCluster;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location = /50x.html {
root html;
}
}
}

nginx http://localhost:8090/cms/index.html

不停刷新页面,看内容变化。负载均衡完成。