Nginx+Tomcat realizes load balancing, dynamic and static separation

1. Principles of Nginx Load Balancing Implementation

1. Nginx realizes load balancing through reverse proxy

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 results obtained 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.

2. Nginx configures the main parameters of the reverse proxy

upstream service pool name {}

Role: Configure the back-end server pool to provide response data

proxy_pass http:// service pool name

Role: Configure the server processing that forwards the access request to the back-end server pool

Two, Nginx dynamic and static separation implementation principle

1. The principle of dynamic and static separation

The server receives the request from the client, there are both static resources and dynamic resources. The static resources are served by Nginx, and the dynamic resources are forwarded by Nginx to the backend.

2. Nginx static processing advantages

The efficiency of Nginx to process static pages is much higher than that of Tomcat.
If Tomcat’s request volume is 1000 times, then Nginx’s request volume is 6000 times.
Tomcat’s throughput per second is 0.6M, and Nginx’s throughput per second is 3.6M
Nginx The ability to handle static resources is 6 times that of Tomcat

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

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

1. Deploy Nginx load balancing server

Insert picture description here

#!/bin/bash
#脚本说明:编译安装nginx服务
#注意:使用前请将nginx-1.12.0.tar.gz放入/opt目录下

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

#安装所需开发包和编译环境、编译器
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/

#编译安装nginx;指定安装路径、指定用户名、组名、启用模块以支持统计状态
cd /opt/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 restart nginx.service
systemctl enable nginx.service

Insert picture description here
Insert picture description here

2. Deploy two Tomcat application servers

Insert picture description here

#!/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

Insert picture description here

3. Dynamic and static separation configuration

Please see tomcat

1) Tomcat1 server 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>

Insert picture description here

#修改配置文件
vim /usr/local/tomcat/conf/server.xml
<Host name="192" 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
Insert picture description here

2) Tomcat2 server 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>

Insert picture description here

#修改配置文件
vim /usr/local/tomcat/conf/server.xml
<Host name="192" 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
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/long
cd /usr/local/nginx/html/long

Insert picture description here

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

upstream tomcat_server {
    server 192.168.109.22:8080 weight=1;
    server 192.168.109.23:8080 weight=1;
}

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

    #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
Insert picture description here

Insert picture description here

4. Access test

Test the effect of the static page
browser visit http://192.168.109.7/
browser visit http://192.168.109.7/long.jpg to
Insert picture description here
test the load balancing effect, constantly refresh the browser to test the
browser visit http://192.168.109.7/ test/index.jsp
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51616026/article/details/113486130