preface

This article mainly introduces how to build a load balancing server using Nginx + Tomcat on CentOS from scratch. Learn the basic usage of Nginx and Knowledge related to Tomcat during the construction process, and further understand the operation principle of the combination of the two.

Nginx installation

Install Nginx on CentOS using source code compilation:

  1. Install operating environment

    • Nginx is C language development, compilation depends on the GCC environment, so if you do not have GCC first need to install:
    yum install -y gcc-c++
    Copy the code
    • Install Nginx dependency libraries
    The rewrite module requires PCRE(Perl Compatible Regular Expressions), the Pcre-devel library
    yum install -y pcre pcre-devel 
    The gzip module requires the Zlib library
    yum install -y zlib
    The OpenSSL library is required for SSL functionality
    yum install -y openssl openssl-devel
    
    Copy the code
  2. Download Ngnix

    Download the latest stable version from Ngnix official website, currently 1.12.2

    If you have not installed wget, run the install command first
    yum install -y wget
    # Use the wget command to downloadWget -c https://nginx.org/download/nginx-1.12.2.tar.gzUnzip the fileThe tar - ZXVF nginx - 1.12.2. Tar. GzGo to the decompressed directory
    cdNginx - 1.12.2Use the default configuration
    ./configure
    Copy the code
  3. Compile and install Nginx

    Compiling and installing is as simple as executing two commands

    # compiler
    make
    # installation
    make install
    The default installation directory is /usr/local/nginx
    # You can find whereis nginx[root @ localhost nginx - 1.12.2]# whereis nginx
    nginx: /usr/local/nginx
    Copy the code
  4. Start the test

    After the installation of Ngnix is complete, the test can be started to check whether it can run normally

    Enter the ngnix executable file directory
    cd /usr/local/ngnix/sbin
    # start
    #. / nginx start
    #./nginx -s stop stop the nginx process.
    #./nginx -s quit (wait for the nginx process to complete the task and then stop)
    #./nginx -s reload, applicable after configuration file modification
    ./nginx
    You can run the ps command to query the ngnix process
    ps aux|grep nginx
    # Visit localhost:80 to test whether the Ngnix is running properly, return to the nginx welcome page to indicate that it is running properly
    [root@localhost sbin]# curl localhost:80<! 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>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/>
    Commercial support is available at
    <a href="http://nginx.com/">nginx.com</a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    Copy the code

Configure Tomcat multi-instance deployment

Tomcat single-instance deployment, that is, when a Tomcat server is running, there is no load balancing. Therefore, the first thing we need to do is to implement Tomcat multi-instance deployment, where our same application is deployed to run on multiple Tomcat servers simultaneously. The main steps are as follows:

  1. Install the JDK (skip step 1 if it’s already installed on your machine)
# Download and unzip JDK8wget http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c3b165be4555a1fa6e98c45e0808/jdk-8u161-linux-x64.tar.gz?AuthPa ram=1520068673_6f545cf32470b83658219011266e65b8Configure Java environment variables
vi /etc/profile
Add the following to the end of the file
export JAVA_HOME=/usr/local/jdk1.8.0_161export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
Reload /etc/profile after saving
source /etc/profile
Verify that the configuration is correct
[root@localhost local]# java -version
java version "1.8.0 comes with _161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
Copy the code
  1. Download Tomcat
Download Tomcat 9Wget HTTP: / / http://mirrors.hust.edu.cn/apache/tomcat/tomcat-9/v9.0.5/bin/apache-tomcat-9.0.5.tar.gz# decompressionTar ZXVF - apache tomcat - 9.0.5. Tar. GzPrepare for the next step
Rename to tomcat-homeMv apache tomcat - 9.0.5 tomcat - home# copy the second copy and rename it tomcat-8080
cp -R tomcat-home tomcat-8080
Duplicate the third copy and rename it tomcat-9090
cp -R tomcat-home tomcat-9090
Copy the code
  1. Separate directory

    I’ll start with two of the more important Tomcat concepts (and usually two system variables) CATALINA_HOME and CATALINA_BASE

    • CATALINA_HOME: System variable pointing to the Tomcat installation path (installation directory)
    • CATALINA_BASE: system variable pointing to the active configuration path (working directory)

    By setting these two variables, you can separate the installation directory of Tomcat from the working directory to implement tomcat multi-instance deployment. Here is the basic directory structure of Tomcat and what it does.

    directory Introduction to the
    bin Save script files, such as startup.sh and shutdown.sh
    conf It houses configuration files, most importantly server.xml, which is the main tomcat configuration file
    lib Dependency packages required for Tomcat operation
    log Storing log files
    temp Store temporary files generated at run time
    webapps The default directory for web applications
    work The main repository is servlets generated by JSP files (Java files and eventually compiled class files)

    The bin and lib directories in CATALINA_HOME are required. CATALINA_BASE can contain all directories, but bin and lib are not required. Bin and lib in CATALINA_HOME are used by default. So we can deploy multiple instances of CATALINA_HOME with multiple CatalINA_Bases, which has the benefit of being easy to manage and upgrade Tomcat. In the previous step, we have copied three Tomcats. they do the following:

    directory role
    tomcat-home As CATALINA_HOME, you only need to keep the bin and lib folders
    tomcat-8080 As CATALINA_BASE, you need to keep folders other than bin and lib, using port 8080
    tomcat-9090 The same as tomcat-8080, use port 9090

    Separate directories based on CATALINA_HOME and CATALINA_BASE

    After the table above is sorted, the directory structure is as follows:
    [root@localhost tomcat-home]# ls
    bin  lib  LICENSE  NOTICE  RELEASE-NOTES  RUNNING.txt
    [root@localhost tomcat-8080]# ls
    conf  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
    [root@localhost tomcat-9090]# ls
    conf  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
    Copy the code
  2. Modify the Tomcat configuration file

    This step is mainly to modify the configuration of server. XML port. In server. XML, four listening ports are configured, respectively:

    • Server port(default 8005): listens for the shutdown command to shutdown tomcat
    • Connector port(default 8080) : Listens for HTTP requests
    • AJP Connector Port (default 8009) : Listens for AJP requests
    • RedirectPort (default 8443) : A redirection Port that appears in the Connector configuration and redirects HTTPS requests to the Port specified by the Redirect Port if the Connector only supports normal NON-SSL HTTP requests.

    Now that we know what each port is for, we can start modifying server.xml. If we don’t use AJP requests, we just need to make sure that the Server port and Connector port in the multi-instance are different.

    Keep the default configuration for tomcat-8080
    /usr/local/tomcat-9090/conf/server.xml. <Server port="9005" shutdown="SHUTDOWN">... <Connector port="9090" protocol="HTTP / 1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    ...
    Copy the code
  3. Write a script

    Sh and shutdown.sh are in the tomcat-home/bin directory, and we need to start 8080 and 9090 instances, so we need to write a script. Modify CATALINA_BASE to operate on both instances separately.

  • Start script start.sh:

    #! /bin/sh
    CUR_DIR=`dirname $BASH_SOURCE`
    export CATALINA_BASE=`readlink -f $CUR_DIR`
    export CATALINA_HOME="/usr/local/tomcat-home"
    
    if [ -f $CATALINA_HOME/bin/startup.sh ]; then
            $CATALINA_HOME/bin/startup.sh
    else
            echo "$CATALINA_HOME/bin/startup.sh not exist"
    fi
    
    Copy the code
  • Stop script stop.sh:

    #! /bin/sh
    CUR_DIR=`dirname $BASH_SOURCE`
    export CATALINA_BASE=`readlink -f $CUR_DIR`
    export CATALINA_HOME="/usr/local/tomcat-home"
    
    if [ -f $CATALINA_HOME/bin/shutdown.sh ]; then
           $CATALINA_HOME/bin/shutdown.sh
    else
           echo "$CATALINA_HOME/bin/shutdown.sh not exist"
    fi
    
    Copy the code

    Put these two script files in the root directory of tomcat-8080 and tomcat-9090

    [root@localhost tomcat-8080]# ls
    conf     logs    RELEASE-NOTES  start.sh  temp     work
    LICENSE  NOTICE  RUNNING.txt    stop.sh   webapps
    
    Start tomcat for port 8080
    [root@localhost tomcat-8080]# ./start.sh 
    Using CATALINA_BASE:   /usr/local/tomcat-8080
    Using CATALINA_HOME:   /usr/local/tomcat-home
    Using CATALINA_TMPDIR: /usr/local/tomcat-8080/temp
    Using JRE_HOME:        /usr/local/ jdk1.8.0 _161 Using the CLASSPATH: / usr /local/tomcat-home/bin/bootstrap.jar:/usr/local/tomcat-home/bin/tomcat-juli.jar
    Tomcat started.
    # stop
    [root@localhost tomcat-8080]# ./stop.sh 
    Using CATALINA_BASE:   /usr/local/tomcat-8080
    Using CATALINA_HOME:   /usr/local/tomcat-home
    Using CATALINA_TMPDIR: /usr/local/tomcat-8080/temp
    Using JRE_HOME:        /usr/local/ jdk1.8.0 _161 Using the CLASSPATH: / usr /local/tomcat-home/bin/bootstrap.jar:/usr/local/tomcat-home/bin/tomcat-juli.jar
    
    Copy the code
  1. Tests whether both Tomcats are running properly at the same time

    Create an index. HTML file in /usr/local/tomcat-8080/webapps/ROOT and /usr/local/tomcat-9090/webapps/ROOT. The contents are “tomcat-8080” and “tomcat-9090” so that we can distinguish them. Start two instances with start.sh.

    Access port 9090 to obtain data from 9090
    [root@localhost tomcat-9090]# curl localhost:9090
    <h1> tomcat-9090 </h1>
    Access port 8080 and obtain data from port 8080
    [root@localhost tomcat-8080]# curl localhost:8080
    <h1> tomcat-8080 </h1>
    Copy the code

Nginx is combined with Tomcat

The requirements for load balancing combined with Ngnix and Tomcat are as follows: the user accesses port 8888 of the server. After receiving the request, Ngnix forwards it to port 8080 or Port 9090 for Tomcat to process. The same application is deployed on both Tomcats, which enables load balancing and allows both Tomcats to process user requests at the same time. Here we take localHost as an example to configure Ngnix

This section uses localhost as an example. You can replace localhost with the domain name in the configuration file

Enter the directory of the ngnix profile
The default configuration file is ngnix.conf
/usr/local/nginx/conf
Localhost. conf = localhost.conf = localhost.confUpstream {server 127.0.0.1:8080; Server 127.0.0.1:9090; } server { listen 8888; server_name localhost; index index.html index.htm index.jsp index.php; location / { proxy_pass http://localhost; proxy_set_header X-Real-IP$remote_addr;
                        add_header X-Slave $upstream_addr; }}Localhost. conf will then be introduced in nginx.conf. http { ...# introduction localhost. Conf;
    include /usr/local/nginx/conf/localhost.conf; . }.../nginx -t to check whether the configuration file is correct
cd /usr/local/nginx/sbin/
./nginx -t
If the following message is displayed, the configuration is correct. If not, check 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

Reload the ngnix configuration file
./nginx -s reload
Copy the code

At this point, the balancing server for Nginx + Tomcat has been set up, and you can now visit localhost:8888 to see that both Tomcat servers can handle requests from port 8888

[root@localhost sbin]# curl localhost:8888
<h1> tomcat-8080 </h1>
[root@localhost sbin]# curl localhost:8888
<h1> tomcat-9090 </h1>
Copy the code

Load Balancing Policy

We used the default Nginx load balancing policy above. We can also configure other policies based on our own requirements. Nginx provides the following policies:

  1. Polling (default)

    Each request is allocated to a different backend server one by one in chronological order, and if the backend server fails, it is automatically ignored.

    Upstream {server 127.0.0.1:8080; Server 127.0.0.1:9090; }Copy the code
  2. The weight

    The weight is assigned to different servers according to the configuration. This method is applicable to the situation where there is a difference in server performance. High weight can be assigned to each high-performance server

    Upstream {server 127.0.0.1:8080 weight=1; Server 127.0.0.1:9090 weight = 3; }Copy the code
  3. The IP ip_hash

    Each request is allocated according to the hash result of the access IP, so that each visitor has a fixed access to the back-end server, which can solve the session problem.

    upstream localhost {
        ip_hash; 
        server 127.0.0.1:8080;
        server 127.0.0.1:9090;
    }
    Copy the code
  4. The minimum connection

    Assign requests to the server with the fewest current connections

    upstream localhost {
        least_conn; 
        server 127.0.0.1:8080;
        server 127.0.0.1:9090;
    }
    Copy the code

conclusion

This section describes how to use CATALINA_HOME and CATALINA_BASE to implement Tomcat multi-instance deployment. This section describes how to use CATALINA_HOME and CATALINA_BASE to implement Tomcat multi-instance deployment. It is easy to implement load balancing with Nginx on the basis of Tomcat multi-instance deployment.