Implementation example of Nginx+Tomcat load balancing cluster

Implementation example of Nginx+Tomcat load balancing cluster

introduction

Generally speaking, a Tomcat site cannot be used alone in a production environment because of the possibility of single point failure and the inability to cope with too many complex and diverse requests from customers. Therefore, a more reliable solution is needed to improve the Web site architecture.

1. Case Overview

Nginx is a very good http server software. It can support up to 50,000 concurrent connections, has powerful static resource processing capabilities, runs very stably, and consumes very low system resources such as memory and CPU.

Currently, many large websites use Nginx servers as reverse proxies and load balancers for backend website programs to improve the load concurrency of the entire site.

The example uses Nginx as the load balancer and Tomcat as the application server to set up a load cluster. The architecture diagram is as follows

insert image description here

2. Environment Deployment

The case environment is as follows

Host operating system IP address Main software
Nginx Server CentOS 7.4 x86_64 192.168.8.140 nginx-1.12.2.tar.gz
Tomcat Server 1 CentOS 7.4 x86_64 192.168.8.133 ①apache-tomcat-9.0.16.tar.gz / ②jdk-8u201-linux-x64.rpm
Tomcat Server 2 CentOS 7.4 x86_64 192.168.8.134 ①apache-tomcat-9.0.16.tar.gz / ②jdk-8u201-linux-x64.rpm

Disable the firewall and the automatic startup

[root@ng133 ~]#systemctl stop firewalld.service #Turn off the firewall and the automatic startup function [root@ng133 ~]#systemctl status firewalld.service
[root@ng133 ~]#setenforce 0 #Disable security enhancement system [root@ng133 ~]#setenforce: SELinux is disabled

3. Nginx host installation

Install Nginx service, here is a one-click deployment script

#!/bin/bash
iptables -F
yum -y install epel-release && yum clean all && yum makecache
yum -y install pcre-devel zlib-devel gcc gcc-c++ make wget
useradd -M -s /sbin/nologin nginx
wget http://nginx.org/download/nginx-1.12.2.tar.gz -P /opt
tar zxvf /opt/nginx-1.12.2.tar.gz -C /opt
cd /opt/nginx-1.12.2

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

cd /opt/nginx-1.12.2
make -j 4 && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile = /usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

chmod 754 /usr/lib/systemd/system/nginx.service
systemctl daemon-reload && systemctl start nginx.service && systemctl enable nginx.service

echo " "
pgrep "nginx" &> /dev/null
if [ $? -eq 0 ];then
        echo -e "\033[32mnginx service is running normally, you can use curl to view\033[0m"
else
        echo -e "\033[31mnginx service is running abnormally, please check\033[0m"
fi

View the results after the installation is complete

[root@ng140 /opt/nginx-1.12.2]#curl -I http://192.168.8.140
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Fri, 15 Oct 2021 01:46:17 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 15 Oct 2021 01:45:34 GMT
Connection: keep-alive
ETag: "6168dd3e-264"
Accept-Ranges: bytes

insert image description here

4. Tomcat installation and configuration

1. Install Tomcat

The configuration methods of Tomcat Server 1 and Tomcat Server 2 are basically the same.

Install JDK and configure the environment

[root@tm1133 ~]#cd /opt/ #Upload the installation package to the /opt directory [root@tm1133 /opt]#ls
apache-tomcat-9.0.16.tar.gz jdk-8u201-linux-x64.rpm  
[root@tm1133 /opt]#rpm -ivh jdk-8u201-linux-x64.rpm #Install [root@tomcat /opt]#vim /etc/profile.d/java.sh #/etc/profile.d/Environment variable script directory	

export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar		
export PATH=$JAVA_HOME/bin:$PATH

[root@tm1133 /opt]#source /etc/profile.d/java.sh #Import the script into the environment variable to make it effective [root@tm1133 /opt]#java -version #Check the version java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

Unpacking

[root@tm1133 /opt]#tar zxvf apache-tomcat-9.0.16.tar.gz #Unpack [root@tm1133 /opt]#mv apache-tomcat-9.0.16 /usr/local/tomcat #Transfer the package location and rename it

Start and optimize Tomcat management (create soft links and optimize the start command)

[root@tm1133 /opt]#ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
[root@tm1133 /opt]#ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
[root@tm1133 /opt]#startup.sh #OpenUsing CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/jdk1.8.0_201-amd64
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tm1133 /opt]#netstat -antp |grep 8080 #Check whether tcp6 is enabled successfully 0 0 :::8080 :::* LISTEN 2520/java  

2. Tomcat Server 1 Configuration

[root@tm1133 ~]#mkdir /usr/local/tomcat/webapps/gl #Create a test directory [root@tm1133 ~]#vim /usr/local/tomcat/webapps/gl/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> #Dynamic page configuration <html> 
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("Dynamic page 1, http://www.test1.com");%>
</body>
</html>


#Edit the tomcat main configuration file and add the virtual host configuration. Here you need to delete the original host name and other configurations first [root@tm2134 /opt]#vim /usr/local/tomcat/conf/server.xml			   
-------------------------------------------------------------------------------------------------
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" 
#Find this and delete it first, otherwise there will be an error in the end-------------------------------------------------------------------------------------------------
#Insert the following configuration at the end of line 162, paying attention to the ending </Host>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
     <Context docBase="/usr/local/tomcat/webapps/gl" path="" reloadable="true" />
</Host>
-------------------------------------------------------------------------------------------------
[root@tm2134 /opt]#shutdown.sh		
[root@tm2134 /opt]#startup.sh #Restart the service

3. Tomcat Server 2 Configuration

[root@tm2134 /opt]#mkdir /usr/local/tomcat/webapps/gl #Create a test directory [root@tm2134 /opt]#vim /usr/local/tomcat/webapps/gl/index.jsp #Configuration of dynamic pages <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title>
</head>
<body>
<% out.println("Dynamic page 2, http://www.test2.com");%>
</body>
</html>


#Edit the tomcat main configuration file and add the virtual host configuration. Here you need to delete the original host name and other configurations first [root@tm2134 /opt]#vim /usr/local/tomcat/conf/server.xml			   
-------------------------------------------------------------------------------------------------
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" 
#Find this and delete it first, otherwise there will be an error in the end-------------------------------------------------------------------------------------------------
#Insert the following configuration at the end of line 162, paying attention to the ending </Host>
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
     <Context docBase="/usr/local/tomcat/webapps/gl" path="" reloadable="true" />
</Host>
-------------------------------------------------------------------------------------------------
[root@tm2134 /opt]#shutdown.sh		
[root@tm2134 /opt]#startup.sh #Restart the service

5. Nginx server configuration

Static page configuration

[root@ng140 ~]#echo '<html><body><h1>this is static</h1></body></html>' > /usr/local/nginx/html/index.html
[root@ng140 ~]#cat /usr/local/nginx/html/index.html 
<html><body><h1>this is static</h1></body></html>

[root@ng140 /usr/local/nginx/html]#mkdir /usr/local/nginx/html/picture #Upload pictures [root@ng140 /usr/local/nginx/html]#cd picture/
[root@ng140 /usr/local/nginx/html/picture]#rz -E
rz waiting to receive.
[root@ng140 /usr/local/nginx/html/picture]#ls
ha.jpg
#Add the following configuration parameters under line 57 of the main configuration file to enable the page to load pictures [root@ng140 /usr/local/nginx/html/picture]#vim /usr/local/nginx/conf/nginx.conf
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root /usr/local/nginx/html/picture;
expires 10d;
 }
[root@ng140 /usr/local/nginx/html/picture]#nginx -t
[root@ng140 /usr/local/nginx/html/picture]#systemctl restart nginx.service 

Configure the nginx main configuration file

[root@ng140 ~]#vim /usr/local/nginx/conf/nginx.conf
......
#Configure the server list for load balancing. The weight parameter indicates the weight. The higher the weight, the greater the probability of being assigned. #gzip on; #Add the following content below line 33 upstream tomcat_server {
server 192.168.8.133:8080 weight=1;
server 192.168.8.134:8080 weight=1;
}  


															#Add the following configuration parameters under line 45 #access_log logs/host.access.log main;

 46 location ~ .*\.jsp$ {
 47 proxy_pass http://tomcat_server; 
 48 proxy_set_header HOST $host;
 49 proxy_set_header X-Real-IP $remote_addr;                
 50 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 51 }            

Add parameter parsing

location ~ .*\.jsp$ {			
#Assign the client IP address received by nginx to the source IP in the request to jump to tomcat, identify the client's real IP, and assign and jump proxy_pass http://tomcat_server; 

proxy_set_header HOST $host;	
#Set the host name (domain name or IP, port) of the request received by the backend web server. The default host value is the host name set by proxy_pass direct connection proxy_set_header X-Real-IP $remote_addr;		
#Copy $remote_addr to X-Real-IP (customized) to get the source IP back and forth

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;		
#When nginx is used as a proxy server, the IP list set will record the IPs passed by, the proxy and its IP

Check the main configuration file syntax and restart the service

[root@ng140 ~]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@ng140 ~]#systemctl restart nginx.service

6. Verification Results

Testing the effect of static pages with Firefox

insert image description here

Test whether the load balancing is effective. Refresh the web page and you can see that the dynamic pages 1 and 2 are switched back and forth.

http://192.168.8.140/index.jsp

insert image description here

insert image description here

Summarize

You can put two or more Tomcat servers in the upstream of Nginx to form a load balancing cluster, and then set the cluster site in the location through the Web proxy method such as proxy_pass, and then set the weight of the Tomcat server separately through the weight value.

In a production environment, the hardware configurations of Tomcat servers may vary. You can modify the weight values ​​of the corresponding servers to control the distribution of access requests to servers with higher or lower configurations.

This is the end of this article about the implementation example of Nginx+Tomcat load balancing cluster. For more relevant Nginx+Tomcat load balancing cluster content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of Nginx+Tomcat load balancing cluster installation and configuration case
  • Nginx+Tomcat high performance load balancing cluster construction tutorial
  • How to build a high-performance load balancing cluster with Nginx+Tomcat
  • nginx+tomcat implements load balancing and uses redis session sharing
  • Nginx and Tomcat realize dynamic and static separation and load balancing
  • Detailed explanation of Nginx+Tomcat+Https server load balancing configuration practice
  • Implementation of Nginx+Tomcat load balancing and dynamic and static separation cluster

<<:  A brief discussion on the use and analysis of nofollow tags

>>:  33 of the best free English fonts shared

Recommend

Some tips on deep optimization to improve website access speed

<br />The website access speed can directly ...

How to install PHP7 Redis extension on CentOS7

Introduction In the previous article, we installe...

How to display web pages properly in various resolutions and browsers

The key codes are as follows: Copy code The code i...

Detailed tutorial on installing MySQL 8.0.20 database on CentOS 7

Related reading: MySQL8.0.20 installation tutoria...

Analyze the working principle of Tomcat

SpringBoot is like a giant python, slowly winding...

Vite introduces the implementation of virtual files

Table of contents background Importing virtual fi...

30 minutes to give you a comprehensive understanding of React Hooks

Table of contents Overview 1. useState 1.1 Three ...

MySQL and sqlyog installation tutorial with pictures and text

1. MySQL 1.1 MySQL installation mysql-5.5.27-winx...

Detailed explanation of the principles and usage of MySQL stored procedures

This article uses examples to explain the princip...

Detailed explanation of how to enable slow query log in MySQL database

The database enables slow query logs Modify the c...

Detailed explanation of Svn one-click installation shell script under linxu

#!/bin/bash #Download SVN yum -y install subversi...

Implementation of MySQL5.7 mysqldump backup and recovery

MySQL backup Cold backup:停止服務進行備份,即停止數據庫的寫入Hot ba...

How to completely uninstall node and npm on mac

npm uninstall sudo npm uninstall npm -g If you en...