Load Balancing for nginx and tomcat in CentOS
Some time ago, leaders always looked at the system, but the system was always updated frequently. In order to prevent the leaders from seeing anything when updating the system, they planned to use ngix as the proxy and deploy two tomcat servers in the background for load balancing, to avoid the problem that the system cannot be used when updating the system, I read the documents and record several key points here for my memo.
Environment: jdk, 1.7, tomcat7, nginx1.5.8; 64-bit windows Configuration
Step 1: Change the three tomcat ports to ensure that two tomcat servers can run on the same machine. The changed ports include server ports and two ctor ports. For xml configuration, see the following. To avoid excessive file sizes, commented and irrelevant configurations are deleted:
[Html] view plaincopy
- <? Xmlversion = '1. 0' encoding = 'utf-8'?>
- <Serverport = "18005" shutdown = "SHUTDOWN">
- <ListenerclassName = "org. apache. catalina. core. AprLifecycleListener" SSLEngine = "on"/>
- <! -- Initializejasperpriortowebjavasareloaded. Documentationat/docs/jasper-howto.html -->
- <ListenerclassName = "org. apache. catalina. core. JasperListener"/>
- <! -- Preventmemoryleaksduetouseofularjava/javaxAPIs -->
- <ListenerclassName = "org. apache. catalina. core. JreMemoryLeakPreventionListener"/>
- <ListenerclassName = "org. apache. catalina. mbeans. GlobalResourcesLifecycleListener"/>
- <ListenerclassName = "org. apache. catalina. core. ThreadLocalLeakPreventionListener"/>
- <GlobalNamingResources>
- <Resourcename = "UserDatabase" auth = "Container"
- Type = "org. apache. catalina. UserDatabase"
- Description = "Userdatabasethatcanbeupdatedandsaved"
- Factory = "org. apache. catalina. users. MemoryUserDatabaseFactory"
- Pathname = "conf/tomcat-users.xml"/>
- </GlobalNamingResources>
- <Servicename = "Catalina">
- <Connectorport = "8082" protocol = "HTTP/1.1"
- ConnectionTimeout = "20000"
- RedirectPort = "8443" type = "regxph" text = "yourobjectname"/>
- <Connectorport = "18009" protocol = "AJP/1.3" redirectPort = "8443"/>
- .....
- </Service>
- </Server>
Second, configure nginx (Server Load balancer ):
Add the following configuration under the http node:
[Plain] view plaincopy
- Upstreamlocalhost {
- # Assign a fixed tomcat for user requests from different ip addresses to respond to their requests.
- Ip_hash;
- # Configure the tomcat server ip Address: Port and process the request weight
- Serverlocalhost: 8080 weight = 5;
- Serverlocalhost: 8082 weight = 5;
- }
Change location/node configuration under http nodes: [plain] view plaincopy
- Location /{
- # Roothtml;
- Define indexindex.htmlindex.htm;
- Proxy_connect_timeout3;
- Proxy_send_timeout30;
- Proxy_read_timeout30;
- Proxy_passhttp: // localhost;
- }
After the configuration is complete, start two tomcat servers, then start nginx, and enable ngix: Enter the DOS command window, switch to the nginxmain directory, and enter the nginx.exe command,
You can use nginx.exe-s stop to stop nginx.
For more detailed steps, see the following two links:
Http://ari.iteye.com
Http://www.blogjava.net/tunaic/archive/2009/11/30/304212.html