Nginx + Tomcat Reverse proxy load Balancing cluster Deployment Guide

Source: Internet
Author: User
Tags nginx server nginx reverse proxy nginx load balancing automake

Reprint Please specify source: http://blog.csdn.net/smartbetter/article/details/53535435

Nginx is a server software, is also a high-performance HTTP and reverse proxy server, but also a proxy mail server. In other words, we can publish the website on Nginx, can achieve load balance (improve the response efficiency, avoid the server crashes), but also can be used as mail server to send and receive mail and other functions. And the most common is to use Nginx to achieve load balancing.

Performance comparison of Nginx with other servers:

The Tomcat server is for the Java language and is a heavyweight server, and Nginx is a lightweight server. Apache Server stable, open source, cross-platform, but the Apache server does not support high concurrency, nginx can support processing millions TCP connection, more than 100,000 of concurrent connections, and is a good cross-platform server.
Nginx main advantages can be achieved high concurrency, simple deployment, low memory consumption, low cost, the main disadvantage of rewrite function is not strong enough, the module does not have more Apache.

This article is mainly about Nginx + Tomcat reverse proxy and load balancing deployment, to popular and practical-oriented. This article does not have much to do with each section and can be studied separately according to the requirements.

Come down and look at the process of Nginx reverse proxy:

Nginx Load Balancing process (will automatically select a server with less pressure to access):

It can be seen that load balancing is implemented by the principle of reverse proxy, so it is also called the reverse proxy load balancer . So we'll deploy load balancing, and then the reverse proxy will be there.

In general, the implementation of load balancing method is divided into software implementation and hardware implementation of two, hardware implementation of the efficiency is very high, but the corresponding cost is very high. Software runs less efficiently than hardware, but costs are relatively low. The use of Nginx server to achieve load balancing, it is through the software to achieve load balancing, and nginx itself supports high concurrency and so on. Therefore, the use of Nginx server load balancing, can greatly save the cost of enterprises, and because Nginx is a server software, its execution efficiency is very high. The purpose of this article is to help you use Nginx to achieve load balancing.

The core of load balancing is to set up a server cluster, then the user first access to a third-party proxy server (here we choose Nginx), and then by the proxy Server select a cluster server, and then the request into the selected server (here we choose Tomcat).

For example, through the reverse proxy we achieve the following load balancing, here we assume four servers public network IP, a proxy server, three servers to do load balancing:

Well, the overall architecture has been basically clear, down to the concrete implementation of the following:

Tip: We basically use SSH to do related operations, under Windows can try to install Putty,mac directly using the system comes with the terminal tools.

Build Nginx server under 1.Linux

We build the Nginx server on the 192.168.2.20 server:

1) Download Nginx http://nginx.org/

2) upload the server Nginx installation package

$ scp ~/Downloads/nginx-1.10.2.tar.gz [email protected]:/usr/local

3) Install Nginx

$ ssh [email protected]                          //SSH连接# yum -y install gcc gcc-c++ autoconf automake   //gcc、gcc-c++的库文件# yum install -y pcre pcre-devel                 //安装Nginx依赖包# yum install -y zlib zlib-devel

Note:-y indicates that all yes,autoconf are encountered to determine the automatic configuration, and Automake represents automatic compilation.

# cd /usr/local# tar -zxvf nginx-1.10.2.tar.gz                 //解压缩# cd nginx-1.10.2                               //切换到该目录下# ./configure                                   //配置# make# make install                                  //安装

Verify that the installation is complete:

# cd /usr/local# ls                                            //如果存在nginx文件夹,则安装成功

After the above installation steps and directory settings, Nginx startup program is/usr/local/nginx/sbin/nginx, the default configuration file is/usr/local/nginx/conf/ Nginx.conf, but it is not recommended to edit nginx.conf directly, generally we choose to create a new configuration file, and then modify the port, reverse proxy path, and so on in the newly created configuration file.

2.Nginx start, stop, signal control

1) Start Nginx server (format: nginx executable file-C nginx configuration file):

# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

2) Stop Nginx server:

To stop the Nginx server first to query the Nginx main process number (master processes), if the query here to get 1060 (easy to use the following demonstration):

# ps -ef|grep nginx

To see how to stop Nginx server, there are three ways to stop Nginx:

Calmly stop:

# ps -ef|grep nginx    //查看Nginx的主进程号(master process),假设此处查询得到1060# kill -quit 1060

Quick STOP:

# kill -term 1060

Force stop:

# pkill -9 nginx

3) Restart the Nginx server:

When we modify the Nginx configuration file, a reboot is required to take effect. We also need to verify the correctness of the configuration file before restarting, and then restart the operation:

# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf      //验证# /usr/local/nginx/sbin/nginx -s reload                                   //重启

4) Smooth Upgrade Nginx server

A smooth upgrade does not stop the processes that are running, and these processes continue to process requests, but no longer accept new requests, which stop after the old processes have processed the requests that are still being processed. During this smooth upgrade process, the newly opened process is processed. This is the smooth upgrade.

# /usr/local/nginx/sbin/nginx -v             //查看当前版本

The following is a smooth upgrade:

# cd /usr/local# tar -zxvf nginx-1.11.6.tar.gz              //解压缩 新版本Nginx# cd nginx-1.11.6                            //切换到该目录下# ./configure                                //配置# make# cd /usr/local/nginx/sbin                   //打开 旧版本Nginx可执行文件位置# cp nginx nginx.old                  //备份 旧版本Nginx可执行文件,防止升级错误而无法恢复# cp -rfp /usr/local/nginx-1.11.6/objs/nginx /usr/local/nginx/sbin     //复制新版本可执行文件到旧版本处# rm -f /usr/local/nginx-1.11.6.tar.gz       //善后工作 删除压缩文件# rm -rf /usr/local/nginx-1.11.6             //删除文件夹

At this point, the Nginx server smooth upgrade successfully.

Implementation of load Balancing in 3.Nginx

We still connect SSH first, and then do the following (generally not recommended to modify the default master profile nginx.conf, so we create a new load Balancer profile fzjh.conf, to ensure that the server security, as follows):

# cd /usr/local/nginx/conf# touch fzjh.conf# vi fzjh.conf    //用vi编辑器打开文件,然后按键盘的i

Note: In the VI editor, the keyboard presses I into the insert state and press ESC to exit the insert state.
Then enter the following configuration code (the comment section opens on demand):

#设置低权限用户, set for securityUser nobody;#工作衍生进程数Worker_processes4;#设置错误文件存放路径#error_log Logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#设置pid存放路径 (PID is an important document in the control system)#pid logs/nginx.pid;#设置最大连接数events{worker_connections1024x768;} http{#主要是用于设置一组可以在proxy_pass和fastcgi_pass指令中使用额代理服务器, the default load balancing method is pollingUpstream Tomcat_client {#设置同一个cookie的两次/Multiple requests, the same server is requestedIp_hash;#weight权重, default 1, the larger the weight, the greater the access probability, the backup standby server, the server all crashes after the boot        Server 192.168. 2.:8080weight=5;Server 192.168. 2.:8080weight=5;Server 192.168. 2. at:8080weight=5Backup }#开启gzip压缩, when turned on, access to the webpage will automatically compress    #gzip on;    #指定服务器的名称和参数    Server{Listen the; server_name test.nginxtest.net;#设置字符        #charset koi8-r;        #location/Refer to the root directory for load BalancingLocation/{Proxy_pass http://tomcat_client;Proxy_redirectdefault;#设置代理Proxy_set_header Host $host;        Proxy_set_header X-real-ip $remote _addr; }    }}

Press ESC when the input is complete, and then enter:

# :wq!

You can save and exit the load Balancer configuration file, and we'll load our configuration file:

# /usr/local/nginx/sbin/nginx                                           //启动Nginx# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/fzjh.conf        //加载配置文件

If there is a port occupancy, you can kill the Nginx program using the following command:

# killall -9 nginx

At this point, Nginx Server deployment is complete. Below we start deploying the three servers that are actually providing data under load balancing.

Installing the JDK under 4.Linux

We installed the JDK in 192.168.2.21, 192.168.2.22, 192.168.2.23 and the three servers, taking 192.168.2.21 as an example:

1) Download JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html

2) Upload the server JDK installation package

$ scp ~/Downloads/jdk-8u112-linux-x64.tar.gz [email protected]:/usr/local

3) Installing the JDK

$ ssh [email protected]                 //SSH连接# cd /usr/local# tar -zxvf jdk-8u112-linux-x64.tar.gz  //解压缩# mv jdk1.8.0_112/ jdk                  //将jdk1.8.0_112重命名为jdk# vi /etc/profile                       //用vi编辑器打开文件,然后按键盘的i

Down we move the cursor to the last side, two carriage return lines, and then add the following code to configure the Java environment variables:

JAVA_HOME="/usr/local/jdk"CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"PATH=".:$PATH:$JAVA_HOME/bin"CATALINA_HOME="/usr/local/tomcat"export JAVA_HOME CATALINA_HOME

Press ESC when the input is complete, and then enter:

# :wq!

You can save and exit. Now that we want it to take effect immediately, we need to continue entering the command:

# source /etc/profile

At this point the profile file is updated, and the environment variable is configured successfully. Below we verify that the JDK installation configuration is successful:

# java -version

If the Java version number is returned, the installation succeeds.

Build Tomcat server under 5.Linux

We build Tomcat servers in 192.168.2.21, 192.168.2.22, 192.168.2.23, and 192.168.2.21 as an example:

1) Download Tomcat http://tomcat.apache.org/

2) Upload the server Tomcat installation package

$ scp ~/Downloads/apache-tomcat-8.5.9.tar.gz [email protected]:/usr/local

3) Install Tomcat

$ ssh [email protected]                 //SSH连接# cd /usr/local# tar -zxvf apache-tomcat-8.5.9.tar.gz  //解压缩# mv apache-tomcat-8.5.9/ tomcat        //将apache-tomcat-8.5.9重命名为tomcat

4) Set the Tomcat Web file directory

The path to the Tomcat Default Web file is/usr/local/tomcat/webapps/root, but generally we don't store it here, we create a new Data/wwwroot directory at the Linux root:

# mkdir /data                         //新建目录# mkdir /data/www# mkdir /data/www/ROOT# cd /usr/local/tomcat/conf# vi server.xml                       //用vi编辑器打开server.xml配置文件,打开后键盘按i

We find the appbase= "WebApps" under the host node and modify it to:

appBase="/data/www"

Press ESC when the input is complete, and then enter:

# :wq!

You can save and exit the configuration file, and then restart Tomcat to take effect, and the Tomcat Web file directory becomes/data/www/root.

Configuring the Tomcat environment variable, we have configured the JDK and can look back.

6.Tomcat start-up, stop

1) Start the Tomcat server

# /usr/local/tomcat/bin/startup.sh

2) Stop the Tomcat server:

# /usr/local/tomcat/bin/shutdown.sh

Here, the Nginx + Tomcat load Balancer cluster has been deployed. However, with the server, there must be no database, the following expansion of Linux under the installation of MySQL database method.

Install MySQL database under 7.Linux

we re-searched the 192.168.2.30 MySQL database on this server:

1) Download MySQL database http://dev.mysql.com/downloads/mysql/

2) upload MySQL database installation package

$ scp ~/Downloads/mysql-5.1.51.tar.gz [email protected]:/usr/local

3) Install MySQL

$ ssh [email protected]//ssh connection # Groupadd MySQL//set up MySQL Group # useradd Mysql-g  MySQL//Add user MySQL to MySQL group # yum list|grep ncurses# yum-y install ncurses-devel# yum Install ncurses-devel# cd/usr/local# TAR-ZXVF mysql-5.1.51.tar.gz//Uncompressed # CD mysql-5.1.51#./configure--prefix=/usr/local/mysql--with -mysqld-ldflags=-all-static--with-client-ldflags=-all-static--with-readline--with-sll//configuration, set installation path, Set compile MySQL without shared library, set compile client without shared library, set to install the TAR package in RMP, set RMP install opensll# make# make install/install, [4] Time to wait longer, a few minutes to more than 10 minutes, is normal phenomenon #/usr/local/mysql/bin/mysql_install_db--user//Initialize the installed MySQL # CP./support-files/m Ysql.server/etc/init.d/mysql//Copy the MySQL boot service to the system and rename it to mysql# CP./SUPPORT-FILES/MY-MEDIUM.CNF/ETC/MY.CNF//Copy rule File # chmod 755/etc/init.d/mysql//Change file permissions//file permissions are composed of three digits, first: The permissions of the owner of the file, second: The permissions of the person in the same group as the owner of the file, and the third: the rights of the owner of the file that are not members of the same group//7 : Readable writable executable, 5: Readable executable # cd/usr/local/mysql# Chown-R MySQL.                                   Change the owner of the/usr/local/mysql to mysql# chgrp-r MySQL. Put/usr/local/mysql into MySQL group # ps-ef|grep mysql# kill-9 3632//kill all MySQL process numbers sequentially, this assumes 3632#/usr/local /mysql/bin/mysql_install_db--user=mysql//Initialize # service MySQL start//start MySQL #/usr/local/mysql/bin/mysqladmin-u root password ' 123456 '//set MySQL password

The MySQL installation was successful and we tested it:

# /usr/local/mysql/bin/mysql -u root -p

If you can log in to MySQL after entering your password, the test is successful. Below we set up MySQL to allow remote connection to open:

# /usr/local/mysql/bin/mysql -u root -pmysql> GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘123456‘ WITH GRANT OPTION;//创建用户用于远程连接(root:用户名,%:所有电脑都可以连接,也可以设置某个ip地址运行连接,123456:密码)mysql> flush privileges;            //立即生效

Let's look at the database users:

mysql> SELECT DISTINCT CONCAT(‘User: ‘‘‘,user,‘‘‘@‘‘‘,host,‘‘‘;‘) AS query FROM mysql.user;mysql> \q                           //登出

Find a computer to test, log in to MySQL using Navicat, login successfully.

This concludes this article.

Tip: Like this article remember "Top" Oh!

Nginx + Tomcat Reverse proxy load Balancing cluster Deployment Guide

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.