Nginx+Tomcat realizes load balancing and separation of dynamics and statics (detailed pictures and texts! <script execution>)

Nginx+Tomcat realizes load balancing and separation of dynamics and statics (detailed pictures and texts!)

1. Principles of Nginx Load Balancing Implementation

1. Nginx achieves load balancing through reverse proxy

The Nginx server is the front end, the Tomcat server is the back end, and web page requests are forwarded by the Nginx service. But instead of forwarding all web requests, the Nginx server handles static page requests, and forwards dynamic page requests to the back-end Tomcat server for processing.

Reverse Proxy refers to using a proxy server (example: Nginx) to accept connection requests on the internet, and then forward the request to a server on the internal network (example: Tomcat), and return the result from the server For clients requesting a connection on the internet, the proxy server (for example: Nginx) acts as a reverse proxy server externally.
From the perspective of the client, in fact, the client does not know which server the real service provider is, it only knows that it has requested a reverse proxy server. Therefore, the reverse proxy method hides the address of the real server from the outside, reducing security risks to a certain extent.

Insert picture description here

2. Nginx static processing advantages

●Nginx's efficiency in processing static pages is much higher than that of Tomcat

●If Tomcat requests 1000 times, Nginx requests 6000 times

●Tomcat's throughput per second is 0.6M, and Nginx's throughput per second is 3.6M

●Nginx's ability to handle static resources is 6 times that of Tomcat

Two, Nginx configures the main parameters of the reverse proxy

Configure the back-end server pool to provide response data

upstream 服务器名 {}

Configure to forward access requests to the back-end server pool name

proxy_pass http://服务器名

Three, Nginx + Tomcat dynamic and static separation, load balancing configuration steps

Environmental preparation

Host operating system IP address Required software
Nginx Server CentOS7 192.168.2.3 nginx-1.12.0.tar.gz
Tomcat Server1 CentOS7 192.168.2.6 apache-tomcat-9.0.16.tar.gz
jdk-8u201-linux-x64.rpm
Tomcat Server2 CentOS7 192.168.2.7 apache-tomcat-9.0.16.tar.gz
jdk-8u201-linux-x64.rpm

1. Deploy yum source and Nginx load balancing server

Before this, you need to drag the relevant software package to the /opt directory

Copy the following code into the script file, then source the script to execute, and then wait for everything to be completed

#!/bin/bash
#======配置yum源======
echo -e "\033[31m =====正在验证当前为仅主机还是NAT模式===== \033[0m"
ping -c2 -w2 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then echo -e "\033[31m 检测当前为NAT模式,为您配置在线yum源 \033[0m"
mkdir -p /etc/yum.repos.d/repo.bak

mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null

yum clean all &> /dev/null
yum list &> /dev/null
echo -e "\033[31m 在线源已配置完成 \033[0m"

else
echo -e "\033[31m 检测当前为仅主机模式,为您配置本地yum源 \033[0m"
mount /dev/sr0 /mnt &> /dev/null
cd /etc/yum.repos.d/
mkdir -p /etc/yum.repos.d/repo.bak

mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null

echo '[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0' > /etc/yum.repos.d/local.repo
yum clean all &> /dev/null
yum makecache &> /dev/null

df -h | grep "/mnt" 
if [ $? -ne 0 ];then
echo -e "\033[31m 检测当前为仅主机模式,但光盘未连接! \033[0m"
else
echo -e "\033[31m 本地yum源已配置完成 \033[0m"
fi
fi
#======编译安装nginx服务======
#安装所需开发包和编译环境、编译器
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
#创建程序用户,便于准确控制访问
useradd -M -s /sbin/nologin nginx

#解压安装包
cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/

#指定安装路径、指定用户名、组名、启用模块以支持统计状态
cd nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-file-aio --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module


#编译及安装
make && make install

#软链接便于系统识别nginx操作命令
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

#添加nginx系统服务
echo '[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' > /lib/systemd/system/nginx.service

#赋权及开启服务、开启开机自启
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service

Insert picture description here

netstat -natp | grep 80

Insert picture description here

2. Deploy two Tomcat application servers

Before this, you need to drag the relevant software package to the /opt directory

Copy the following code into the script file, then source the script to execute, and then wait for everything to be completed

#!/bin/bash
#安装Tomcat服务

#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
setenforce 0

#安装JDK
cd /opt
rpm -ivh jdk-8u201-linux-x64.rpm

#设置JDK环境变量
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

#安装并启动Tomcat
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 Server1

Insert picture description here

Tomcat Server2

Insert picture description here

3. Dynamic and static separation configuration

1) Tomcat Server1 configuration

mkdir /usr/local/tomcat/webapps/test

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

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("动态页面 1:www.test1.com");%>
</body>
</html>

#修改配置文件
vim /usr/local/tomcat/conf/server.xml
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" />
</Host>

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

Insert picture description here

2) Tomcat Server2 configuration

mkdir /usr/local/tomcat/webapps/test

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

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

#修改配置文件
vim /usr/local/tomcat/conf/server.xml
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" />
</Host>

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

Insert picture description here

3) Nginx server configuration

#准备静态页面和静态图片
echo '<html><body><h1>this is static</h1></body></html>' > /usr/local/nginx/html/index.html

mkdir /usr/local/nginx/html/img
cd /usr/local/nginx/html/img
#把图片拖进来
ls

Insert picture description here

Modify the nginx main configuration file

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
#gzip on;

upstream tomcat_server {
    server 192.168.2.6:8080 weight=1;
    server 192.168.2.7:8080 weight=1;
}

server {
	listen 80;
	server_name localhost;

    #charset koi8-r;

	#access_log logs/host.access.log main;

        #配置Nginx处理动态页面请求,将 .jsp 文件请求转发到Tomcat 服务器处理
	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;
	}
        
    #配置Nginx处理静态图片请求
	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
		root /usr/local/nginx/html/img/;
		expires 10d;
	}
        location / {
            root html;
            index  index.html index.htm;
        }


systemctl restart nginx.service

Insert picture description here

4. Access test

Note: Be sure to ensure that all server and client firewalls and security mechanisms are closed, otherwise an error will be reported when accessing

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

Test the static page effect
browser visit http://192.168.2.3/
browser visit http://192.168.2.3/xlql.png

Insert picture description here

Test the load balancing effect, constantly refresh the browser to test the
browser visit http://192.168.2.3/test/index.jsp

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_35456705/article/details/113525433