Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)

Nginx application

Nginx is a very good HTTP server software

  • Support response up to 50,000 concurrent connections
  • It has a strong ability to deal with static resources
  • run smoothly
  • Memory, CPU and other system resource consumption is very low

At present, many large sites use Nginx server as a backend web application of a reverse proxy and load balancers to lift the load concurrency entire site

Nginx load balancing implementation principle

The main parameters to configure Nginx reverse proxy

upstream Service pool name {}

Configuring backend server pools to provide data in response to

_pass proxy_ HTTP: // Service pool name

Configured to forward the request to access the back-end server pool of server processes

Nginx handling the static advantages

  • Nginx processing efficiency is much higher than static pages Tomcat handling capacity
  • If the requested amount Tomcat is 1000 times the amount requested for 6000 Nginx
  • Tomcat throughput per second is 0.6M, 3.6M per second throughput for Nginx
  • Nginx ability to deal with static resources is six times the Tomcat process, the advantage is evident

Static and dynamic separation principle

The server receives a request from a client, both static resources but also dynamic resource

Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)

lab environment:

Nginx服务器:192.168.52.135
Tomcat服务器1:192.168.52.134
Tomcat服务器2:192.168.52.150

1, the toolkit desired share out the experiment from the host

Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)

Load balancing configurations:

First, build a Tomcat server 1

1, the installation jdk

[root@tomcat1 ~]# mkdir /mnt/tools   //创建挂载目录
[root@tomcat1 ~]# mount.cifs //192.168.100.100/tools /mnt/tools/   //挂载共享工具包
Password for root@//192.168.100.100/tools:  
[root@tomcat1 ~]# cd /mnt/tools/tomcat/   //切换到挂载目录
[root@tomcat1 tomcat]# ls
12D18CFCD6599AFF0445766ACC4CA231C5025773.torrent  apache-jmeter-5.1.zip        jdk-8u201-linux-x64.rpm
apache-jmeter-5.1                                 apache-tomcat-9.0.16.tar.gz  tomcat优化压测.jmx
[root@tomcat1 tomcat]#
[root@tomcat1 tomcat]# rpm -ivh jdk-8u201-linux-x64.rpm   //直接安装rpm包

2, configure the environment variables

[root@tomcat1 tomcat]# cd /usr/java/jdk1.8.0_201-amd64/   //切换目录
[root@tomcat1 jdk1.8.0_201-amd64]# vim /etc/profile   //修改系统环境变量文件
##文件末尾添加环境变量
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
[root@tomcat1 jdk1.8.0_201-amd64]# source /etc/profile   //重新加载配置文件
[root@tomcat1 jdk1.8.0_201-amd64]# java -version   //查看版本
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
[root@tomcat1 jdk1.8.0_201-amd64]# 

3, configure Tomcat

[root@tomcat1 jdk1.8.0_201-amd64]# cd /mnt/tools/tomcat/   //切换到挂载目录
[root@tomcat1 tomcat]# tar zxf apache-tomcat-9.0.16.tar.gz -C /opt/   //解压到“/opt/”目录
[root@tomcat1 tomcat]# mv /opt/apache-tomcat-9.0.16/ /usr/local/tomcat9   //移动到“/usr/local/”目录并重命名
[root@tomcat1 bin]# ln -s /usr/local/tomcat9/bin/startup.sh /usr/bin/   //建立启动脚本软链接
[root@tomcat1 bin]# ln -s /usr/local/tomcat9/bin/shutdown.sh /usr/bin/   //建立关闭脚本软链接
[root@tomcat1 bin]#
[root@tomcat1 bin]# startup.sh    //开启Tomcat服务
Using CATALINA_BASE:   /usr/local/tomcat9
Using CATALINA_HOME:   /usr/local/tomcat9
Using CATALINA_TMPDIR: /usr/local/tomcat9/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat1 bin]#
[root@tomcat1 bin]# netstat -ntap | grep 8080   //查看端口
tcp6       0      0 :::8080                 :::*                    LISTEN      19488/java          
[root@tomcat1 bin]# 
[root@tomcat1 bin]# systemctl stop firewalld.service    //关闭防火墙
[root@tomcat1 bin]# setenforce 0   //关闭增强性安全功能
[root@tomcat1 bin]#

4, test access

Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)

5, add sites and files Home

[root@tomcat1 bin]# mkdir -pv /web/webapp1   //创建站点目录
mkdir: 已创建目录 "/web"
mkdir: 已创建目录 "/web/webapp1"
[root@tomcat1 bin]# vim /web/webapp1/index.jsp   //添加首页文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
 <head>
  <title>JSP test1 page</title>
 </head>
 <body>
  <% out.println("Welcome to test site,http://www.test1.com");%>
 </body>
</html>

6, modify Tomcat configuration files

[root@tomcat1 bin]# cd ../conf/   //切换目录
[root@tomcat1 conf]# vim server.xml   //修改配置文件
    <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
                        ##在Host标签里,添加下面内容
        <Context docBase="/web/webapp1" path="" reloadable="false">
        </Context>
[root@tomcat1 conf]# shutdown.sh    //关闭服务
Using CATALINA_BASE:   /usr/local/tomcat9
Using CATALINA_HOME:   /usr/local/tomcat9
Using CATALINA_TMPDIR: /usr/local/tomcat9/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar
[root@tomcat1 conf]# startup.sh    //开启服务
Using CATALINA_BASE:   /usr/local/tomcat9
Using CATALINA_HOME:   /usr/local/tomcat9
Using CATALINA_TMPDIR: /usr/local/tomcat9/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat1 conf]# 

7, visit the Web site testing

Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)

Second, build a Tomcat server 2

1, the installation jdk

[root@tomcat2 ~]# mkdir /mnt/tools   //创建挂载目录
[root@tomcat2 ~]# mount.cifs //192.168.100.100/tools /mnt/tools/   //挂载共享工具包
Password for root@//192.168.100.100/tools:  
[root@tomcat2 ~]# cd /mnt/tools/tomcat/   //切换到挂载目录
[root@tomcat2 tomcat]# rpm -ivh jdk-8u201-linux-x64.rpm   //直接安装rpm包

2, configure the environment variables

[root@tomcat2 tomcat]# cd /usr/java/jdk1.8.0_201-amd64/   //切换目录
[root@tomcat2 jdk1.8.0_201-amd64]# vim /etc/profile   //修改系统环境变量文件
##文件末尾添加环境变量
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
[root@tomcat2 jdk1.8.0_201-amd64]# source /etc/profile   //重新加载配置文件
[root@tomcat2 jdk1.8.0_201-amd64]# java -version   //查看版本

java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
[root@tomcat2 jdk1.8.0_201-amd64]#

3, configure Tomcat

[root@tomcat2 jdk1.8.0_201-amd64]# cd /mnt/tools/tomcat/   //切换到挂载目录
[root@tomcat2 tomcat]# tar zxf apache-tomcat-9.0.16.tar.gz -C /opt/    //解压到“/opt/”目录
[root@tomcat2 tomcat]# mv /opt/apache-tomcat-9.0.16/ /usr/local/tomcat9   //移动到“/usr/local/”目录并重命名
[root@tomcat2 tomcat]# ln -s /usr/local/tomcat9/bin/startup.sh /usr/bin/   //建立启动脚本软链接
[root@tomcat2 tomcat]# ln -s /usr/local/tomcat9/bin/shutdown.sh /usr/bin/   //建立关闭脚本软链接
[root@tomcat2 tomcat]# startup.sh    //开启服务
Using CATALINA_BASE:   /usr/local/tomcat9
Using CATALINA_HOME:   /usr/local/tomcat9
Using CATALINA_TMPDIR: /usr/local/tomcat9/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat2 tomcat]# netstat -ntap | grep 8080   //查看端口
tcp6       0      0 :::8080                 :::*                    LISTEN      52941/java          
[root@tomcat2 tomcat]#
[root@tomcat2 tomcat]# systemctl stop firewalld.service    //关闭防火墙
[root@tomcat2 tomcat]# setenforce 0   //关闭增强性安全功能
[root@tomcat2 tomcat]# 

4, detect access

Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)

5, add sites and files Home

[root@tomcat2 tomcat]# mkdir -pv /web/webapp1   //创建站点目录
mkdir: 已创建目录 "/web"
mkdir: 已创建目录 "/web/webapp1"
[root@tomcat2 tomcat]# vim /web/webapp1/index.jsp   //添加首页文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
 <head>
  <title>JSP test1 page</title>
 </head>
 <body>
  <% out.println("Welcome to test site,http://www.test2.com");%>
 </body>
</html>

6, modify Tomcat configuration files

[root@tomcat2 tomcat]# cd /usr/local/tomcat9/conf/   //切换目录
[root@tomcat2 conf]# vim server.xml    //修改配置文件
    <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
                        ##在Host标签里,添加下面内容                       
        <Context docBase="/web/webapp1" path="" reloadable="false">
        </Context>
[root@tomcat2 conf]# shutdown.sh     //关闭服务
Using CATALINA_BASE:   /usr/local/tomcat9
Using CATALINA_HOME:   /usr/local/tomcat9
Using CATALINA_TMPDIR: /usr/local/tomcat9/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar
[root@tomcat2 conf]# startup.sh    //开启服务
Using CATALINA_BASE:   /usr/local/tomcat9
Using CATALINA_HOME:   /usr/local/tomcat9
Using CATALINA_TMPDIR: /usr/local/tomcat9/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_201-amd64
Using CLASSPATH:       /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat2 conf]# 

7, detect access site

Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)

Third, set up Nginx server

1, package installation environment

[root@nginx ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++
............//省略过程
[root@nginx ~]#

2, extract the source package Nginx

[root@nginx ~]# mkdir /mnt/tools   //创建挂载目录
[root@nginx ~]# mount.cifs //192.168.100.100/tools /mnt/tools/   //挂载共享目录
Password for root@//192.168.100.100/tools:  
[root@nginx ~]# cd /mnt/tools/LNMP/   //切换到挂载目录
[root@nginx LNMP]# ls
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.2.tar.gz  php-7.1.20.tar.gz
mysql-boost-5.7.20.tar.gz  php-7.1.10.tar.bz2
[root@nginx LNMP]# tar zxf nginx-1.12.2.tar.gz -C /opt/    //解压源码包
[root@nginx LNMP]# cd /opt/   
[root@nginx opt]# ls
nginx-1.12.2  rh
[root@nginx opt]# 

3, compile and install Nginx Service

[root@nginx opt]# cd nginx-1.12.2/   //切换目录
[root@nginx nginx-1.12.2]# useradd -M -s /sbin/nologin nginx   //创建用户nginx
[root@nginx nginx-1.12.2]# ./configure \   //配置nginx服务
> --prefix=/usr/local/nginx \   //安装路径
> --user=nginx \   //属主
> --group=nginx \   //属组
> --with-file-aio \   //启用file aio支持(一种APL文件传输格式)
> --with-http_stub_status_module \   //获取nginx自上次启动以来的工作状态
> --with-http_gzip_static_module \   //在线实时压缩输出数据流
> --with-http_flv_module \   //为Flash Video(FLV)文件 提供服务端伪流媒体支持
> --with-http_ssl_module    //为HTTPS提供了必要的支持
[root@nginx nginx-1.12.2]# make && make install

4, Nginx service management optimization

[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/   //为nginx命令建立软链接,方便系统识别
[root@nginx nginx-1.12.2]# 
[root@nginx nginx-1.12.2]# vim /etc/init.d/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
[root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx   //给脚本添加执行权限
[root@nginx nginx-1.12.2]# chkconfig --add nginx   //添加让service工具能够识别
[root@nginx nginx-1.12.2]#
[root@nginx nginx-1.12.2]# service nginx start    //开启服务
[root@nginx nginx-1.12.2]# service nginx stop    //关闭服务
[root@nginx nginx-1.12.2]#

5, modify the service configuration file Nginx

[root@nginx nginx-1.12.2]# cd /usr/local/nginx/conf/   //切换目录
[root@nginx conf]# ls
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf
[root@nginx conf]# vim nginx.conf   //修改配置文件

    upstream tomcat-server {   ##添加tomcat服务器
                server 192.168.52.134:8080 weight=1;
                server 192.168.52.150:8080 weight=1;
        }

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://tomcat-server;   ##添加代理
        }

[root@nginx conf]# nginx -t   //检查配置文件格式
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
[root@nginx conf]# 
[root@nginx conf]# service nginx start    //开启服务
[root@nginx conf]# netstat -ntap | grep nginx   //查看端口
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      41747/nginx: master 
[root@nginx conf]# systemctl stop firewalld.service    关闭防火墙
[root@nginx conf]# setenforce 0   //关闭增强性安全功能
[root@nginx conf]# 

Fourth, the detection Load Balancing Service

1, access Nginx proxy server IP address

Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)

2, refresh the page, to test whether the load balancing

Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)
(A production environment, two pages need the same, which is to facilitate the demonstration deliberately set different)

Configuring static and dynamic separation

A, Nginx server configuration

1, modify the service configuration file Nginx

[root@nginx conf]# vim nginx.conf   //修改配置文件
##不用修改
          upstream tomcat-server {
                server 192.168.52.134:8080 weight=1;
                server 192.168.52.150:8080 weight=1;
        }

        location / {
            root   html;
            index  index.html index.htm;
           # proxy_pass http://tomcat-server;   ##注释
        }
##添加以下内容
        location ~.*.jsp$ {
            proxy_pass http://tomcat-server;
            proxy_set_header Host $host;
        }

        location ~.*.\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
            root html;
            expires 30d;
        }

2, modify the file Nginx service Home

[root@nginx conf]# vim ../html/index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">   //支持中文字符集
<title>静态页面</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>静态页面</h1>
<p>这是静态页面</p>
</body>
</html>
[root@nginx conf]# service nginx stop 
[root@nginx conf]# service nginx start 
[root@nginx conf]#

3, Test Home

Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)

Two, Tomcat server configuration 1

Create a new file Home

[root@tomcat1 ~]# cd /usr/local/tomcat9/webapps/   //切换目录
[root@tomcat1 webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@tomcat1 webapps]# mkdir test   //创建站点目录
[root@tomcat1 webapps]# cd test/
[root@tomcat1 test]# vim 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>动态页面1</title>
</head>
<body>
<div>动态页面1</div>
</body>
</html>

Three, Tomcat server configuration 2

Create a new file Home

[root@tomcat2 ~]# cd /usr/local/tomcat9/webapps/   //切换目录
[root@tomcat2 webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@tomcat2 webapps]# mkdir test   //创建站点目录
[root@tomcat2 webapps]# cd test/
[root@tomcat2 test]# vim /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>动态页面2</title>
</head>
<body>
<div>动态页面2</div>
</body>
</html>

Fourth, test whether the static and dynamic separation

1, dynamic file access via Nginx server IP address

Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)

2, refresh the page, load balancing test

Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)

Fifth, let Tomcat server calls Nginx server site pictures

1, respectively, in the two Tomcat server page file, add pictures

[root@tomcat1 test]# vim index.jsp 
<div>动态页面1</div><br><img src="picture.jpg">   //只需修改此行

[root@tomcat2 test]# vim index.jsp
<div>动态页面2</div><br><img src="picture.jpg">   //只需修改此行

2, add pictures to Nginx server specified site

[root@nginx conf]# cd /mnt/tools/   //切换到挂载点
[root@nginx tools]# mkdir -p /usr/local/nginx/html/test   //创建站点目录test,注意必须与Tomcat站点相同
[root@nginx tools]# cp picture.jpg /usr/local/nginx/html/test/   //复制图片到站点
[root@nginx tools]# 

3, test access

Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)

4, once again refresh, load balancing test

Nginx + Tomcat-- configure load balancing and separation of static and dynamic (real!)

Guess you like

Origin blog.51cto.com/14449541/2455755