Tomcat + Nginx cluster and load balancing

Source: Internet
Author: User
: This article mainly introduces Tomcat + Nginx clusters and server load balancer. For more information about PHP tutorials, see. Reprinted please indicate the source:

Today, we will work with you to achieve Tomcat and nginx clusters and load balancing.

I. configure a port for a Tomcat cluster

Note: This port is used to start two tomcat servers on the same machine. you need to modify the port to avoid duplicates. if only one tomcat server is deployed on one machine, the port can be the same.

Serial Number




Cluster worker Er










1. Tomcat cluster configuration

Modify in server. xml:


Among them, jvmRoute = "jvm1" has different names in different tomcat.

In Add the following content to the node for Session replication and sharing:

Port = "5001" autoBind = "100" selectorTimeout = "5000" maxThreads = "6"/>

Note: On the same machine: worker Er node port: port ="5001"It must be different.

2. modify tomcat's web. xml

You must modify the web. xml configuration parameters of tomcat to set the synchronous session replication.

Index. jsp
 (This code can be added in the last row to implement the synchronous session replication function)

II. optimization:

1. memory optimization configuration 1) Windows:
set JAVA_OPTS = " -server -Xms512M -Xmx2048M -XX:MaxNewSize=256M -XX:PermSize=256M -XX:MaxPermSize=512M -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails  -XX:+PrintGCTimeStamps -Djava.awt.headless=true"  
2) Linux
JAVA_OPTS = " -server -Xms512M -Xmx2048M -XX:MaxNewSize=256M -XX:PermSize=256M -XX:MaxPermSize=512M -XX:+UseConcMarkSweepGC  -XX:+PrintGCDetails  -XX:+PrintGCTimeStamps -Djava.awt.headless=true" 
2. Other optimized configurations: 1) add the following configurations: 1
2) or



Note: maxThreads = "500" is ignored when executor is used and associated.

3) Several attributes:
  • MaxThreads

Tomcat uses a thread to process each received request. This value indicates the maximum number of threads that Tomcat can create. The default value is 200. The performance and memory size can be adjusted according to the time range of the machine, generally between 400 and 500. The maximum value is about 800.

  • AcceptCount

Specify the number of threads that can be used to process requests, and the number of requests that can be placed in the processing queue. requests that exceed this number will not be processed. The default value is 10.

  • MinSpareThreads

The number of threads created during Tomcat initialization. The default value is 4.

  • ConnnectionTimeout

Network connection timeout. the default value is 20000. unit: milliseconds. If it is set to 0, it indicates that the request never times out. this setting has potential risks. Generally, it can be set to 30000 Ms. (This system is set to 60000 because it has a long time-out period with the background system interface)

  • EnableLookups

Whether to reverse query the domain name. The default value is true. Set to false to improve processing capability.

  • Executor node

Indicates the thread pool shared between Tomcat components.

  • MaxIdleTime

The number of milliseconds before idle threads are disabled. The default value is 60000.

III. Nginx + Tomcat server load balancer configuration

Add the following code to the configuration file/usr/local/nginx/conf/nginx. conf:

  upstream  tomcat {        server  weight=1;        server  weight=1;  }
The tomcat name is random. pay attention to the IP address and port.
Modify the location on the Server node:
location / {        root   html;        index  index.html index.htm;    }
location  / {   #root   html;   #index  index.html index.htm;           proxy_pass  http://tomcat6;}

Note that the tomcat name in http: // tomcat is the same as the preceding configuration.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces Tomcat + Nginx clusters and server load balancer, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.