1 . 准备:一个nginx,两个tomcat
2 . 配置tomcat
tomcat1配置:
新增环境变量CATALINA_HOME_1
,修改tomcat1的bin目录下的的start.bat和catalina.bat,将其中的CANALINA_HOME
改为CATALINA_HOME_1
,然后打开conf目录下的server.xml配置文件,修改端口,一共三处修改
<Server port="8006" shutdown="SHUTDOWN">
<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
tomcat2配置:
新增环境变量CATALINA_HOME_2
,修改tomcat2的bin目录下的的start.bat和catalina.bat,将其中的CANALINA_HOME
改为CATALINA_HOME_2
,然后打开conf目录下的server.xml配置文件,修改端口,一共三处修改
<Server port="8007" shutdown="SHUTDOWN">
<Connector port="8082" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />
分别启动两个tomcat:
tomcat1启动信息如图:
tomcat2启动信息如图:
3 . 配置nginx
打开nginx.conf,修改如下
upstream localhost {
server localhost:8081 weight=1;
server localhost:8082 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://localhost;
#root html;
#index index.html index.htm;
}
启动nginx,运行 start nginx.exe,或者修改配置文件后重新载入nginx -s reload.
4 . 演示负载均衡
访问 http://localhost,结果如下:每刷新一次,就从tomcat1变成tomcat2或者从tomcat2变成tomcat1,只是因为上面负载均衡的权重配置的都是1,可以根据情况自己适当修改。