Nginx + Tomcat achieve load balancing on 7 CentOS

  • Outline

Typically, a Tomcat site due to possible single point of failure and can not cope with too many complex and diverse customer requests and other issues, can not be used alone in a production environment, we need a more robust solution to improve the web site architecture.

nginx is an excellent http server software, which can support up to 50,000 concurrent response to the number of connections, has a strong ability to deal with static resources, stable, and memory, CPU and other system resource consumption is very low. There are many large sites use nginx as a reverse proxy and load balancing of the back-end web application.

This case we will have nginx as a load balancer, reverse proxy, tomcat as a node server.

  • Case environment
Host computer IP addresses
nginx server 192.168.177.132
tomcat server 1 192.168.177.145
tomcat server 2 192.168.177.135

tomcat server

Installation jdk (1.8 version)
The Apache-Tomcat tar XF #-8.5.23.tar.gz / opt 
# tar the JDK XF-8u144-Linux-x64.tar.gz / opt 
// extract the installation package
The Apache-Tomcat-mv # 8.5.23 / / usr / local / tomcat8 
# mv jdk1.8.0_144 / / usr / local / the Java 
// manageable
# vim /etc/profile   //设置环境变量

export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib

# source /etc/profile
# Java -version // Check information java version 
   java Version "1.8.0_131" 
   the OpenJDK Runtime Environment (Build 1.8.0_131-B12) 
   the OpenJDK-64-Bit Server VM (Build 25.131-B12, the MODE Mixed)

Nginx + Tomcat achieve load balancing on 7 CentOS

Tomcat installation
-S /usr/local/tomcat8/bin/startup.sh LN # / usr / bin / tomcatup 
# /usr/local/tomcat8/bin/shutdown.sh LN -s / usr / bin / tomcatdown 
// in / usr easy identification command / bin
# Tomcatup // open service 
# netstat -anpt | grep 8080 // Check whether to open ports tomcat 
# http://192.168.177.145:8080/ // default test page to test whether the normal display (node) 
# vim / usr / local / tomcat8 / webapps / ROOT / index.jsp // modify the default web content (site) 
    <h1> the this IS 145 !!! </ h1>

Nginx + Tomcat achieve load balancing on 7 CentOS
Nginx + Tomcat achieve load balancing on 7 CentOS
Nginx + Tomcat achieve load balancing on 7 CentOS
Nginx + Tomcat achieve load balancing on 7 CentOS
Nginx + Tomcat achieve load balancing on 7 CentOS
Nginx + Tomcat achieve load balancing on 7 CentOS
== == Note: Two tomcat server is configured exactly the same

nginx server

Manually compile and install
The tar -C # / opt-1.12.0.tar.gz zxvf Nginx 
# yum the install PCRE devel zlib-devel-GCC GCC ++ -Y-C package installation environment // 
# useradd -M -s / sbin / nologin nginx // Create nginx user management 
# cd /opt/nginx-1.12.0/ 
    ./configure \ 
    --prefix = / usr / local / nginx \ 
    --user = nginx \ // specified user running 
    --group = nginx \ // group designated to run 
    --with-http_stub_status_module // enable state statistics 
# && the make the make install 
# LN -s / usr / local / nginx / sbin / nginx / usr / local / sbin / // system to recognize the command 
# nginx -t // validate the syntax
Write scripts to facilitate service management
# cd /etc/init.d/
  vim nginx

#!/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
# chmod +x /etc/init.d/nginx   //执行权限
# chkconfig --add nginx
# chkconfig --level 35 nginx on
# service nginx stop
# netstat -ntap | grep 80
# service nginx start
# netstat -ntap | grep 80
Modify the configuration file
Vim /usr/local/nginx/conf/nginx.conf # 

  #keepalive_timeout 0; 
    keepalive_timeout 65; 

    #gzip ON; 
     added: 
    upstream tomcat_server {# Add 
              server 192.168.177.145:8080 weight = 1; // the same weight, the page will be repeated the following two pages back and forth between the switching 
              Server 192.168.177.135:8080 weight =. 1;     
            } 

    Server { 
        the listen 80; 
..... omitted 
LOCATION / { 
            # the root HTML; 
            # index index.html index.htm; 
            proxy_pass HTTP: / / tomcat_server; // add a line to the default site nginx proxy to set a good tomcat_server server load balancing method by proxy_pass    
        } 
# service enabled nginx nginx restart // // service

Nginx + Tomcat achieve load balancing on 7 CentOS
Nginx + Tomcat achieve load balancing on 7 CentOS
Nginx + Tomcat achieve load balancing on 7 CentOS

Guess you like

Origin www.linuxidc.com/Linux/2019-09/160681.htm