系统总是频繁更新,为了避免更新系统的时候领导看不到东西,打算用ngix做代理,后台部署两个tomcat做负载均衡,避免更新一台就无法使用系统的问题,这两天看了写资料,把几个关键点记录在这里以便备忘。
环境:jdk,1.7,tomcat7,nginx1.5.8; 基于64位的windows配置
第一步:更改tomcat三个端口,保证同一台机器上可以运行两个tomcat,更改的端口包括server port,两个connector port,xml配置参见下面,为了避免文件过大,删除了注释和无关的配置:
- <? xml version= '1.0' encoding= 'utf-8' ?>
- < Server port= "18005" shutdown= "SHUTDOWN" >
- < Listener className= "org.apache.catalina.core.AprLifecycleListener" SSLEngine= "on" />
- <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
- < Listener className= "org.apache.catalina.core.JasperListener" />
- <!-- Prevent memory leaks due to use of particular java/javax APIs-->
- < Listener className= "org.apache.catalina.core.JreMemoryLeakPreventionListener" />
- < Listener className= "org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
- < Listener className= "org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
- < GlobalNamingResources >
- < Resource name= "UserDatabase" auth= "Container"
- type= "org.apache.catalina.UserDatabase"
- description= "User database that can be updated and saved"
- factory= "org.apache.catalina.users.MemoryUserDatabaseFactory"
- pathname= "conf/tomcat-users.xml" />
- </ GlobalNamingResources >
- < Service name= "Catalina" >
- < Connector port= "8082" protocol= "HTTP/1.1"
- connectionTimeout= "20000"
- redirectPort= "8443" />
- < Connector port= "18009" protocol= "AJP/1.3" redirectPort= "8443" />
- 。。。。。
- </ Service >
- </ Server >
第二部,配置nginx(负载均衡):
http节点下增加如下配置:
- upstream localhost {
- #针对不同ip的用户请求分配固定的tomcat响应其请求。
- ip_hash;
- #配置tomcat服务器的ip:端口,处理请求权重
- server localhost:8080 weight=5;
- server localhost:8082 weight=5;
- }
http的节点下更改location/节点配置:
- location / {
- #root html;
- #index index.html index.htm;
- proxy_connect_timeout 3;
- proxy_send_timeout 30;
- proxy_read_timeout 30;
- proxy_pass http://localhost;
- }
配置完毕后,启动两个tomcat,再启动nginx,启动ngix方式:进入dos命令窗口,切换至nginx主目录,输入命令nginx.exe即可,
停止nginx可以使用nginx.exe -s stop
了解更详细的步骤参考下面两个链接:
http://ari.iteye.com
http://www.blogjava.net/tunaic/archive/2009/11/30/304212.html