1. Implementation principle of Nginx load balancing1. 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. 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 separation1. 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. 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 |
#!/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
2. Deploy two Tomcat application servers
#!/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
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>
#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
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>
#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
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
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
4. Access test
Test the static page effect. Browser access http://192.168.109.7/
Browser access http://192.168.109.7/long.jpg
Test the load balancing effect, refresh the browser continuously to test the browser access http://192.168.109.7/test/index.jsp
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!
<<: W3C Tutorial (6): W3C CSS Activities
>>: CSS fixes the container level (div...) tag in one position (on the far right of the page)
Method 1: hostnamectl modification Step 1 Check t...
First of all, you can understand the difference b...
Table of contents Preface Problem Description Cau...
Table of contents Overview Promise Race Method Re...
Table of contents 1. Database constraints 1.1 Int...
Copy code The code is as follows: <!DOCTYPE ht...
An error message appears when MySQL is started in...
Strictly speaking, nginx does not have a health c...
First is the idea We use the <input type="...
Table of contents 1. Introduction to FastDFS 1. I...
MySql Null field judgment and IFNULL failure proc...
Table of contents Simple CASEWHEN function: This ...
describe When calling this interface, you need to...
Table of contents First we need to build the page...
1. Go to the official website www.mysql.com and s...