nginx + tomcat achieve separation of static and dynamic load balancing +

nginx + tomcat achieve separation of static and dynamic load balancing +

First, the introduction to static and dynamic separation and load balancing

Nginx + Tomcat's static and dynamic separation:
the so-called static and dynamic separation is to process pictures the client request by nginx (or apache, etc.), html and other static files, tomcat (or weblogic) processing jsp, do other dynamic document, so as to achieve static and dynamic page processed through different container access. nginx process much more efficient than tomcat static pages, dynamic pages and tomcat good deal, so to be able to better improve concurrent processing performance.

Nginx + Tomcat load balancing:
In a cluster of servers, Nginx play the role of a proxy server (reverse proxy), in order to avoid excessive pressure on a single server, the request is forwarded from the user to a different server, tomcat handles user request forwarded nginx

Second, the specific steps

1, Environment Introduction

name Roles address
centos7-1 nginx 192.168.142.153
centos7-2 Tomcat 192.168.142.154
centos7-3 Tomcat 192.168.142.132
win7 Client Unimportant (the same segment)

purpose:

When accessing the server, static pages, dynamic pages handled by nginx server is handled by tomcat

2, Tomcat configuration (Tomcat two same-side configuration)

Install jdk environment package

[root@localhost tomcat]# rpm -ivh jdk-8u201-linux-x64.rpm
[root@localhost tomcat]# cd /usr/java/jdk1.8.0_201-amd64/

Modify the global configuration file

[root@localhost 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@localhost jdk1.8.0_201-amd64]# source /etc/profile
[root@localhost jdk1.8.0_201-amd64]# cd /mnt/tomcat/

Tomcat software installation body

[root@localhost tomcat]# tar zxvf apache-tomcat-9.0.16.tar.gz -C /usr/local/
[root@localhost tomcat]# cd /usr/local/
[root@localhost local]# mv apache-tomcat-9.0.16/ tomcat

The establishment of start / stop soft link

[root@localhost local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
[root@localhost local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/

//启服务,关防火墙
[root@localhost local]# startup.sh 
[root@localhost local]# systemctl stop firewalld.service 
[root@localhost local]# setenforce 0

Create a Tomcat Home

[root@tomcat1 local]# mkdir -p /web/webapp1      #建立站点
[root@tomcat1 local]# vim /web/webapp1/index.jsp    #新建Tomcat首页
<%@ 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.testl.com" );%>
  </body>
</html>

Specified in the configuration file

[root@tomcat1 local]# cd /usr/local/tomcat/conf/
[root@tomcat1 conf]# vim server.xml
##149行插入
<Context docBase="/web/webapp1" path="" reloadable="false">
</Context>
//注解: 
//docBase: web应用的文档基准目录
//reloadable设置监视“类”是否变化
//path=""设置默认""类

//重启服务
[root@tomcat1 conf]# shutdown.sh
[root@tomcat1 conf]# startup.sh 

3, Nginx configuration

Installation environment package

[root@nginx ~]# yum -y install gcc gcc-c++ zlib-devel expat-devel pcre-devel pcre openssl-devel

Nginx install software body

[root@nginx mnt]# cd /opt/nginx-1.12.0/
[root@nginx nginx-1.12.0]# useradd -M -s /sbin/nologin nginx
[root@nginx nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx --group=nginx \
--with-file-aio \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_ssl_module
[root@nginx nginx-1.12.0]# make && make install

When you have reached this step, nginx will have three different directions in conjunction with Tomcat, here I will explain one by one

(1) nginx load balancing only, do not provide web services

[root@nginx nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
//gzip后添加
upstream tomcat-server {                  //地址池名称
        server 192.168.142.154:8080 weight=1;       //指向Tomcat地址,采用轮询,权重相同
        server 192.168.142.132:8080 weight=1;
}
//location /段后添加
proxy_pass http://tomcat-server;      //代理指向Tomcat地址池(之前指定的)

When (2) nginx only for static and dynamic separation

[root@nginx nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
//server段后添加
location ~ (. *). jsp$   {
    proxy_pass http://192.168.142.132:8080;      //在nginx遇到以jsp结尾的网页时将自动代理至Tomcat服务器
    proxy_set_header Host $host;
}

(3) nginx simultaneous load balancing and reverse proxy

[root@nginx nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
//gzip后添加
upstream tomcat-server {                  //地址池名称
        server 192.168.142.154:8080 weight=1;       //指向Tomcat地址,采用轮询,权重相同
        server 192.168.142.132:8080 weight=1;
}
//server段后添加
location ~ (. *). jsp$   {
    proxy_pass http://tomcat-server;
    proxy_set_header Host $host;
}

Open service

[root@nginx nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx nginx-1.12.0]# nginx 
[root@nginx nginx-1.12.0]# systemctl stop firewalld.service 
[root@nginx nginx-1.12.0]# setenforce 0

Guess you like

Origin blog.51cto.com/14484404/2460673