Nginx+Tomcat multi-instance and load balancing configuration

The reverse proxy load balancing function of NGINx is used to realize the Load balancing of Tomcat WEB service with tomcat multi-instance at the back end

01

Install the nginx service

Install the required PCRE libraries

The tar ZXF pcre – 8.38. Tar. Gz

CD pcre – 8.38

./configure 

make && make install

cd .. /

Compile and install the Nginx service

The tar ZXF nginx – 1.11.3. Tar. Gz

CD nginx – 1.11.3

useradd nginx -s /sbin/nologin -M

yum install openssl openssl-devel -y

/configure –user=nginx –group=nginx –prefix=/application/nginx-1.11.3 –with-http_ssl_module –with-http_stub_status_module

make && make install

Nginx service for the relevant introduction and compilation parameters can refer to the public number in front of the article, the detailed configuration of the service are involved

02

Installing the JDK Environment

Tomcat requires the JDK environment. Therefore, install the JDK environment before installing Tomcat. Download the CORRESPONDING JDK software package first

tar xf jdk-8u60-linux-x64.tar.gz -C /application/

Unzip to the specified directory without compiling

Ln -s/application/jdk1.8.0 _60 / application/JDK

## Create soft links

sed -i.ori ‘$a export JAVA_HOME=/application/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar’ /etc/profile           

Configure environment variables

source /etc/profile             

## Environment variables that take effect

java -version                      

Check whether the JDK environment is installed successfully

03    

Install and configure Tomcat multiple instances

Tomcat multi-instance configuration is also quite simple, download the corresponding version of the software, decompress to use

Tar xf apache-tomcat-8.0.27.tar.gz -c /application/

Ln -s/application/apache tomcat – 8.0.27 / application/tomcat

echo ‘export TOMCAT_HOME=/application/tomcat’>>/etc/profile

The multi-instance installation configuration is as follows

Cp – a apache tomcat – 8.0.27 / application/tomcat8_1

Cp – a apache tomcat – 8.0.27 / application/tomcat8_2

Switch to the related directory and modify the corresponding configuration file

(The configuration files of both instances are changed in the same way, only the port and the site directory need to be changed)

cd /application/tomcat8_2/conf/

diff /application/tomcat/conf/server.xml ./server.xml

22c22

< <Server port=”8005″ shutdown=”SHUTDOWN”>

> <Server port=”8012″ shutdown=”SHUTDOWN”>

69c69

< < the Connector port = “8080” protocol = “HTTP / 1.1”

> < Connector port = “8082” protocol = “HTTP / 1.1”

123c123

<       <Host name=”localhost”  appBase=”webapps”

>       <Host name=”localhost”  appBase=”/web/www/bbs”

Creating a site Directory

mkdir /web/www/{www,bbs}/ROOT -p

echo “hello”>/web/www/www/ROOT/index.jsp

echo “world”>/web/www/bbs/ROOT/index.html

Start multi-instance

/application/tomcat8_1/bin/startup.sh

/application/tomcat8_2/bin/startup.sh

04

Configure nginx

vim /application/nginx/conf/nginx.conf

#user  nobody;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

#log_format  main  ‘$remote_addr – $remote_user [$time_local] “$request” ‘

    #                  ‘$status $body_bytes_sent “$http_referer” ‘

    #                  ‘”$http_user_agent” “$http_x_forwarded_for”‘;

    #access_log  logs/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    #gzip  on;

upstream web_pools {

Server 127.0.0.1:8081;

Server 127.0.0.1:8082;

        }

    server {

        listen       80;

        server_name  localhost;

        location / {

            root   html;

            index  index.jsp index.html index.htm;

            proxy_pass http://web_pools;

        }

     }

}

Check syntax and start nginx service

/application/nginx/sbin/nginx -t

/application/nginx/sbin/nginx

05

Test the load balancing effect

for i in `echo {1.. 6} `; Do the curl 192.168.1.129; done

hello

world

hello

world

hello

world

It indicates that load balancing has been configured