Linux+nginx+tomcat load balancing by reverse proxy __linux

Source: Internet
Author: User
Tags openssl nginx server tomcat server

Recently looking at a book on Javaweb implementation of the Web site distributed architecture, it mentions that through load balancing, you can avoid issues such as server paralysis caused by massive access or the inability of users to access due to a single server failure. There are many ways to achieve load balancing, such as: F5 server or software, such as LVS (Linux virtual Server) or Nginx. I chose to use Nginx to practice load balancing.

Nginx Introduction

Nginx is the way to achieve load balancing by reverse proxy, what is reverse proxy. Citing a paragraph of explanations found on Baidu, I think it is very appropriate:

"We've all had this experience, call 10086 customer service phone, may be a region of 10086 customer service There are a few or dozens of, you never need to care about the phone that the head is which one, called what, male, or female, beautiful or handsome, you do not care, Your concern is that you can not get professional answers to your questions, you only need to dial 10086 of the switchboard number, the phone will always have someone to answer you, but sometimes slow and sometimes fast. So here's the 10086 switchboard number is what we call the reverse proxy. The customer does not know who is actually providing the service. ”

For example, when you enter www.baidu.com, you do not know what you visit the specific IP is (of course, you do not need to know), www.baidu.com is equivalent to a reverse proxy server, there are many servers behind it, it will pass a certain algorithm, specify a " From your nearest server, send your request to it. This server cluster can greatly reduce the pressure on a single server.

Environment Selection and construction:

Since the company's projects were run on Linux, I also chose to operate on Linux systems.

The Web project selects the MAVEN project based on the SSM framework;

Because is the simulation, I did not build two servers, but in the same server, set up two Tomcat server (how to build two Tomcat server, can refer to: Click to open the link);

The installation of Nginx is relatively complex because it relies on many other environments:

1, because Nginx and other software depends on the need to compile and install, so you need to configure the C + + compilation environment: Yum install gcc gcc-c++

2, download Openssl:wget http://www.openssl.org/source/openssl-fips-2.0.9.tar.gz

3, download Zlib:wget http://zlib.net/zlib-1.2.8.tar.gz

4, download Pcre:wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz

There are similar ways to install these software, as an example of installing zlib:

1, Tar zxvf pcre-8.36.tar.gz

2. CD pcre-8.36

3./configure && make && make install

After installation, go to Nginx/sbin run Nginx:


Nginx Server default running port is 80, if your 80 port is occupied by other programs, you can go to nginx/conf under Edit nginx.conf related network configuration:

Listen corresponds to the nginx current listening port (after configuration, be sure to remember to restart the Nginx service under Nginx/sbin:./nginx-s Reload)


At this point, the whole LINUX+NGINX+TOMCAT environment is completed.

Configure reverse proxy

Entering the corresponding IP and port on the browser appears as follows, proving that Nginx has been successfully deployed:


If you want to use Nginx to delegate access to a Web project on the server that corresponds to another port, you need to nginx.conf the appropriate configuration:
Set up IP that requires proxy access in Proxy_pass, save exit (Wq), reboot the server.

After refreshing the page, it is true that the Nginx server jumps to the IP corresponding item just set itself, but the picture, JS, CSS style can not load properly.


Baidu only found that if you need to access the resource files in Nginx, you also need to configure the following in nginx.conf:


After reboot:


This way, the Nginx proxy access to a single server is OK.

achieve load balancing of two machines

In the same way, it is configured under nginx.conf:

To configure a server cluster first:

The Nginx is then directed to this server cluster:


After restarting the Nginx, refresh a few more times, you will see this effect:



Nginx will repeatedly switch between two different servers, which enables the simplest load balancing. Of course, load balance also need to match the algorithm, and later have time to continue to study it.


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.