Load Balancer Configuration under Nginx + Tomcat windows

Source: Internet
Author: User
Tags nginx server

Why do I need to load balance the tomcat server?
The Tomcat server acts as a Web server with a number of concurrent numbers between 300-500, and if more than 500 concurrency occurs, Tomcat does not respond to new requests and severely affects the operation of the site. At the same time, if the traffic is very large, the number of threads in Tomcat is increasing. As a result, it takes up a lot of memory and when there is a memory overflow, you need to restart Tomcat to free up memory and block the site from running.
So it's necessary to load-balance tomcat. Currently can and Tomcat do load balance of the mainstream server is Apache, but Nginx due to many features, simple configuration and other advantages to become a lot of load balancing server preferred. Nginx concurrency can reach 50000, so theoretically and Tomcat to 1:100 of the ratio to configure, this side can be a good solution to the site concurrency bottleneck problem.

Second, the configuration of NGINX+TOMCA under Windows Load balancer:
First show me the environment: Nginx1.3.11 + Tomcat 6.0 (2), Win 7.
Here is the configuration process:
1, the Nginx-1.3.11.rar directly extracted to D: disk.
2, the Apache-tomcat-6.0.26.rar decompression to D: disk, since it is to do load balancing Tomcat copy two copies, respectively named Tomcat_1, Tomcat_2.
3. The project deployment to be released will be deployed to the WebApps under two Tomcat root directories, guaranteeing the same project name under two tomcat.
4, modify the tomcat_2 configuration file, the profile is located under/conf, the file name is Server.xml, modify the <server port= "8005" shutdown= "Shutdown" > for <server Port= "8006" shutdown= "shutdown", modify the

?
123 <Connectorport="8080 protocol="HTTP/1.1"           connectionTimeout="20000"          redirectPort="8443"/>

For

?
123 <Connectorport="8088 protocol="HTTP/1.1"         connectionTimeout="20000"        redirectPort="8443"/>

?
1 <Connectorport="8009" protocol="AJP/1.3" redirectPort="8443"/>

(AJP is a protocol that Tomcat can use for a server cluster or to find more detailed AJP data in csdn) for

?
1 <Connectorport="7009" protocol="AJP/1.3" redirectPort="8443"/>

5, modify the Nginx configuration file nginx.conf, the file is located in the Nginx root directory under the/conf directory
A, on #gzip, after adding the following configuration:

?
12345 upstream localhost {    server localhost:8080 weight=5;    server localhost:8080 weight=5;    ip_hash;}


Where Server localhost:8080 is the startup address of tomcat_1, server localhsot:8088 is the tomcat_2 start address, Ip_hash is used for session synchronization.
B, modify

?
1234 location / {     root html;     index index.html index.htm; }

Switch

?
123456789101112131415161718 location / {       root   html;       index  index.html index.htm;       proxy_pass   http://localhost;       proxy_redirect    off;       proxy_set_header   Host $host;       proxy_set_header   X-Real-IP $remote_addr;       proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;        client_max_body_size   10m;       client_body_buffer_size   128k;       proxy_connect_timeout   90;       proxy_send_timeout   90;       proxy_read_timeout   90;       proxy_buffer_size   4k;       proxy_buffers   4 32k;       proxy_busy_buffers_size   64k;       proxy_temp_file_write_size  64k;           }

Where the Proxy_pass parameter corresponds to upstream localhost{}.

After the above steps, the load Balancer configuration is completed, the following to start Tomcat_1, tomcat_2, and then double-click the nginx root directory Nginx.exe file or use start Nginx boot (shutdown is: nginx-s stop), open the browser, Enter Address: http://localhost will be able to see the first page of Tomcat.

Learn more about Nginx configuration: http://www.howtocn.org/doku.php

---------------------------------------------------------------------------------------

The backend is two tomcat servers, we are referred to as Node1 and Node2, and the access addresses are http://192.168.1.2:8080 and http://192.168.1.4:8080 respectively.

The front end uses Nginx to load balance the two services.

First download Apahce tomcat7 and deploy on 192.168.1.4 and 192.168.1.2 (you can modify the index.jsp of the files deployed on both servers to differentiate Node1 and Node2 respectively)

Node1:

Node2:

Download Nginx Server (currently stable version is 1.4.4), unzip and then double-click Nginx.exe boot

The nginx.conf file in the Conf directory is the Nginx configuration file, where we configure load balancing

The configuration file is given below,

HTTP {
Include Mime.types;
Default_type Application/octet-stream;

Sendfile on;

Keepalive_timeout 65;


Upstream localhost{
#这里指定多个源服务器, IP: Port, 80-port can be written without writing
Server 192.168.1.2:8080;
Server 192.168.1.4:8080;
}

server {
Listen 80;
server_name localhost;

Location/{

#启动代理

Proxy_pass http://localhost;
}

}

}

One of the upstream localhost is not in the original configuration file, need to add.

Make sure that the Node1 and Node2 two nodes are available, and then start the Nginx access http://localhost nginx will load balance on Node1 and node2. NODE1 or NODE2 are randomly displayed on the page

Load Balancer Configuration under Nginx + Tomcat windows

Related Article

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: info-contact@alibabacloud.com 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.