1. Nginx implements load balancing principleNginx implements load balancing through reverse proxy The Nginx server is used as the front end, the Tomcat server is used as the back end, and web page requests are forwarded by the Nginx service. However, instead of forwarding all web requests, the static page requests are processed by the Nginx server itself, and the dynamic page requests are forwarded to the backend Tomcat server for processing. Tomcat is a lightweight application server, and the amount of traffic it can accept may be insufficient, so we need multiple Tomcat servers, and then use Nginx to configure weights to select the Tomcat server for processing, which is a load balancing strategy . Nginx Server 1. Use exact matching on the homepage 2. Static pages use regular matching to process them yourself 3. Dynamic pages use regular expressions to match requests ending with jsp and use proxy_pass to forward them to the Tomcat server 2. Principle of Nginx dynamic and static separationThe 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. Nginx static processing advantages
3. Nginx + Tomcat dynamic and static separation, load balancing configuration stepsEnvironmental preparation:Nginx server: 192.168.121.13 Tomcat server 1: 192.168.121.12:8080 192.168.121.12:8081 Tomcat Server 2: 192.168.121.10 1. Deploy Nginx load balancing server First upload the nginx-1.12.0.tar.gz compressed package to the /opt directory systemctl stop firewalld.service systemctl disable firewalld.service setenforce 0 yum install -y pcre-devel zlib-devel openssl-devel gcc gcc-c++ make useradd -M -s /sbin/nologin nginx cd /opt tar zxvf nginx-1.12.0.tar.gz -C /opt/ cd 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 make && make install ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ vim /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile = /usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target chmod 754 /lib/systemd/system/nginx.service systemctl start nginx.service systemctl enable nginx.service 2. Deploy two Tomcat application servers The required installation packages are: apache-tomcat-9.0.16.tar.gz jdk-8u201-linux-x64.rpm #scp apache-tomcat-9.0.16.tar.gz [email protected]:/opt #Upload the required compressed package from Tomcat1 to Tomcat2 server. Of course, we can also directly pull the compressed package to the /opt directory systemctl stop firewalld.service systemctl disable firewalld.service setenforce 0 rpm -qpl jdk-8u201-linux-x64.rpm rpm -ivh jdk-8u201-linux-x64.rpm java -version vim /etc/profile.d/java.sh 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 source /etc/profile.d/java.sh java -version cd /opt vim abc.java public class abc { public static void main(String[] args){ System.out.println("Hello World!") } } [root@localhost?opt]#javac abc.java #Used to detect whether the JDK environment is set up successfully [root@localhost?opt]#java abc Hello World! cd /opt tar zxvf apache-tomcat-9.0.16.tar.gz mv apache-tomcat-9.0.16 /usr/local/tomcat ##Start tomcat## /usr/local/tomcat/bin/startup.sh netstat -natp | grep 8080 Dynamic and static separation configuration(1) Tomcat1 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 test1 page</title> #Specify as test1 page</head> <body> <% out.println("Dynamic page 1, http://www.test1.com");%> </body> </html> vim /usr/local/tomcat/conf/server.xml #Since the host name is configured as localhost, you need to delete the previous HOST configuration <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true"> </Context> </Host> /usr/local/tomcat/bin/shutdown.sh /usr/local/tomcat/bin/startup.sh (2) Tomcat2 server configuration mkdir /usr/local/tomcat/tomcat1/webapps/test /usr/local/tomcat/tomcat2/webapps/test vim /usr/local/tomcat/tomcat1/webapps/test/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test2 page</title> #Specify as test2 page</head> <body> <% out.println("Dynamic page 2, http://www.test2.com");%> </body> </html> vim /usr/local/tomcat/tomcat1/conf/server.xml #Delete the previous HOST configuration <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/tomcat1/webapps/test" path="" reloadable="true" /> </Host> /usr/local/tomcat/tomcat1/bin/shutdown.sh /usr/local/tomcat/tomcat1/bin/startup.sh vim /usr/local/tomcat/tomcat2/webapps/test/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test3 page</title> #Specify as test3 page</head> <body> <% out.println("Dynamic page 3, http://www.test3.com");%> </body> </html> vim /usr/local/tomcat/tomcat2/conf/server.xml #Delete the previous HOST configuration <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/tomcat2/webapps/test" path="" reloadable="true" /> </Host> /usr/local/tomcat/tomcat2/bin/shutdown.sh /usr/local/tomcat/tomcat2/ Nginx load balancing moderr Load balancing mode: Each request is assigned to a different backend server in chronological order. If the maximum number of failures is exceeded (max_fails, default is 1), within the failure time (fail_timeout, default is 10 seconds), the failure weight of the node becomes 0. After the failure time, it returns to normal. Or when all nodes are down, all nodes are restored to valid and continue to detect. Generally speaking, rr can be evenly distributed according to the weight. least_conn least connections: Prioritize dispatching client requests to the server with the least current connections. ip_hash load balancing mode: Each request is assigned according to the hash result of the access IP, so that each visitor accesses a fixed backend server, which can solve the session problem. However, ip_hash will cause uneven load. Some services receive more requests, while others receive fewer requests. Therefore, it is not recommended to use the ip_hash mode. The session sharing problem can be solved by using the session sharing of the backend service instead of nginx's ip_hash. fair (third-party) load balancing mode: Requests are distributed based on the response time of the backend server, with requests with shorter response times given priority. url_hash (third-party) load balancing mode: Similar to the ip_hash algorithm, it distributes each request according to the hash result of the URL, so that each URL is directed to the same backend server, but it will also cause the problem of uneven distribution. This mode is better when the backend server is cached. SummarizeThis concludes this article about the principles and configuration of Nginx load balancing and dynamic and static separation. 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:
|
>>: What does input type mean and how to limit input
In the horizontal direction, you can set the row ...
Floating elements cause their parent elements to ...
ssh-secure shell, provides secure remote login. W...
This article introduces 5 ways to solve the 1px b...
Introduction to Debian Debian in a broad sense re...
1. Introduction to inode To understand inode, we ...
error message: ERROR 1862 (HY000): Your password ...
<br />The official version of Baidu Encyclop...
Everything needs a foundation. To build a house, ...
Table of contents 1. The role of index 2. Creatin...
Table of contents 1. Open the file Parameter Intr...
Recently, there has been a growing demand for imp...
Whenever I have any unclear questions, I come to ...
Table of contents Preface ErrorBoundary Beyond Er...
Through the brief introduction in the previous tw...