Analysis of the principle of Nginx+Tomcat to achieve load balancing and dynamic and static separation

Analysis of the principle of Nginx+Tomcat to achieve load balancing and dynamic and static separation

1. Implementation principle of Nginx load balancing

1. Nginx implements load balancing through reverse proxy

Reverse Proxy refers to using a proxy server (e.g. Nginx) to accept connection requests on the Internet, then forward the requests to a server on the internal network (e.g. Tomcat), and return the results obtained from the server to the client requesting the connection on the Internet. At this time, the proxy server (e.g. Nginx) appears to the outside world as a reverse proxy server.
From the client's perspective, the client actually does not know which server is the real service provider. It only knows that it has requested the reverse proxy server. Therefore, the reverse proxy method hides the address of the real server from the outside, reducing security risks to a certain extent.

2. Main parameters of Nginx reverse proxy configuration upstream service pool name {}

Role: Configure the backend server pool to provide response data

proxy_pass http:// service pool name

Function: Configure the server processing that forwards access requests to the backend server pool

2. Implementation principle of Nginx dynamic and static separation

1. Dynamic and static separation principle

The server receives requests from the client, which include both static resources and dynamic resources. Static resources are served by Nginx, and dynamic resources are forwarded to the backend by Nginx.

2. Advantages of Nginx static processing

Nginx is much more efficient at processing static pages than Tomcat. If Tomcat handles 1,000 requests, Nginx handles 6,000 requests.
Tomcat's throughput is 0.6M per second, and Nginx's throughput is 3.6M per second
Nginx's ability to handle static resources is 6 times that of Tomcat

3. Nginx + Tomcat dynamic and static separation, load balancing configuration steps and environment preparation

Host operating system IP address Required Software
Nginx Server CentOS7 192.168.109.7 nginx-1.12.0.tar.gz
Tomcat Server1 CentOS7 192.168.109.22 apache-tomcat-9.0.16.tar.gz, jdk-8u201-linux-x64.rpm
Tomcat Server2 CentOS7 192.168.109.23 apache-tomcat-9.0.16.tar.gz, jdk-8u201-linux-x64.rpm
1. Deploy Nginx load balancing server

insert image description here

#!/bin/bash
#Script description: Compile and install nginx service #Note: Please put nginx-1.12.0.tar.gz into /opt directory before use #Shut down the firewall systemctl stop firewalld
systemctl disable firewalld
setenforce 0

#Install the required development packages, compilation environment, and compiler yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make

#Create a program user to accurately control access useradd -M -s /sbin/nologin nginx

#Unzip the installation package cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/

#Compile and install nginx; specify the installation path, user name, group name, and enable modules to support statistics cd /opt/nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-file-aio --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module

make && make install

#Soft link makes it easier for the system to identify the nginx operation command ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

#Add nginx system service echo '[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target' > /lib/systemd/system/nginx.service

#Empower and enable services, enable automatic startup chmod 754 /lib/systemd/system/nginx.service
systemctl restart nginx.service
systemctl enable nginx.service 

insert image description here
insert image description here

2. Deploy two Tomcat application servers

insert image description here

#!/bin/bash
#Install Tomcat service#Shut down the firewallsystemctl stop firewalld
systemctl disable firewalld
setenforce 0

#Install JDK
cd /opt
rpm -ivh jdk-8u201-linux-x64.rpm

#Set JDK environment variable echo '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' > /etc/profile.d/java.sh

source /etc/profile

#Install and start Tomcat
cd /opt
tar zxvf apache-tomcat-9.0.16.tar.gz
mv apache-tomcat-9.0.16 /usr/local/tomcat
/usr/local/tomcat/bin/startup.sh 

insert image description here

3. Dynamic and static separation configuration

Please see tomcat

1) Tomcat1 server configuration

mkdir /usr/local/tomcat/webapps/test

#Configure dynamic page vim /usr/local/tomcat/webapps/test/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("Dynamic page 1: www.test1.com");%>
</body>
</html> 

insert image description here

#Modify the configuration file vim /usr/local/tomcat/conf/server.xml
<Host name="192" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" />
</Host>

/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh 

insert image description here
insert image description here

2) Tomcat2 server configuration

mkdir /usr/local/tomcat/webapps/test

vim /usr/local/tomcat/webapps/test/index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title>
</head>
<body>
<% out.println("Dynamic page 1: www.test2.com");%>
</body>
</html> 

insert image description here

#Modify the configuration file vim /usr/local/tomcat/conf/server.xml
<Host name="192" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
	<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" />
</Host>

/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh 

insert image description here
insert image description here

3) Nginx server configuration

#Prepare static pages and static images echo '<html><body><h1>this is static</h1></body></html>' > /usr/local/nginx/html/index.html
mkdir /usr/local/nginx/html/long
cd /usr/local/nginx/html/long 

insert image description here

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
#gzip on;

upstream tomcat_server {
  server 192.168.109.22:8080 weight=1;
  server 192.168.109.23:8080 weight=1;
}

server {
	listen 80;
	server_name www.long.com;

  #charset koi8-r;

	#access_log logs/host.access.log main;

    #Configure Nginx to handle dynamic page requests and forward .jsp file requests to the Tomcat server for processing location ~ .*\.jsp$ {
		proxy_pass http://tomcat_server;
		
		proxy_set_header HOST $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	}
    
  #Configure Nginx to handle static image requests location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
		root /usr/local/nginx/html/img/;
		expires 10d;
	}
    location / {
      root html;
      index index.html index.htm;
    }


systemctl restart nginx.service 

insert image description here
insert image description here

insert image description here

4. Access test

Test the static page effect. Browser access http://192.168.109.7/
Browser access http://192.168.109.7/long.jpg

insert image description here

Test the load balancing effect, refresh the browser continuously to test the browser access http://192.168.109.7/test/index.jsp

insert image description here
insert image description here

This concludes this article on the analysis of the principles of load balancing and dynamic and static separation achieved with Nginx+Tomcat. For more relevant content on Nginx load balancing and dynamic and static separation, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Basic configuration example of Nginx with Apache or Tomcat for dynamic and static separation
  • Using Nginx+uWsgi to separate the dynamic and static parts of Python's Django framework site
  • Simple implementation of nginx+tomcat reverse proxy and dynamic and static separation
  • Detailed explanation of nginx to separate static and dynamic tomcat
  • nginx realizes load balancing and dynamic and static separation
  • Nginx sample code for implementing dynamic and static separation
  • Detailed example of deploying Nginx+Apache dynamic and static separation
  • Sample code for nginx to achieve dynamic and static separation
  • Nginx implements dynamic and static separation example explanation
  • Nginx dynamic and static separation implementation case code analysis
  • Detailed explanation of the process of realizing dynamic and static separation in Springmvc nginx
  • Docker Nginx container and Tomcat container to achieve load balancing and dynamic and static separation operations
  • The principle and configuration of Nginx load balancing and dynamic and static separation
  • Example of how nginx implements dynamic and static separation
  • Detailed instructions for nginx from installation to configuration (installation, security configuration, anti-hotlinking, dynamic and static separation, HTTPS configuration, performance optimization)
  • Implementation of Nginx+Tomcat load balancing and dynamic and static separation cluster
  • Server load balancing nginx+tomcat to achieve dynamic and static separation
  • Nginx dynamic and static separation configuration implementation and description

<<:  W3C Tutorial (6): W3C CSS Activities

>>:  CSS fixes the container level (div...) tag in one position (on the far right of the page)

Recommend

Three methods to modify the hostname of Centos7

Method 1: hostnamectl modification Step 1 Check t...

How to build SFTP server and image server on Linux cloud server

First of all, you can understand the difference b...

Causes and solutions for MySQL data loss

Table of contents Preface Problem Description Cau...

How to add abort function to promise in JS

Table of contents Overview Promise Race Method Re...

MySQL database constraints and data table design principles

Table of contents 1. Database constraints 1.1 Int...

MySQL startup error InnoDB: Unable to lock/ibdata1 error

An error message appears when MySQL is started in...

Use pure CSS to achieve switch effect

First is the idea We use the <input type="...

Build a file management system step by step with nginx+FastDFS

Table of contents 1. Introduction to FastDFS 1. I...

Solutions to invalid is Null segment judgment and IFNULL() failure in MySql

MySql Null field judgment and IFNULL failure proc...

This article will show you how to use SQL CASE WHEN in detail

Table of contents Simple CASEWHEN function: This ...

How to assign a public IP address to an instance in Linux

describe When calling this interface, you need to...

mysql5.7.22 download process diagram

1. Go to the official website www.mysql.com and s...