Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Methods of Nginx+Tomcat reverse proxy, load balancing and cluster deployment

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "the method of Nginx+Tomcat reverse proxy, load balancing and cluster deployment". In the daily operation, I believe many people have doubts about the methods of Nginx+Tomcat reverse proxy, load balancing and cluster deployment. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Nginx+Tomcat reverse proxy, load balancing, cluster deployment". Next, please follow the editor to study!

Performance comparison of nginx with other servers:

The tomcat server is a heavyweight server for the java language, while nginx is a lightweight server. Apache server is stable, open source and cross-platform, but apache server does not support high concurrency. Nginx can handle millions of tcp connections and more than 100000 concurrent connections, and it is a good cross-platform server.

The main advantages of nginx are high concurrency, simple deployment, low memory consumption, low cost and so on. The main disadvantages are that rewrite is not powerful enough and there are not as many modules as apache.

This article mainly explains the deployment of nginx + tomcat reverse proxy and load balancing, which is popular and practical. There is not much relationship between each part of this article, you can study separately according to your needs.

Let's take a look at the process of nginx reverse proxy:

The process of nginx load balancing (servers with less pressure are automatically selected for access):

It can be seen that load balancing is achieved through the principle of reverse proxy, so it is also called reverse proxy load balancing. So we will deploy load balancing, so will the reverse proxy.

Generally speaking, the implementation of load balancing can be divided into software implementation and hardware implementation. The efficiency of hardware implementation is very high, but the corresponding cost is also very high. Software is not as efficient as hardware, but the cost is much lower. Using nginx server to achieve load balancing, then it is through the software to achieve load balancing, and nginx itself supports high concurrency and so on. Therefore, using nginx server to achieve load balancing can greatly save the cost of enterprises, and because nginx is server software, its execution efficiency is also very high. The purpose of this article is to help you use nginx to achieve load balancing.

The core of load balancing is to establish a server cluster, and then the user first accesses the third-party proxy server (here we choose nginx), then the proxy server selects a server in the cluster, and then introduces the request to the selected server (here we choose tomcat).

As shown in the figure below, we implement the following load balancer through reverse proxy. Here we assume that there are four servers in the public network ip, one as a proxy server, and three as a load balancer server:

All right, the overall structure is basically clear, so let's implement it:

Tip: in this article, we basically use ssh for related operations. Under windows, you can try to install putty,mac and directly use the terminal tools that come with the system.

Build nginx server under 1.linux

We set up the nginx server on the server 192.168.2.20:

1) download nginx

2) upload server nginx installation package

$scp ~ / downloads/nginx-1.10.2.tar.gz root@192.168.2.20:/usr/local

3) install nginx

$ssh root@192.168.2.20 / / ssh connection # yum-y install gcc gcc-c++ autoconf automake / / gcc, library file for gcc-c++ # yum install-y pcre pcre-devel / / install nginx dependency package # yum install-y zlib zlib-devel

Note:-y means to judge all yes,autoconf means automatic configuration, and automake means automatic compilation.

# cd / usr/local# tar-zxvf nginx-1.10.2.tar.gz / / decompress # cd nginx-1.10.2 / / switch to this directory #. / configure / / configure # make# make install / / install

Verify that the installation is complete:

# cd / usr/local# ls / / if the nginx folder exists, the installation is successful

After the installation steps and directory settings above, the startup program of nginx is / usr/local/nginx/sbin/nginx, and 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 and reverse proxy path in the new configuration file.

Start, stop and signal control of 2.nginx

1) start the nginx server (format: nginx executable file-c nginx configuration file):

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

2) stop the nginx server:

The first step to stop the nginx server is to query the main process number (master process) of nginx, assuming that the query here gets 1060 (for the convenience of the following demonstration):

# ps-ef | grep nginx

Let's take a look at how to stop the nginx server. There are three ways to stop nginx:

Take your time to stop:

# ps-ef | grep nginx / / View the main process number (master process) of nginx, assuming that the query here gets 106 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, we need to restart to take effect. We also need to verify the correctness of the configuration file before restarting, and then restart:

# / usr/local/nginx/sbin/nginx-t-c / usr/local/nginx/conf/nginx.conf / / verify # / usr/local/nginx/sbin/nginx-s reload / / restart

4) smooth upgrade of nginx server

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

# / usr/local/nginx/sbin/nginx-v / / View the current version

The following is a smooth upgrade:

# cd / usr/local# tar-zxvf nginx-1.11.6.tar.gz / / extract the new version of nginx# cd nginx-1.11.6 / / switch to this directory #. / configure / / configure # make# cd / usr/local/nginx/sbin / / Open the old version of nginx executable location # cp nginx nginx.old / / back up the old version of nginx executable file Prevent upgrade errors from restoring # cp-rfp / usr/local/nginx-1.11.6/objs/nginx / usr/local/nginx/sbin / / copy the new version of the executable file to the old version # rm-f / usr/local/nginx-1.11.6.tar.gz / / remove the compressed file # rm-rf / usr/local/nginx-1.11.6/ / delete the folder

At this point, the nginx server was smoothly upgraded successfully.

Implementation of load balancing in 3.nginx

Let's first connect to ssh, and then do the following (it is generally not recommended to modify the default main configuration file nginx.conf, so we create a new load balancer configuration file fzjh.conf to ensure server security, as shown below):

# cd / usr/local/nginx/conf# touch fzjh.conf# vi fzjh.conf / / Open the file with the vi editor, and then press I on the keyboard

Note: in the vi editor, the keyboard presses I to enter insert state and esc to exit insert state.

Then enter the following configuration code (the comments section is opened on demand):

# set low-privileged users, set the number of user nobody;# work derivative processes for security worker_processes 4 * set error file storage path # error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;# set pid storage path (pid is an important file in the control system) # pid logs/nginx.pid;# set the maximum number of connections events {worker_connections 1024 } http {# is mainly used to set a set of proxy servers that can be used in proxy_pass and fastcgi_pass instructions. The default load balancing method is to poll upstream tomcat_client {# to set two or more requests for the same cookie, requesting the same server ip_hash # weight weight, default 1. The higher the weight, the greater the access probability. For backup backup server, start server 192.168.2.21 weight=5; server 8080 weight=5; server 192.168.2.22 server 8080 weight=5; server 192.168.2.23 server 8080 weight=5 backup;} # enable gzip compression. When enabled, the visiting web page will automatically compress # gzip on; # specify the server name and parameter server {listen 80 Server_name test.nginxtest.net; # setting character # charset koi8-r; # location / refers to using the root directory for load balancing location / {proxy_pass http://tomcat_client; proxy_redirect default; # setting proxy 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 let's load our configuration file:

# / usr/local/nginx/sbin/nginx / / launch nginx# / usr/local/nginx/sbin/nginx-c / usr/local/nginx/conf/fzjh.conf / / load the configuration file

If there is port occupancy, you can use the following command to kill the nginx program:

# killall-9 nginx

At this point, the nginx server is deployed. Let's start to deploy the three real data servers under the load balancer.

Install jdk under 4.linux

We installed jdk on the three servers 192.168.2.21, 192.168.2.22, and 192.168.2.23, taking 192.168.2.21 as an example:

1) download jdk

2) upload server jdk installation package

$scp ~ / downloads/jdk-8u112-linux-x64.tar.gz root@192.168.2.21:/usr/local

3) install jdk

$ssh root@192.168.2.21 / / ssh connection # cd / usr/local# tar-zxvf jdk-8u112-linux-x64.tar.gz / / extract # mv jdk1.8.0_112/ jdk / / rename jdk1.8.0_112 to jdk# vi / etc/profile / / Open the file with the vi editor, and then press I on the keyboard

Next, we move the cursor to the end, enter the two newlines, and then add the following code to configure the environment variable for java:

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. At this point, if 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 variables are configured successfully. Let's verify that jdk is installed and configured successfully:

# java-version

If the java version number is returned, the installation is successful.

Build tomcat server under 5.linux

We build tomcat servers on three servers: 192.168.2.21, 192.168.2.22, and 192.168.2.23. Take 192.168.2.21 as an example:

1) download tomcat

2) upload server tomcat installation package

$scp ~ / downloads/apache-tomcat-8.5.9.tar.gz root@192.168.2.21:/usr/local

3) install tomcat

$ssh root@192.168.2.21 / / ssh connection # cd / usr/local# tar-zxvf apache-tomcat-8.5.9.tar.gz / / decompress # mv apache-tomcat-8.5.9/ tomcat / / rename apache-tomcat-8.5.9 to tomcat

4) set the tomcat web page file directory

The path to the default web page file of tomcat is / usr/local/tomcat/webapps/root, but usually we don't store it here. We create a new data/wwwroot directory in the linux root directory:

# mkdir / data/ / create a new directory # mkdir / data/www# mkdir / data/www/root# cd / usr/local/tomcat/conf# vi server.xml / / Open the server.xml configuration file with the vi editor, then press I on the keyboard

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, then restart tomcat to take effect, and the tomcat web page file directory becomes / data/www/root.

Configure the tomcat environment variable, which is already configured when we configure jdk, so we can take a look back.

Start and stop of 6.tomcat

1) start the tomcat server

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

2) stop the tomcat server:

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

At this point, the nginx + tomcat load balancing cluster has been deployed. However, with a server, there must be a database, so let's expand the method of installing mysql database under linux.

Install mysql database under 7.linux

We re-found the mysql database on the server 192.168.2.30:

1) download mysql database

2) upload mysql database installation package

$scp ~ / downloads/mysql-5.1.51.tar.gz root@192.168.2.30:/usr/local

3) install mysql

$ssh root@192.168.2.30 / / 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 / / decompress # 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 the installation path, set the compilation mysql without shared libraries, set the compilation client without shared libraries, set the tar package to be installed in rmp, install opensll# make# make install / / installation in rmp, and wait a long time in make [4], several minutes to more than ten minutes. All are normal # / usr/local/mysql/bin/mysql_install_db-- user / / A pair of installed mysql initializes # cp. / support-files/mysql.server / etc/init.d/mysql / / copy the mysql startup service to the system and rename it to mysql# cp. / support-files/my-medium.cnf / etc/my.cnf / / replication rules file # chmod 755 / etc/init.d/mysql / / change file permissions / / File permissions consist of three numbers The first: the permissions of the file owner, the second: the permissions of the file owner in the same group, the third: the rights of the file owner in different groups / / 7: readable, writable and executable, 5: readable and executable # cd / usr/local/mysql# chown-r mysql. Change the owner of / usr/local/mysql to mysql# chgrp-r mysql. / / put / usr/local/mysql into the mysql group # ps-ef | grep mysql# kill-9 3632 / / kill all the process numbers of mysql in turn. It is assumed that 363 processes / 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 the mysql password

At this point, mysql has been successfully installed, so let's test it:

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

If you can log in to mysql after entering your password, the test is successful. Let's set up mysql to allow remote connections to be opened:

# / usr/local/mysql/bin/mysql-u root-pmysql > grant all privileges on *. * to 'root'@'%' identified by' 123456' with grant option;// creates users for remote connections (root: user name,%: all computers can connect, or you can set an ip address to run the connection, 123456: password) mysql > flush privileges; / / effective immediately

Let's query the users of the database:

Mysql > select distinct concat ('user:'', user,'''@''',host,''';') as query from mysql.user;mysql >\ Q / / Logout

Find a computer to test, use navicat to remotely log in to mysql, log in successfully.

At this point, the study of "Nginx+Tomcat reverse proxy, load balancing, cluster deployment" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report