Nginx+Tomcat multi instance and load balancing, dynamic and static separation cluster
Introduction: tomcat service can handle both dynamic pages and static pages; However, its processing speed of static pages is far lower than that of nginx and apache services, but nginx and apache services cannot directly process dynamic pages. The following describes the combined use of nginx and tomcat to achieve dynamic and static separation and load balancing; At present, many large websites use the nginx server as the reverse proxy and load balancer of back-end website programs to improve the load concurrency of the entire site
1, Tomcat multi instance deployment and installation
JDK must be installed before deploying tomcat, because JDK is a necessary environment for Tomcat to run
1. close the firewall and transfer the software packages required for installing Tomcat to the /opt directory
Turn off firewall systemctl stop firewalld systemctl disable firewalld setenforce 0 Tomcat Install package into opt Directory jdk-8u201-linux-x64.rpm apache-tomcat-9.0.16.tar.gz
2. install JDK
rpm -ivh jdk-8u201-linux-x64.rpm install JDK Operating environment
3. set JDK environment variables
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
4. install tomcat
cd /opt tar zxvf apache-tomcat-9.0.16.tar.gz mkdir /usr/local/tomcat Mobile decompression package, two sets configured tomcat mv apache-tomcat-9.0.16 /usr/local/tomcat/tomcat1 cp -a /usr/local/tomcat/tomcat1 /usr/local/tomcat/tomcat2
5. configure tomcat environment variables
vim /etc/profile.d/tomcat.sh #tomcat1 export CATALINA_HOME1=/usr/local/tomcat/tomcat1 export CATALINA_BASE1=/usr/local/tomcat/tomcat1 export TOMCAT_HOME1=/usr/local/tomcat/tomcat1 #tomcat2 export CATALINA_HOME2=/usr/local/tomcat/tomcat2 export CATALINA_BASE2=/usr/local/tomcat/tomcat2 export TOMCAT_HOME2=/usr/local/tomcat/tomcat2 source /etc/profile.d/tomcat.sh
6. modify the tomcat2 main configuration file
vim /usr/local/tomcat/tomcat2/conf/server.xml <Server port="8006" shutdown="SHUTDOWN"> #Line 22, modify the Server prot, default to 8005 - > modify to 8006 <Connector port="8081" protocol="HTTP/1.1" #Line 69, modify the Connector port. HTTP/1.1 defaults to 8080 - > modify to 8081 <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /> #Line 116, modify Connector port AJP/1.3, default to 8009 - > modify to 8010
7. modify the startup SH and shutdown SH file, add tomcat environment variable
vim /etc/profile.d/tomcat.sh #tomcat1 export CATALINA_HOME1=/usr/local/tomcat/tomcat1 export CATALINA_BASE1=/usr/local/tomcat/tomcat1 export TOMCAT_HOME1=/usr/local/tomcat/tomcat1 #tomcat2 export CATALINA_HOME1=/usr/local/tomcat/tomcat2 export CATALINA_BASE1=/usr/local/tomcat/tomcat2 export TOMCAT_HOME1=/usr/local/tomcat/tomcat2 #Refresh variables source /etc/profile.d/tomcat.sh
8. start the service and verify the browser
start-up tomcat1 cd /usr/local/tomcat/ cd /usr/local/tomcat/ netstat -anpt | grep java Check whether all ports are open normally http://192.168.59.118:8080/
2, Nginx+Tomcat load balancing and dynamic static separation
Project requirements: deploy two tomcat services on 192.168.50.128, with ports 8080 and 8081 respectively; Deploy a tomcat service on 192.168.50.168 with port 8080; Deploy an Nginx service on 192.168.50.138
On this basis, when web access is performed, Nginx handles the requests of static pages, and Tomcat handles the requests of dynamic pages, so as to realize dynamic static separation and load balancing.
1. principle of load balancing
1.1 rr load balancing mode
Each request is allocated to different back-end servers one by one in chronological order. If the maximum number of failures is exceeded (max\u failures, the default is 1), within the failure time (fail\u timeout, the default is 10 seconds), the node's failure weight becomes 0. After the failure time is exceeded, it returns to normal. Or after all nodes are down, all nodes return to effective and continue detection. Generally speaking, it can be evenly distributed according to the weight
1.2 least_conn minimum connection:
Give priority to scheduling client requests to the server with the least current connections
1.3 ip_hash load balancing mode:
The hash result allocation of each request to access ip, so that each visitor can access a back-end server, which can solve the problem of session, but the ip_hash will cause uneven load. Some services receive more and some service requests receive less. Therefore, ip is not recommended_ In the hash mode, the session sharing problem can be solved by using the sessin share of the back-end service to replace the ip of nginx_ hash
1.4 fair load balancing mode:
Requests are allocated according to the response time of the back-end server, and the response time side is allocated first
1.5 url_hash (third party) load balancing mode:
Hash and IP based on the URL requested by the user_ The hash algorithm is similar. It allocates each request according to the hash result of the URL, so that each URL is directed to the same back-end server, but it will also cause uneven allocation. This mode is relatively good when the back-end server is caching
2. Implementation principle of nginx load balancing
Main parameters of Nginx configuration reverse proxy
upstream Service pool name {} Configure the back-end server pool to provide response data proxy_pass http://Service pool name Configure server processing for the server pool that forwards access requests to the backend server pool
3. Realization principle of nginx dynamic and static separation
Dynamic static separation principle The server receives requests from the client, including both static and dynamic resources. The static resources are Nginx Providing services, dynamic resources Nginx Forward to back end Nginx Static processing advantages Nginx Processing static pages is much more efficient than Tomcat Processing capacity of if Tomcat If the number of requests is 1000, then Nginx 6000 requests Tomcat Throughput per second is 0.6M,Nginx Throughput per second of is 3.6M Nginx Ability to process static resources Tomcat 6X of treatment
4.Nginx service deployment
4.1 installing nginx service
systemctl stop firewalld setenforce 0 yum install -y gcc gcc-c++ make pcre-devel zlib-devel
4.2 compilation and installation
cd nginx-1.20.2/ ./configure \ #Compile configuration --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module make && make install #Compile configuration
4.3 adding system services
vim /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid #PID ExecStart=/usr/local/nginx/sbin/nginx #start-up ExecrReload=/bin/kill -s HUP $MAINPID #heavy load ExecrStop=/bin/kill -s QUIT $MAINPID #stop it PrivateTmp=true [Install] WantedBy=multi-user.target #Enable multi-user character interface chmod 754 /lib/systemd/system/nginx.service #Empowerment systemctl start nginx.service systemctl enable nginx.service
4.4 modifying the nginx service master configuration file
Add address pool for jump stay http Add this paragraph to the module upstream tomcat_server { Configure load balancing address pool 35 server 192.168.50.128:8080 weight=1; weight Means weight 36 server 192.168.50.128:8081 weight=1; 37 server 192.168.50.168:8080 weight=1; 38 } 39 40 server { 41 listen 80; 42 server_name localhost; 43 44 charset utf-8;
Set dynamic jump and local still picture request location ~ .*\.(gif|jpg|jpeg|png|bmp)$ { 48 root html/img/; Jump to picture img catalogue 49 expires 5d; Keep cache records for 5 days 50 } 51 location ~ .*\.jsp$ { 52 proxy_pass http://tomcat_server; Jump to address of address pool 53 proxy_set_header HOST $host; 54 proxy_set_header X-Real-IP $remote_addr; hold $remote_addr Assign to X-Real-IP,obtain IP 55 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; Give Way nginx Logged visited IP 56 57 }
4.5 preparation of test documents
Create an img directory and put pictures in it
5.Tomcat deployment
5.1. First Tomcat service deployment
Deploy two tomcat services on the 192.168.50.128 server. They have been deployed in the above multi instance deployment. Configure them based on that machine
mkdir /usr/local/tomcat1/webapps/boss Create the first test directory vim /usr/local/tomcat1/webapps/boss/index.jsp Write the contents of this dynamic page file ?ge language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test1 page</title> </head> <body> <% out.println("This is the dynamic test page 1 www.boss.com");%> </body> </html>
On the other side of this machine tomcat2 Dynamic page content authoring for mkdir /usr/local/tomcat2/webapps/zxc vim /usr/local/tomcat2/webapps/zxc/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test1 page</title> </head> <body> <% out.println("This is the dynamic test page 2 www.zxc.com");%> </body> </html>
5.2 modifying the configuration file of tomcat
This station tomcat1 Master profile modification for service vim /usr/local/tomcat1/conf/server.xml <Host name="localhost" appBase="webapps" #Delete the original site, add this site, and the above comments unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat1/webapps/boss" path="" reloadable="true" /> </Host>
This station tomcat1 Master profile modification for service <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat1/webapps/zxc" path="" reloadable="true" /> </Host>
/usr/local/tomcat1/bin/shutdown.sh /usr/local/tomcat1/bin/startup.sh /usr/local/tomcat2/bin/shutdown.sh /usr/local/tomcat2/bin/startup.sh
6. deployment of the second Tomcat server
Install a tomcat service on the server 192.168.50.168
6.1 uploading packages and installing environment
systemctl stop firewalld Turn off firewall setenforce 0 rpm -ivh jdk-8u201-linux-x64.rpm
6.2 setting JDK environment variables
vim /etc/profile.d/java.sh #new file 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 #Refresh variables java -version
6.3 changing the master configuration file
Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat1/webapps/boss" path="" reloadable="true" /> </Host>
6.4 writing dynamic page test file content
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("This is the dynamic test page 3 www.qwer.com");%> </body> </html>
Restart service /usr/local/tomcat/shutdown.sh /usr/local/tomcat/startup.sh
7. testing services
7.1 dynamic and static separation test
Enter the static address in the browser, access the default address first, and the display is normal
Enter the picture address, and the picture will automatically jump to the /img directory
Input 192.168.50.158/index.jsp Show dynamic pages
7.2 load balancing test
Input 192.168.50.158/index.jsp Each time you refresh a page, you use a different tomcat The service display indicates that the load balancing configuration is successful; Random display with the same weight
summary
Through the above two examples, we can preliminarily understand the status of the combined use of nginx and Tomcat to achieve load balancing and dynamic static separation. It is often used in the production environment, and can give full play to the advantages of nginx and Tomcat. The use of dynamic static separation greatly reduces the working hours of nginx and tomcat, and the use of load balancing reduces the running pressure of the server. It is a good method to deal with high concurrency