nginx+tomcat for dynamic-static separation+load balancing

1. Introduction to static-dynamic separation and load balancing

Dynamic and Static Separation of Nginx+Tomcat:
The so-called dynamic-static separation is to process static files such as pictures, html, etc. requested by clients through nginx (or apache, etc.), and dynamic files such as jsp, do, etc. by Tomcat (or weblogic), so as to achieve dynamic and static page access through different containers.Nginx is much more efficient at handling static pages than tomcat, which is good at dynamic page processing, which can improve concurrency and processing performance.

Load balancing for Nginx+Tomcat:
In a server cluster, Nginx acts as a proxy server (i.e., a reverse proxy). To avoid excessive pressure on a single server, requests from users are forwarded to different servers. tomcat handles user requests forwarded by nginx

2. Specific steps

1. Introduction to the environment

Name role address
centos7-1 nginx
centos7-2 Tomcat
centos7-3 Tomcat
win7 Client Not important (the same segment is fine)


When accessing the server, static pages are processed by the nginx server and dynamic pages are processed by tomcat

2. Tomcat configuration (same configuration on both Tomcat sides)

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 Global Profile

[root@localhost jdk1.8.0_201-amd64]# vim /etc/profile
//Last Line Add
    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/

Install Tomcat software Ontology

[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

Establish start/stop soft links

[root@localhost local]# ln -s /usr/local/tomcat/bin/ /usr/local/bin/
[root@localhost local]# ln -s /usr/local/tomcat/bin/ /usr/local/bin/

//Turn on services, turn off firewalls
[root@localhost local]# 
[root@localhost local]# systemctl stop firewalld.service 
[root@localhost local]# setenforce 0

Create Tomcat Home Page

[root@tomcat1 local]# mkdir -p /web/webapp1      #Set up a site
[root@tomcat1 local]# vim /web/webapp1/index.jsp    #New Tomcat Home Page
<%@ page language= "java" import="java.util.*" pageEncoding="UTF-8" %>
    <title>JSP test1 page</title>
    <% out.println("Welcome to test site," );%>

Specify in configuration file

[root@tomcat1 local]# cd /usr/local/tomcat/conf/
[root@tomcat1 conf]# vim server.xml
##149 row inserts
<Context docBase="/web/webapp1" path="" reloadable="false">
//DocBase: Document base directory for web applications
//The reloadable setting monitors whether the Class changes
//path="" Sets the default "" class

//Restart Service
[root@tomcat1 conf]#
[root@tomcat1 conf]# 

3. Nginx Configuration

Install Environment Package

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

Install nginx software Ontology

[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 \
[root@nginx nginx-1.12.0]# make && make install

At this point, nginx will be combined with Tomcat in three different directions, and I'll explain each one here as well.

(1) nginx only performs load balancing and does not provide web services

[root@nginx nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
//Add after gzip
upstream tomcat-server {                  //Address pool name
        server weight=1;       //Point to Tomcat address, poll, weight equal
        server weight=1;
//Add after location/segment
proxy_pass http://tomcat-server; //Proxy points to Tomcat address pool (previously specified)

(2) When nginx is only used for static and dynamic separation

[root@nginx nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
//Add after server segment
location ~ (. *). jsp$   {
    proxy_pass; //Automatically proxy to Tomcat server when nginx encounters a web page ending in jsp
    proxy_set_header Host $host;

(3) nginx carries out load balancing and reverse proxy simultaneously

[root@nginx nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf
//Add after gzip
upstream tomcat-server {                  //Address pool name
        server weight=1;       //Point to Tomcat address, poll, weight equal
        server weight=1;
//Add after server segment
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

Tags: Web Server Nginx Tomcat JSP vim

Posted on Sun, 22 Dec 2019 22:28:23 -0500 by psychohagis