Nginx+Tomcat load balancing (separation of dynamic and static) cluster


1. Cluster overview

  • Under normal circumstances, a Tomcat site may have a single point of failure and cannot cope with the complex and diverse requests of too many customers. It cannot be used in a production environment alone, so we need a more reliable solution.
  • Nginx is a very good http server software, it can support up to 5000 concurrent connections, has strong static resource processing capability, stable operation, and very low consumption of system resources such as memory and CPU
  • At present, many large websites use Nginx server as the reverse proxy and load balancer of the back-end website to improve the load concurrency of the entire site

Using Nginx as the load balancer and Tomcat as the load cluster of the application server, the website topology is shown in the following figure:
mark
Nginx server as the front end, Tomcat server as the back end, web page requests are forwarded by the Nginx service
but not all web requests Forwarding, instead of requesting static pages to the Nginx server for processing, dynamic page requests are forwarded to the back-end Tomcat server for processing

But as far as we know, Tomcat is a lightweight application server, the acceptable amount of access may be insufficient, so we need multiple Tomcat servers, and then through the Nginx configuration weight to select the Tomcat server for processing, one of load balancing Strategy


Two, Nginx

Nginx server:

  1. Homepage uses precise matching
  2. Static pages use regular matching to process themselves
  3. The dynamic page uses a regular match to match the request at the end of jsp and forwards it to the Tomcat server using proxy_pass

Nginx static processing advantages

  1. Nginx's efficiency in processing static pages is much higher than that of Tomcat
  2. If Tomcat requests 1000 times, Nginx requests 6000 times
  3. Tomcat's throughput per second is 0.6M, and Nginx's throughput per second is 3.6M
  4. Nginx's ability to handle static resources is 6 times that of Tomcat

Nginx dynamic and static separation principle:
mark

  1. When the server receives the request from the client, there are both static and dynamic resources
  2. Static resources are served by Nginx
  3. Dynamic resource Nginx forwarded to the backend

The main parameters of Nginx configuration reverse proxy:

配置后端服务器池,以提供响应数据
upstream 服务器名 {
    
    }

配置将访问请求转发给后端服务器池名
proxy_pass http://服务器名

Three, configuration steps

1. Environment

  • VMware 16 Pro(16.1.0)
  • The network adapter is in NAT mode
  • The network card is configured to obtain an IP statically
  • Local YUM source warehouse
  • Source code compilation and installation
Host correspond IP address Required software
Nginx Server CentOS 7-1 192.168.126.11 nginx-1.12.2.tar.gz
Tomcat Server1 CentOS 7-2 192.168.126.12 apache-tomcat-9.0.16.tar.gz、jdk-8u201-linux-x64.rpm
Tomcat Server2 CentOS 7-3 192.168.126.13 apache-tomcat-9.0.16.tar.gz、jdk-8u201-linux-x64.rpm

2. Deploy Nginx load balancer

  • For detailed theoretical and practical aspects of Nginx source code compilation and installation, please refer to my previous blog, portal: https://blog.csdn.net/weixin_51486343/article/details/112390348
  • After downloading the package sharing resources, open Xshell and drag the Nginx package to the /opt/ directory
    mark
  • Here is a one-click deployment Nginx Shell script
cd /opt/
vim nginx.sh

#!/bin/bash

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

yum -y install pcre-devel zlib-devel gcc gcc-c++ make

useradd -M -s /sbin/nologin nginx

cd /opt
tar zxvf nginx-1.12.2.tar.gz

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

make -j 2 && make install

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

killall -9 nginx
nginx

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

chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service


. nginx.sh
#等待执行完毕
  • verification
    mark
    mark

3. Deploy 2 Tomcat application servers

cd /opt
vim tomcat.sh

#/bin/bash

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

cd /opt
rpm -ivh jdk-8u201-linux-x64.rpm

echo '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' > /etc/profile.d/java.sh

source /etc/profile

cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mv apache-tomcat-9.0.16 /usr/local/tomcat

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


. tomcat.sh
#等待执行完毕

#验证
netstat -natp | grep 8080 
java -version
http://192.168.126.11:8080
(http://192.168.126.12:8080)

mark
mark

  • The other one does the same
    mark

4. Dynamic and static separation configuration

  1. Tomcat Server1
mkdir /usr/local/tomcat/webapps/xcf1
#创建指定首页目录
#这里随后拖进来一张jpg格式的照片

vim /usr/local/tomcat/webapps/xcf1/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
        <html>
        <head>
        <title>JSP test2 page</title>
        </head>
        <body>
        <% out.println("这里是动态页面:123123");%>
        </body>
        </html>

mark

vim /usr/local/tomcat/conf/server.xml
#编辑tomcat主配置文件

<Host name="TomcatServer1" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
        <Context docBase="/usr/local/tomcat/webapps/xcf1" path="" reloadable="true" />
</Host>
#主机名:TomcatServer1
#工作目录:Webapps
#开启解压war包
#运行时有web应用自动部署
#首页文件在/usr/local/tomcat/webapps/xcf1目录中
默认文件:index.jsp

mark

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

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

mark

  1. Tomcat Server2 configuration

Basically the same as Tomcat Server1 configuration

mkdir /usr/local/tomcat/webapps/xcf2

mark
mark

#记得重启
/usr/local/tomcat/bin/shutdown.sh 
/usr/local/tomcat/bin/startup.sh 

mark

5. Configure Nginx Server

  1. Prepare static pages and static images
echo '<html><body><h1>这是默认目录静态页面</h1></body></html>' > /usr/local/nginx/html/index.html

mkdir /usr/local/nginx/html/xiaoxu

mark

  1. Configure the server list for load balancing

The weight parameter represents the weight, the higher the weight, the greater the probability of being assigned

vim /usr/local/nginx/conf/nginx.conf
#编辑nginx主配置文件

    #gzip  on;

    upstream tomcat_server {
    
    
        server 192.168.126.12:8080 weight=1;
        server 192.168.126.13:8080 weight=1;
        }


    server {
    
    
        listen       80;
        server_name  www.xcf.com;

        charset utf-8;

mark

  1. Configure Nginx to process dynamic page requests and forward the .jsp file request to the Tomcat server for processing
	location ~ .*.jsp$ {
    
    
		proxy_pass http://tomcat_server;	
		proxy_set_header HOST $host;		
		
		proxy_set_header X-Real-IP $remote_addr;    				
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
	}

mark

  1. Configure Nginx to handle static image requests
	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
    
    
		root /usr/local/nginx/html/xiaoxu;
		expires 10d;
	}

mark

  1. Restart the nginx service and test
systemctl restart nginx.service

Test the effect of static web pages:

Browser access: http://192.168.126.11/
mark

Browser visit http://192.168.126.11/cat.jpg
mark

Test the load balancing effect, refresh the browser to visit http://192.168.126.11/xcf/index.jsp
mark
mark

Guess you like

Origin blog.csdn.net/weixin_51486343/article/details/112759401