Nginx+Tomcat load balancing, dynamic and static separation cluster construction method

Preface

Introduction to Tomcat
· It was originally developed by James Duncan Davidson, a software architect from Sun. After Tomcat is installed, the directories and files under the installation path are important files for using or configuring Tomcat

1. Tomcat important directory

Insert picture description here

2. Nginx application

2.1 nginx advantages

Nginx is an excellent HTTP server software. It
supports responses of up to 50,000 concurrent connections. It
has powerful static resource processing capabilities
, stable operation
, and very low consumption of system resources such as memory and CPU.
At present, many large websites use Nginx servers as the back
Reverse proxy and load balancer of the end website program to improve the load concurrency of the entire site

Advantages
of Nginx static processing · 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 per second Throughput is 3.6M
· Nginx's ability to handle static resources is 6 times that of Tomcat

2.2 Principles of Nginx Load Balancing Implementation

Nginx realizes load balancing through reverse proxy

Principle of reverse proxy
Insert picture description here

The main parameters of Nginx configuration reverse proxy
upstream service pool name{} Four-layer proxy
Configure the back-end server pool to provide response data
proxy_pass http://service pool name//Proxy module to forward requests to the service pool
configuration to forward access requests Server processing for the back-end server pool

nginx supports seven-layer proxy

2.3 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
Advantages
of Nginx static processing · 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 per second Throughput is 3.6M
· Nginx's ability to handle static resources is 6 times that of Tomcat

3. Case configuration

Test load balancing, dynamic and static separation effect.
Environmental configuration. Use
Nginx as load balancer, Tomcat as application server
site, and realize load balancing
. Turn off Firewalld firewall.
Install Nginx dependent software package.
Decompress, compile and install Nginx
configuration nginx.conf, add location static page processing And add the
upstream configuration section and proxy_pass
detection configuration file and start Nginx

Tomcat deployment and test website construction
requires the deployment of two back-end Tomcat servers
. In order to test, set up two different websites.
Tomcat deployment and website construction steps
. Turn off the firewall
. Install JDK, configure the JAVA environment.
Install and configure Tomcat to
create /web/ webapp1 directory, modify server.xml, change the website file directory
to /web/webapp1/ under the path
/web/webapp1/ to create a test page index.isp, and test

3.1 Environment construction

Prepare 4 devices,
one nginx server for static address resolution and reverse proxy, to achieve load balancing control (NGINX),
two tomcat servers for dynamic resolution and node server use (PC-3, PC-4),
one client Machine access

Package preparation:
Insert picture description here

3.2 Dynamic and static separation case configuration

3.2.1 Configure nginx server

Compile and install nginx

[root@nginx ~]# systemctl stop firewalld
[root@nginx ~]# setenforce 0
setenforce: SELinux is disabled
[root@nginx ~]# yum -y install gcc \
> gcc-c++ \
> make \
> pcre-devel \
> expat-devel \
> perl \
> zlib-devel \
> pcre

[root@nginx nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@nginx nginx-1.12.2]# tar zxvf nginx-1.12.2.tar.gz
[root@nginx nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module     //统计模块
[root@nginx nginx-1.12.2]# make && make install

Create soft connection
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

Hand over service to systemctl management

[root@nginx nginx-1.12.2]#  vim /etc/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/kill -S QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
~

Add execute permission

[root@nginx nginx-1.12.2]# chmod 754 /etc/systemd/system/nginx.service

Start service test

[root@nginx nginx-1.12.2]# systemctl start nginx.service


[root@nginx nginx-1.12.2]# netstat -ntap | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      19171/nginx: master

3.2.2 Install JDK and TOMCAT on two node servers

Here is one set as an example, the same installation method for two sets

[root@pc-3 opt]# tar -zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local

Modify environment variables

[root@pc-3 opt]# vim /etc/profile
加入4行
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${
    
    JAVA_HOME}/jre
export CLASSPATH=.:${
    
    JAVA_HOME}/lib:${
    
    JRE_HOME}/lib
export PATH=${
    
    JAVA_HOME}/bin:$PATH
[root@pc-3 opt]# source /etc/profile

Install tomcat

[root@pc-3 opt]# tar -zxvf apache-tomcat-8.5.16.tar.gz  -C /usr/local
[root@pc-3 opt]# cd /usr/local
[root@pc-3 local]# mv apache-tomcat-8.5.16/ tomcat
[root@pc-3 local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin
[root@pc-3 local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin

3.2.3 Configure nginx to start dynamic and static separation

[root@nginx conf]# vim nginx.conf

添加此配置.......................
以JSP 结尾的 动态请求都指向 192.168.100.38080服务器
 location ~.*.jsp$ {
    
    
          proxy_pass http://192.168.100.3:8080;
          proxy_set_header HOST $host;
  }


Check the configuration

[root@nginx conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax isok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Configure home page file

[root@nginx conf]# vim /usr/local/nginx/html/index.html
<h1> this is 静态页面</h1>
<meta http-equiv="content-type" content="text/html;charset=utf-8">

Configure tomcat home page file

Vim /usr/local/tomcat/webapps/test/index.html

<!DOCTYPE html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html>
 <head>
    <title>JSP test1 page</title>
 </head>
 <body>
    <div> 这是动态页面</div>
 </body>
</html>

3.2.4 The client accesses 192.168.100.20 and 192.168.100.20/test/index.jsp to test dynamic and static separation

Insert picture description here

Insert picture description here

3.2.5 nginx handles static images, TOMcat handles dynamic page tests

Modify the tomcat home page file

<!DOCTYPE html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
 <head>
    <title>JSP test1 page</title>
 </head>
 <body>
    <div> 这是动态页面</div>
   <img src="123.jpg"/>
 </body>
</html>
~

Modify nginx configuration file

Insert picture description here
Put the 123.jpg picture in this path site directory

[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# mkdir test
[root@nginx html]# cd test
[root@nginx test]# rz -E
rz waiting to receive.
[root@nginx test]# ls
123.jpg
[root@nginx test]# pwd
/usr/local/nginx/html/test
[root@nginx test]#

3.2.6 Test, the visit is successful

Insert picture description here

3.3 Load balancing case

3.3.1 Configure the home page files of two tomcats

[root@pc-3 examples]# cd /usr/local/tomcat/webapps/
[root@pc-3 webapps]# mkdir new

Vim index.html

Insert picture description here

[root@pc-4 examples]# cd /usr/local/tomcat/webapps/
[root@pc-4 webapps]# mkdir new

Vim index.html

Insert picture description here

3.3.2 Configure tomcat's home page site directory settings (same operation for both)

Take one of them as an example

[root@pc-4 conf]# vim server.xml
添加此行

<Context path="" docBase="new"  reloadable="true"/>    //new 为默认读取的站点目录

Insert picture description here

[root@pc-4 conf]# vim web.xml

Insert picture description here

3.3.3 Configure nginx configuration file

Insert picture description here

3.3.4 Access 192.168.100.20 test

Insert picture description here
Refresh, automatically switch to node 2

Insert picture description here

Guess you like

Origin blog.csdn.net/BIGmustang/article/details/108413124