编程知识 cdmana.com

When nginx meets Tomcat cluster, it is a load balanced love hate relationship

Preface

I think everyone is concerned about Nginx and Tomcat It's all very familiar ,Nginx It's widely used , It's not just right web Static resources are very friendly , It is also a very practical reverse agent and load balancing software . Combined with the back end Tomcat Service for , To build Nginx+Tomcat colony .

For the friends who want to practice directly, you can get the software package in the link of this article and directly look at the content of the third backup .

One 、 Cluster building structure and Topology

1.1 Cluster architecture diagram

Nginx+Tomcat The structure of the cluster is as follows :

 Build like this Nginx+Tomcat colony , Achieve load balancing and dynamic and static separation , Too easy !

 

1.2 System environment and address planning

Use three Centos7 The server (7.4), The plan is as follows :

Server network card mode IP Address NginxNAT20.0.0.128Tomcat1NAT20.0.0.130Tomcat2NAT20.0.0.136

1.3 The topology is as follows

 Build like this Nginx+Tomcat colony , Achieve load balancing and dynamic and static separation , Too easy !

 

1.4 Related resource software

link :https://pan.baidu.com/s/1Qdla-vrpcspcAKJucZdSUg Extraction code :40it

Two 、 Building ideas and core part configuration

According to the above structural diagram , In order to complete the practical content , We need to sort out the idea of building , Understand the operation and configuration of the core part .

1、 First, we need to compile and install the corresponding services on three servers ( The package is in the link above ), Test whether the service is normal ;

2、 Second, based on the core functions : Load balancing and dynamic separation , It needs to be sorted out step by step

Based on Load Balancing

Load balancing is in Nginx Configured on the server , You need to be right about nginx The main configuration file of , The module to achieve load balancing is to use upstream Module and corresponding algorithm ( This paper uses a simple weighted round robin algorithm to achieve load balancing ). The core configuration :

#server Command outer layer 
upstream tomcat-server {
                server 20.0.0.130:8080 weight=1;
                server 20.0.0.136:8080 weight=1;
        }
#server In the instruction 
location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://tomcat-server;
        }

visit nginx The server address of , Can rotate access to the back-end two real Tomcat The server .

Based on dynamic separation

We know that Nginx for , Its support for static resources is very friendly , and Tomcat about java The dynamics of the web Page support is very good . So the need for dynamic separation is to give static requests to nginx Server running ,Tomcat Responsible for handling similar jsp Dynamic requests for files .

This case uses nginx Server and one Tomcat Server dynamic separation . Finally will combine the static picture to let nginx Responsible for handling , While using Tomcat Dealing with dynamic pages .

The core configuration :

nginx The server :

location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
            root html/demo;
            expires 30d;
        }
location ~.*.jsp$ {       ## matching jsp Page Jump proxy pool 
           proxy_pass http://tomcat-server;
           proxy_set_header Host $host;
        }
 location / {
            root   html;
            index  index.html index.htm;
            #proxy_pass http://tomcat-server;
        }

tomcat The server :

        <Context docBase="/usr/local/tomcat/webapps/demo" path="" reloadable="false">
         </Context>

3、 In the process of deployment and configuration , Carry out necessary verification

OK, the general process and core configuration are finished , Let's start the complete demonstration of this case .

3、 ... and 、 Deployment process and practice process

Load balancing cluster construction

3.1 Deploy two tomcat The server

Because of the deployment of two tomcat The flow of the server is almost the same ( Except that the content part of the page is inconsistent , Of course, to verify load balancing ), And it doesn't seem that this article is too long , Just demonstrate tomcat1 Deployment on the server .

3.1.1 install jdk

====================================================================================
tomcat1
[root@localhost ~]# hostnamectl set-hostname tomcat1
[root@localhost ~]# su
[root@tomcat1 ~]# cd /opt/
[root@tomcat1 opt]# ls
apache-tomcat-9.0.16.tar.gz  jdk-8u91-linux-x64.tar.gz  rh
[root@tomcat1 opt]# tar zxf jdk-8u91-linux-x64.tar.gz -C /usr/local/
[root@tomcat1 opt]# vim /etc/profile  # Declare environment variables 
# At the end of 
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_NAME}/bin:$PATH
[root@tomcat1 opt]# source /etc/profile

3.1.2 Deployment installation tomcat

[root@tomcat1 opt]# ls
apache-tomcat-9.0.16.tar.gz  jdk-8u91-linux-x64.tar.gz  rh
[root@tomcat1 opt]# tar zxf apache-tomcat-9.0.16.tar.gz -C /usr/local/
[root@tomcat1 opt]# cd /usr/local/
[root@tomcat1 local]# ls
apache-tomcat-9.0.16  bin  etc  games  include  jdk1.8.0_91  lib  lib64  libexec  sbin  share  src
[root@tomcat1 local]# mv apache-tomcat-9.0.16/ tomcat
[root@tomcat1 local]# cd tomcat/
[root@tomcat1 tomcat]# ls
bin           conf             lib      logs    README.md      RUNNING.txt  webapps
BUILDING.txt  CONTRIBUTING.md  LICENSE  NOTICE  RELEASE-NOTES  temp         work
[root@tomcat1 bin]# ls  # Set up the soft link between the start script and the close script in the following 
bootstrap.jar       ciphers.sh                    daemon.sh     setclasspath.bat  startup.sh            version.bat
catalina.bat        commons-daemon.jar            digest.bat    setclasspath.sh   tomcat-juli.jar       version.sh
catalina.sh         commons-daemon-native.tar.gz  digest.sh     shutdown.bat      tomcat-native.tar.gz
catalina-tasks.xml  configtest.bat                makebase.bat  shutdown.sh       tool-wrapper.bat
ciphers.bat         configtest.sh                 makebase.sh   startup.bat       tool-wrapper.sh
[root@tomcat1 bin]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin
[root@tomcat1 bin]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin

# Create site directories and files (web page )
[root@tomcat1 local]# mkdir -p /web/webapp1
[root@tomcat1 local]# cd /web/webapp1/
[root@tomcat1 webapp1]# vim index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
 <head>
    <title>JSP test1 page</title>  # Page title name 
 </head>
 <body>
    <% out.println("Welcome tomcat1 Web");%> # The content of the page is :welcome tomcat1 web  The only need is tomcat2 Part of the configuration change above ( Again, this is to verify the effect , The production environment is consistent ~)
 </body>
</html>
[root@tomcat1 webapp1]# vim /usr/local/tomcat/conf/server.xml # The configuration service file is in 149 Line add context Tag statement  
148       <Host name="localhost"  appBase="webapps"
149             unpackWARs="true" autoDeploy="true">
150          <Context docBase="/web/webapp1" path="" reloadable="false">
151          </Context>
[root@tomcat1 webapp1]# startup.sh # Opening service 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_91/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat1 webapp1]# netstat -ntap | grep 8080 # Check tomcat Whether the service is on 
tcp6       0      0 :::8080                 :::*                    LISTEN      2020/java 

[root@tomcat1 webapp1]# systemctl status firewalld.service  # Check the firewall settings 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since  Japan  2020-04-05 11:04:32 CST; 19min ago
   ......
[root@tomcat1 webapp1]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@tomcat1 webapp1]# firewall-cmd --reload
success

3.2 Deployment configuration nginx The server

3.2.1 Manual compilation and installation nginx service ( There's no need to say more here )

[root@nginx opt]# tar zxf nginx-1.12.0.tar.gz -C /usr/local/
[root@nginx opt]# yum install -y gcc gcc-c++ make zlib-devel pcre-devel

[root@nginx opt]# useradd -M -s /sbin/nologin nginx
[root@nginx opt]# cd /usr/local/nginx-1.12.0/
[root@nginx nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module
[root@nginx nginx-1.12.0]# make && make install
[root@nginx nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

3.3 Configuration verification to achieve load balancing

3.3.1upstream Module to achieve load balancing

[root@nginx nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
# stay nginx.conf Of gzip Write below tomcat The server pool ,tomcat-server Means a name , It can be understood as the domain name of the server 
#gzip  on;

    upstream tomcat-server {
                server 20.0.0.130:8080 weight=1;# According to the weighted round robin algorithm to schedule access to the backend tomcat The server 
                server 20.0.0.136:8080 weight=1;
                }
location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://tomcat-server; # Configure the proxy server 
        }
[root@nginx nginx-1.12.0]# nginx -t # Check the syntax of the configuration file 
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@nginx nginx-1.12.0]# nginx # Start the service 
[root@nginx nginx-1.12.0]# netstat -napt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      53804/nginx: master 
[root@nginx nginx-1.12.0]# firewall-cmd --zone=public --add-port=80/tcp --permanent
success
[root@nginx nginx-1.12.0]# firewall-cmd --reload
success

3.3.2 Verify load balancing

Access on the client nginx Server address , Then refresh once , The results are as follows :

 Build like this Nginx+Tomcat colony , Achieve load balancing and dynamic and static separation , Too easy !

 

 Build like this Nginx+Tomcat colony , Achieve load balancing and dynamic and static separation , Too easy !

 

Demonstration of dynamic separation implementation process

This time it's just for the purpose of separating the static and the dynamic , So just do the necessary demonstrations , use nginx Server and one tomcat The server can .

3.4 Simulate access to dynamic and static resources ( Not the same web page )

For the convenience of management, it is recommended to write one nginx Management scripts for services

[root@nginx nginx-1.12.2]# vim /etc/init.d/nginx  ## To write service The startup script 
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
    start)
        $PROG
        ;;
    stop)
        kill -s QUIT $(cat $PIDF)
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    reload)
        kill -s HUP $(cat $PIDF)
        ;;
    *)
                echo "Usage: $0 {start|stop|restart|reload}"
                exit 1
esac
exit 0
[root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx 
[root@nginx nginx-1.12.2]# chkconfig --add nginx

3.4.1 Temporarily comment on the original nginx Agent service configuration of

location / {
            root   html;
            index  index.html index.htm;
            #proxy_pass http://tomcat-server;
        }

3.4.2 Modify the default site directory file ( Display page )( Statement nginx As a request handler for static resource access )

[root@nginx html]# vim index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p><em>this is a static web page.</em></p> #em In italics 
</body>
</html>

Restart the service and access nginx The server , What you get is :

 Build like this Nginx+Tomcat colony , Achieve load balancing and dynamic and static separation , Too easy !

 

3.4.3 stay tomcat1 Write a jsp The dynamic page of

# Create a site directory demo, Write a jsp Script 
vim /usr/local/tomcat/webapps/demo/index.jsp 
<!DOCCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Dynamic pages </title>
</head>
<body>
<div> Dynamic pages 1</div>
</body>
</html>

 change server.xml
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Context docBase="/usr/local/tomcat/webapps/demo" path="" reloadable="false">
         </Context>
        <!--<Context docBase="/web/webapp1" path="" reloadable="false">
         </Context> -->

So now nginx The server needs location On the visit jsp Document carried out ip Configuration of jump access :

location ~.*.jsp$ {       ## matching jsp Page Jump proxy pool 
           proxy_pass http://tomcat-server;
           proxy_set_header Host $host;
        }

3.4.4 Test verification

 Build like this Nginx+Tomcat colony , Achieve load balancing and dynamic and static separation , Too easy !

 

3.5 For the same web Page dynamic access tomcat, Static resources from nginx Get on the

Use a picture as jsp file , It contains a jpg Format the picture from nginx Get on the server

The specific configuration is as follows

nginx On : Need to create demo Catalog (demo and tomcat The names of the directories on must be the same ) Deposit jpg

# First, cancel the above proxy comment , Because what I visited during the test was 20.0.0.128
server {
        listen       80;
        server_name  localhost;

        location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
            root html/demo;
            expires 30d;
        }
        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location ~.*.jsp$ {       ## matching jsp Page Jump proxy pool 
           proxy_pass http://tomcat-server;
           proxy_set_header Host $host;
        }
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://tomcat-server;
        }

tomcat:

jsp Add a picture link to the file :

[root@tomcat1 demo]# vim /usr/local/tomcat/webapps/demo/index.jsp 
<!DOCCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Dynamic pages </title>
</head>
<body>
<div> Dynamic pages 1</div><br>
<img src="cat.jpg"> # Added content 
</body>
</html>
~         

jsp resources : stay demo Under the table of contents

[root@tomcat1 demo]# ls
index.jsp

The picture resources are :html Under the table of contents

[root@nginx html]# ls
50x.html  demo  index.html
[root@nginx html]# cd demo/
[root@nginx demo]# ls
cat.jpg
[root@nginx demo]# 

Now restart nginx Service access 20.0.0.128

The first time I visited is the text + picture , The second time, because there is no relevant configuration on the second server, the access content is still the same as the previous load balancing content .

 Build like this Nginx+Tomcat colony , Achieve load balancing and dynamic and static separation , Too easy !

 

 Build like this Nginx+Tomcat colony , Achieve load balancing and dynamic and static separation , Too easy !

 

Simple summary

In fact, combined with this practice , You can understand how to combine static and dynamic separation with load balancing , To build nginx+tomcat Cluster service . If we finally realize the dynamic resources represented by dynamic text , Plus the static resources represented by this cute kitten ( Understand the separation of motion and stillness ), Combined with the previous load balancing improvement tomcat2 The server configuration can realize load balancing according to the algorithm .

All in all , We need to be very familiar with the configuration file , Understand its functional modules , Finally, we need to understand how to perform access jump based on various modules or instruction contexts , The matching relationship needs to be clear ( logical relationship ).

official account Java Architects Alliance , The background to reply Java There's a surprise

版权声明
本文为[Java architects Alliance]所创,转载请带上原文链接,感谢

Scroll to Top