Nginx+Tomcat Load Balancing Cluster Setup

Keywords: Linux Nginx Tomcat vim JSP

Introduction to Tomcat

  • Tomcat was originally developed by Sun's software architect James Duncan Davidson
  • The directories and files below the installation path after Tomcat is installed are important files for using or configuring Tomcat

Tomcat Important Catalog

  • bin: Stores Tomcat scripts to start and close
  • conf: Store different Tomcat configuration files
  • doc: Store Tomcat documents
  • Lib/Japanese ser/common: Store library files needed for Tomcat to run
  • logs: Stores the LOG file for Tomcat execution
  • src: Stores Tomcat's source code
  • webapps:Tomcat's main Web publishing directory
  • work: Stores the class file generated by jsp compilation

Nginx Application

  • Nginx is a very good HTTP server software
    • Support responses up to 50,000 concurrent connections
    • Powerful static resource handling
    • Stable operation
    • Very low consumption of system resources such as memory, CPU, etc.
  • Currently, many large websites use Nginx servers as reverse proxies and load balancers for back-end Web site programs to improve the load concurrency of the entire site

Nginx Load Balancing Implementation Principle

  • Load balancing with Nginx is achieved through reverse proxy
  • Reverse Proxy Principle

Main parameters for 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 server processing that forwards access requests to the back-end server pool

Nginx+Tomcat Load Balancing Cluster Experiment

Experimental environment

Nginx server IP address: 192.168.80.177
 Tomcat1 server IP address: 192.168.80.151
 Tomcat2 server IP address: 192.168.80.178
 client tester

Set up load balancing

  • Install and configure Tomcat services on Tomcat1, Tomcat2 servers (the Tomcat2 web page content is the same as other accp configurations)
    [root@tomcat1 ~]# systemctl stop firewalld.service //close firewall
    [root@tomcat1 ~]# mkdir /abc
    [root@tomcat1 ~]# mount.cifs //192.168.100.8/LNMP-C7 /abc/
    [root@tomcat1 ~]# cd /abc/tomcat/
    [root@tomcat1 tomcat]# Tar zxvf jdk-8u91-linux-x64.tar.gz-C/usr/local/ //decompress JDK
    [root@tomcat1 tomcat]# Vim/etc/profile//Configure environment variables
    ...
    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//Start and close scripts 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
    <%@ 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 //Start the service
  • Install and configure the Nginx service on the Nginx server

    [root@nginx ~]# systemctl stop firewalld.service    ##Close Firewall
    [root@nginx ~]# setenforce 0
    [root@nginx ~]# yum install pcre-devel zlib-devel gcc gcc-c++ make -y  ##Install Environment Requirements
    [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   ##Compile Installation 
    [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.80.151:8080 weight=1;
                            server 192.168.80.178:8080 weight=1;    
                        }
    
        server {
                listen       80;
    .....ellipsis
    location / {
                        root   html;
                        index  index.html index.htm;
                        proxy_pass http://tomcat-server; #Add proxy, 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   ##Open Service
  • Using client tester to access nginx proxy server

Dynamic and Static Separation

  • Modify the Nginx configuration file on the Nginx server
    [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 static web pages
    <!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 Open Service
    [root@nginx nginx-1.12.2]# service nginx start
  • Create jsp dynamic pages on Tomcat1, Tomcat2 servers
    [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" %> 
    <!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>
    //Modify to Dynamic Page 2 on Tomcat2
    //Access Static
    http://192.168.13.177/  
    //Access Dynamics  
    http://192.168.13.177/test/index.jsp
  • Configure Nginx to process static pictures, Tomcat to process dynamic pages
    • Configure on two Tomcat servers
      [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 
      //Append 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
    • Modify the Nginx configuration file on the Nginx server
      [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/
      [root@nginx html]# service nginx restart //restart service
  • Use client testing

Posted by ATLien on Fri, 20 Dec 2019 12:59:55 -0800