Nginx+Tomcat high-availability load balancing cluster-dynamic and static separation

Tomcat overview

Introduction to Tomcat

  1. Originally developed by James Duncan Davidson, Sun’s software architect
  2. After installing Tomcat, the directories and files under the installation path are important files for using or configuring Tomcat

Tomcat important directory

Directory name effect
bin Store startup and shutdown Tomcat scripts
conf Store different configuration files of Tomcat
doc Store Tomcat documents
lib Store the library files needed for Tomcat to run
logs Store the LOG file when Tomcat is running
src Store the source code of Tomcat
webapps Tomcat's main web publishing directory
work Store the class files generated after jsp compilation

Nginx application

Nginx is a very good HTTP server software

  • Supports responses up to 50,000 concurrent connections
  • Possess powerful static resource processing capabilities
  • run smoothly
  • Very low consumption of system resources such as memory and CPU
  • At present, many large websites use Nginx server as a reverse proxy and load balancer for back-end website programs to improve the load concurrency of the entire site

Nginx load balancing implementation principle

Nginx realizes load balancing through reverse
proxy.Reverse proxy principle:
Insert picture description here

The main parameters of Nginx configuration reverse proxy

  • Upstream service pool name {}, configure the back-end server pool to provide response data
  • proxy_pass http:/service pool name, configure the server to forward access requests to the back-end server pool for processing

Nginx dynamic and static separation implementation principle

Dynamic and static separation principle

  • 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 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

Production environment practice

experimental design

  1. Requires deployment of two back-end Tomcat servers
  2. For testing, build two websites with different content
  3. Tomcat and website building steps
  • Turn off firewall
  • Install JDK, configure JAVA environment
  • Install and configure Tomcat
  • Change to /web/webapp1/ path
  • Create the /web/webapp1 directory, modify server.xml, create a test page index.jsp under the website file directory /web/webapp1/, and test
  1. Install Nginx on the Nginx server, reverse proxy two Tomcat sites, and achieve load balancing
  • Turn off Firewalld firewall
  • Install Nginx dependency packages
  • Unzip, compile and install Nginx
  • Configure nginx.conf, add location static page processing and add
  • upstream configuration section and proxy_pass
  • Detect configuration files and start Nginx
  • Test load balance, dynamic and static separation effect

1. Deploy Tomcat [1]

1. Install the JDK environment

[root@localhost ~]# tar xf jdk-8u144-linux-x64.tar.gz -C /opt
[root@localhost ~]# cd /opt
[root@localhost opt]# cp -rv jdk1.8.0_144/ /usr/local/java
[root@localhost opt]# 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
[root@localhost opt]# source /etc/profile
[root@localhost opt]# java -version
java version "1.8.0_144"
……省略部分

2. Install Tomcat service

[root@localhost ~]# tar vxf apache-tomcat-8.5.23.tar.gz -C /opt
[root@localhost ~]# cd /opt
[root@localhost opt]# mv apache-tomcat-8.5.23/ /usr/local/tomcat8
[root@localhost opt]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup 
[root@localhost opt]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@localhost opt]# tomcatup 
……省略部分
Tomcat started.
[root@localhost opt]# netstat -anpt | grep 8080
tcp6       0      0 :::8080                 ::: *                    LISTEN      77010/java      

3. Test the Tomcat server

Insert picture description here

4. Define a Tomcat virtual host

[root@localhost tomcat8]# 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 tomcat8]# mkdir -pv /web/webapp1      //创建网页目录
[root@localhost tomcat8]# vim /web/webapp1/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
        <head>
                <title>JSP test1 page</title>
        </head>
        <body>
        <h1>
                <% out.println("动态页面 1,http://www.TOM1.com");%>
        </h1>
        </body>
        <body>
                <div>静态页面的图片 1</div><br><img src="123.jpg">
				<br><h1>
                床前看月光,<br>
                疑是地上霜。<br>
                抬头望山月,<br>
                低头思故乡。<br>
				</h1>
        </body>
</html>
[root@localhost tomcat8]# tomcatdown 
[root@localhost tomcat8]# tomcatup 

5. Visit the test page

Insert picture description here

Two, deploy Tomcat [2]

1. Install the JDK environment

[root@localhost ~]# tar xf jdk-8u144-linux-x64.tar.gz -C /opt
[root@localhost ~]# cd /opt
[root@localhost opt]# cp -rv jdk1.8.0_144/ /usr/local/java
[root@localhost opt]# 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
[root@localhost opt]# source /etc/profile
[root@localhost opt]# java -version
java version "1.8.0_144"
……省略部分

2. Install Tomcat service

[root@localhost ~]# tar vxf apache-tomcat-8.5.23.tar.gz -C /opt
[root@localhost ~]# cd /opt
[root@localhost opt]# mv apache-tomcat-8.5.23/ /usr/local/tomcat8
[root@localhost opt]# ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup 
[root@localhost opt]# ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
[root@localhost opt]# tomcatup 
……省略部分
Tomcat started.
[root@localhost opt]# netstat -anpt | grep 8080
tcp6       0      0 :::8080                 ::: *                    LISTEN      77010/java      

3. Test the Tomcat server

Insert picture description here

4. Define a Tomcat virtual host

[root@localhost tomcat8]# 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 tomcat8]# mkdir -pv /web/webapp1
[root@localhost tomcat8]# vim /web/webapp1/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
        <head>
                <title>JSP test2 page</title>
        </head>
        <body>
        <h1>
                <% out.println("动态页面2,http://www.TOM1.com");%>
        </h1>
        </body>
        <body>
                <div>静态页面的图片 2</div><br><img src="123.jpg">
				<br><h1>
                床前看月光,<br>
                疑是地上霜。<br>
                抬头望山月,<br>
                低头思故乡。<br>
				</h1>
        </body>
</html>
[root@localhost tomcat8]# tomcatdown 
[root@localhost tomcat8]# tomcatup 

5. Visit the test page

Insert picture description here

Three, compile Nginx service

### 1.安装环境
[root@localhost opt]# yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl \
zlib-devel 

2. Create a running account

[root@localhost ~]# groupadd www
[root@localhost ~]#  useradd -g www www -s /bin/false

Supplement: /bin/false is the strictest option to prohibit login, and all services cannot be used.
/sbin/nologin just does not allow login system

3. Compile and install NGINX

[root@localhost ~]#  tar xzvf nginx-1.15.9.tar.gz -C /opt
[root@localhost ~]# cd /opt/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.15.9]# make -j3 && make install 

4. Optimize Road King and start the test

[root@localhost nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.15.9]# 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-1.15.9]# nginx
[root@localhost nginx-1.15.9]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      14763/nginx: master 

5. Set up system services

[root@localhost nginx-1.15.9]# yum -y install psmisc
[root@localhost nginx-1.15.9]# killall -9 nginx
[root@localhost nginx-1.15.9]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/ki11 -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target  
[root@localhost nginx-1.15.9]# systemctl enable nginx.service
[root@localhost nginx-1.15.9]# systemctl start nginx.service
[root@localhost nginx-1.15.9]# systemctl status nginx.service
● nginx.service - nginx
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-09-25 20:08:41 CST; 6s ago
  Process: 14822 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, sta

6. Modify nginx.comf configuration

[root@localhost img]# vim /usr/local/nginx/conf/nginx.conf
http {
    
    
……省略部分
upstream tomcat_server {
    
            //在http{}区域内插入这4行配置
        server 192.168.100.22:8080 weight=1;
        server 192.168.100.23:8080 weight=1;
        }
###docBase:web 应用的文档基准目录 
###reloadable 设置监视"类"是否变化 
###path="" 设置默认"类"
    server {
    
    
        location ~ .*.jsp$ {
    
                 //在server{}区域内插入以下配置
                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;
        }

[root@localhost nginx-1.15.9]# 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>

7. View pictures in the static resource directory

[root@localhost nginx-1.15.9]# mkdir /usr/local/nginx/html/img 
[root@localhost nginx-1.15.9]# cd /usr/local/nginx/html/img
[root@localhost img]# ls
123.jpg

8. Test dynamic and static separation and load balancing

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/CN_LiTianpeng/article/details/108794903