Nginx+Tomcat load balancing, dynamic and static separation cluster (theory + actual deployment)

1. Principles of Nginx Load Balancing Implementation

Nginx realizes load balancing through reverse proxy.
Principle of reverse proxy:
Insert picture description here
Nginx configures the main parameters of reverse proxy
upstream service pool name {}
configures the back-end server pool to provide corresponding data

proxy_pass http://service pool name
Configure the server processing that forwards the access request to the back-end server pool

Two, Nginx dynamic and static separation principle

Principle of Separation of
Dynamic and Static When the server receives requests from the client, there are both static resources and dynamic resources. The static resources are served by Nginx, and the dynamic resources Nginx are forwarded to the backend.
Insert picture description here

Nginx static processing advantages
Nginx's efficiency in processing static pages is much higher than Tomcat's processing capacity.
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
The ability to handle static resources for 3.6M Nginx is 6 times that of Tomcat

Three, case demonstration

3.1. Environmental preparation

VMware software
One centos7.6 as the scheduler, install Nginx, the ip address is 192.168.100.21
Two centos7.6 as web servers, install Tomcat, the ip addresses are 192.168.100.22/ 192.168.100.23
Turn off the firewall, turn off the core protection

[root@localhost ~]# systemctl stop firewalld
 [root@localhost ~]# setenforce 0

3.2, install and configure Tomcat

JDK must be installed before installing Tomcat.
Run the java -version command to check whether Java is installed. If not installed, you need to download and install it yourself

[root@localhost ~]# java -version 
如果没有装则执行下面命令
tar xf jdk-8u144-linux-x64.tar.gz                ##### 解压安装包
cp -rv jdk1.8.0_144/ /usr/local/java           #####复制文件到/usr/local/java  
vi /etc/profile                                             #####编辑环境变量

export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib

source /etc/profile                                     #####让刚才编辑的环境变量生效
java -version 

Unzip the apache-tomcat-8.5.23.tar.gz package

[root@localhost ~]#  tar xf apache-tomcat-8.5.23.tar.gz 
[root@localhost ~]# mv apache-tomcat-8.5.23/ /usr/local/tomcat8
[root@localhost ~]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
[root@localhost ~]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@localhost ~]# tomcatup
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.
[root@localhost ~]# netstat -anpt | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      22283/java  

Enter 192.168.100.22:8080 /192.168.100.23:8080 on the real machine to test and
Insert picture description here
Insert picture description here
test that both Tomcat servers have been installed and running normally

3.3. Establish a Java Web site

192.168.100.22

[root@localhost ~]# mkdir -pv /web/webapp1
mkdir: created directory ‘/web’
mkdir: created directory ‘/web/webapp1’
[root@localhost ~]#  vim /web/webapp1/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
        <head>
                <title>JSP test1 page</title>
        </head>
        <body>
                <% out.println("动态页面 1,http://www.51xit.com");%>
        </body>
        <body>
                <div>静态页面的图片 1</div><br><img src="logo.jpg">
        </body>
</html>
[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml
<Host name="localhost" appBase="webapps"
	unpackWARs="true" autoDeploy="true">           在此处插入下面两行
<Context docBase="/web/webapp1" path="" reloadable="false" >
</Context> 
[root@localhost ~]# /usr/local/tomcat8/bin/shutdown.sh
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh

Visit http://192.168.100.22:8080 through the browser and
Insert picture description here
display the text "Picture 1 of the static page", but no logo.jpg image appears. The reason is that the static image is temporarily unmatched, so it cannot be loaded.

192.168.100.23

[root@localhost ~]# mkdir -pv /web/webapp1
mkdir: created directory ‘/web’
mkdir: created directory ‘/web/webapp1’
[root@localhost ~]#  vim /web/webapp1/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
        <head>
                <title>JSP test1 page</title>
        </head>
        <body>
                <% out.println("动态页面 2,http://www.52xit.com");%>
        </body>
        <body>
                <div>静态页面的图片 2</div><br><img src="logo.jpg">
        </body>
</html>
[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml
<Host name="localhost" appBase="webapps"
	unpackWARs="true" autoDeploy="true">           在此处插入下面两行
<Context docBase="/web/webapp1" path="" reloadable="false" >
</Context> 
[root@localhost ~]# /usr/local/tomcat8/bin/shutdown.sh
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh

Visit http://192.168.100.23:8080 through the browser to
Insert picture description here
display the text "Picture 2 of the static page", but the logo.jpg image does not appear. The reason is that the static image is temporarily unmatched, so it cannot be loaded.

3.4, Nginx server configuration

Install Nginx on the Nginx server 192.168.100.21, reverse proxy to two Tomcat sites, and achieve load balancing

3.4.1, install Nginx

[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc-c++
[root@localhost ~]# groupadd www
[root@localhost ~]# useradd -g www www -s /bin/false
[root@localhost opt]# tar xzvf nginx-1.15.9.tar.gz
[root@localhost opt]# cd nginx-1.15.9/
[root@localhost nginx-1.15.9]# 
./configure --prefix=/usr/local/nginx --user=www --group=www --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module
[root@localhost nginx-1.12.0]# make && make install 
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost ~]# 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@localhost ~]# nginx                                ####启动
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN

3.4.2, configure nginx.conf

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 
#gzip  on;
    upstream tomcat_server {
    
    
        server 192.168.100.22:8080 weight=1;
        server 192.168.100.23:8080 weight=1;
        }

server {
    
    
	listen 80;
	server_name localhost;
	#charset koi8-r;
	#access_log logs/host.access.log main;
	location ~ .*.jsp$ {
    
    
		proxy_set_header HOST $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header Client-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass http://tomcat_server;
		}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
    
    
	root /usr/local/nginx/html/img;
	expires 30d;
}

location / {
    
    
	root html;
	index index.html index.htm;
}
…… '//省略部分内容 '
} 
…… '//省略部分内容 '
} 

3.4.3, configure Nginx static page

[root@localhost ~]# vim /usr/local/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"> 
<title>静态页面</title>
<style>
	body {
    
    
		width: 35em;
		margin: 0 auto;
		font-family: Tahoma, Verdana, Arial, sans-serif;
	}
</style>
</head>
<body>
<h1>静态页面</h1>
<p>这是个静态页面</p>
</body>
</html>

3.4.4. Prepare static pictures on Nginx

Transfer the picture to the /opt directory

[root@localhost ~]# mkdir /usr/local/nginx/html/img 
[root@localhost ~]# mv /opt/logo.jpg /usr/local/nginx/html/img
[root@localhost ~]# 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@localhost ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

3.5, verification

To test the effect of the static page, open the browser and visit http://192.168.100.21/, you can see that the nginx static page is accessed
Insert picture description here

To test the load balancing effect, open a browser and visit http://192.168.100.21/index.jsp. Constantly refreshing the browser test,
you can see that due to the same weight, the page will repeatedly switch back and forth between the following two pages. The first visit, the test page of dynamic page 1 appears, and the static page image on nginx can be loaded normally. After refreshing, the second visit, the test page of dynamic page 2 appears, which means that the load balancing cluster has been successfully built and it is OK Switched between the two Tomcat server sites.
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_48191211/article/details/108792557