Nginx+Tomcat load balancing cluster + reverse agent

Keywords: Linux Nginx Tomcat vim JSP

Experimental environment

Nginx server (192.168.13.177)
Tomcat1 server (192.168.13.151)
Tomcat2 server (192.168.13.178)
client tester

I. load balancing

1. Install Tomcat service on Tomcat1 and Tomcat2

[root@tomcat1 ~]# systemctl stop firewalld.service  ##Turn off firewall
[root@tomcat1 ~]# mkdir /abc
[root@tomcat1 ~]# mount.cifs //192.168.100.3/LNMP-C7 /abc/
[root@tomcat1 ~]# cd /abc/tomcat/
[root@tomcat1 tomcat]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/  ##Decompression JDK
[root@tomcat1 tomcat]# vim /etc/profile   ##Configure environment variables
##Join the last line of big G
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@tomcat1 tomcat]# source /etc/profile  ##Refresh profile
[root@tomcat1 tomcat]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/   ##decompression
[root@tomcat1 tomcat]# cd /usr/local/
[root@tomcat1 local]# mv apache-tomcat-8.5.16/ tomcat
[root@tomcat1 local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/   
##Make startup and shutdown scripts easy for system identification
[root@tomcat1 local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
[root@tomcat1 local]# mkdir -p /web/webapp1  ##Create site
[root@tomcat1 local]# vim /web/webapp1/index.jsp   ##Write jsp Web page content
##jsp Web page content
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
 <head>
    <title>JSP test1 page</title>
 </head>
 <body>
    <% out.println("Welcome KGC Web");%>    ##Output information
 </body>
</html>
[root@tomcat1 local]# vim /usr/local/tomcat/conf/server.xml  ##Modify Tomcat configuration file
            <Host name="localhost"  appBase="webapps"
                        unpackWARs="true" autoDeploy="true">
                <Context docBase="/web/webapp1" path="" reloadable="false">  ##Add site information
                </Context>
[root@tomcat1 ~]# startup.sh   ##Startup service
##The content of the webpage on Tomcat is accp. Other configurations are the same

2. Install Nginx on the Nginx server

[root@nginx ~]# systemctl stop firewalld.service    ##Turn off firewall
[root@nginx ~]# setenforce 0
[root@nginx ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y  ##Necessary components of installation environment
[root@nginx ~]# mkdir /abc
[root@nginx ~]# mount.cifs //192.168.100.3/LNMP-C7 /abc / × mount
Password for root@//192.168.100.3/LNMP-C7:  
[root@nginx ~]# cd /abc/
[root@nginx abc]# tar zxvf nginx-1.12.2.tar.gz -C /usr/local/  ##decompression
[root@nginx abc]# useradd -M -s /sbin/nologin nginx   ##Create system user
[root@nginx abc]# cd /usr/local/nginx-1.12.2/
[root@nginx nginx-1.12.2]# ./configure \   ##To 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.2]# make && make install   ##Compilation and installation 

3. Modify the Nginx configuration file

[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
 #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;

        upstream tomcat-server {                             #Add address pool
                            server 192.168.13.151:8080 weight=1;
                            server 192.168.13.178:8080 weight=1;    
                        }

        server {
                listen       80;
.....ellipsis
location / {
                        root   html;
                        index  index.html index.htm;
                        proxy_pass http://Tomcat server; add agent, call server address pool
                }
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/  ##Easy system identification
[root@nginx nginx-1.12.2]# nginx   ##Opening service

4. Use client tester to access nginx proxy server


2. Separation of dynamic and static

1. Modify the configuration file on Nginx

[root@nginx nginx-1.12.2]# vim /etc/init.d/nginx  ##Write service 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
[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
server {
...ellipsis...
     location ~.*.jsp$ {       ##Match jsp page Jump proxy pool
         proxy_pass http://tomcat-server;
         proxy_set_header Host $host;
     }
            location / {
        root   html/test;   ##Modify site
        index  index.html index.htm;
        proxy_pass http://tomcat-server;
     }
[root@nginx nginx-1.12.2]# vim /usr/local/nginx/html/index.html   ##Write a static web page
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Static page</title>
<style>
body {
    width: 35em;
    margin: 0 auto;
    font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
    <h1>Static page</h1>
    <p>This is a static page</p>
</body>
</html>
[root@nginx nginx-1.12.2]# service nginx stop    ##Turn off turn on Service
[root@nginx nginx-1.12.2]# service nginx start

2. Create jsp dynamic page on Tomcat1 and Tomcat2

[root@tomcat1 ~]# mkdir /usr/local/tomcat/webapps/test
[root@tomcat1 ~]# vim /usr/local/tomcat/webapps/test/index.jsp
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>   ##Initial declaration
<!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 page</title>
</head>
<body>
<div>Dynamic page 1</div>
</body>
</html>
##Change to dynamic page 22 on Tomcat2
//Access static
http://192.168.13.177/  
//Access dynamics  
http://192.168.13.177/test/index.jsp

Nginx deals with static pictures, Tomcat deals with dynamic pages

1. Add pictures on Tomcat1 and Tomcat2

[root@tomcat1 ~]# vim /usr/local/tomcat/webapps/test/index.jsp
<body>
    <div>Dynamic page</div><br><img src="11.jpg"> //Add page picture 
</body>
[root@tomcat01 local]# vim /usr/local/tomcat/conf/server.xml 
#Add the following entry under line 149,
<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="false">
</Context>
[root@tomcat1 test]# shutdown.sh    ##Close restart
[root@tomcat1 test]# startup.sh

2. Modify the configuration file on Nginx

[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf
location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ { 
     root html/test;
     expires 30d;
}
[root@nginx nginx-1.12.2]# mkdir /usr/local/nginx/html/test
[root@nginx nginx-1.12.2]# cp /abc/11.jpg /usr/local/nginx/html/test/
#Restart service
[root@nginx html]# service nginx restart

3. Use client test


Thank you for reading!!

Posted by lily on Sun, 08 Dec 2019 15:05:40 -0800