1, Implementation principle of Nginx load balancing
Load balancing is mainly to solve the problem of excessive server load. Sometimes, the concurrency of the system is too large, and one server can't afford the requests sent by users. Therefore, we need to build a server cluster, and nginx load balancing is to receive users' requests and forward them to each machine in the background server cluster.
- Load balancing has the following characteristics:
Distribute the load of background server
Automatically remove servers with background downtime
Cache the background request content to speed up the request speed
- There are five main strategies for nginx load balancing:
Polling (default)
Each request is allocated to different back-end servers one by one in chronological order. If the back-end server goes down, it can be automatically eliminated.
weight
Specifies the polling probability. The weight is directly proportional to the access ratio. It is used in the case of uneven performance of the back-end server.
ip_hash
Each request is allocated according to the hash result of access ip, so that each visitor can access a back-end server regularly, which can solve the problem of session.
fair (third party)
Requests are allocated according to the response time of the back-end server, and those with short response time are allocated first.
url_hash (third party)
The request is allocated according to the hash result of the access url, so that each url is directed to the same back-end server, which is more effective when the back-end server is cache.
2, Realization principle of dynamic and static separation of Nginx
The server receives both static and dynamic resources in the request from the client. The static resources are served by Nginx, and the dynamic resources are forwarded to the back end by Nginx
3, Nginx+Tomcat dynamic and static separation
3.1 experimental environment
server name | IP address |
---|---|
nginx | 20.0.0.10 |
tomcat01 | 20.0.0.11 |
tomcat02 | 20.0.0.12 |
3.2 building nginx services
[root@nginx01 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel [root@nginx01 ~]# useradd -M -s /sbin/nologin nginx [root@nginx01 ~]# tar zxvf nginx-1.12.2.tar.gz [root@nginx01 ~]# cd nginx-1.12.2/ [root@nginx01 nginx-1.12.2]# ./configure \ > --prefix=/usr/local/nginx \ > --user=nginx \ > --group=nginx [root@nginx01 nginx-1.12.2]# make && make install [root@nginx1 nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##Create nginx command soft connection [root@nginx1 nginx-1.12.2]# nginx -t ##Check syntax nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@nginx nginx-1.12.2]# vim /etc/init.d/nginx ##Create nginx startup script #!/bin/bash # chkconfig: - 99 20 # description: Nginx Service Control Script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "Usage: $0 {start|stop|restart|reload}" exit 1 esac exit 0 [root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx ##Add permissions [root@nginx nginx-1.12.2]# chkconfig --add nginx ##Add to service management [root@nginx nginx-1.12.2]# service nginx start ##Turn on nginx [root@nginx nginx-1.12.2]# netstat -ntap |grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 66115/nginx: master [root@nginx1 nginx-1.12.2]# systemctl stop firewalld.service ##Turn off the firewall [root@nginx1 nginx-1.12.2]# setenforce 0
3.3 build tomcat service
[root@tomcat01 ~]# systemctl stop firewalld.service [root@tomcat01 ~]# setenforce 0 [root@tomcat01 ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/ [root@tomcat01 ~]# vim /etc/profile export JAVA_HOME=/usr/local/jdk1.8.0_91 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH [root@tomcat01 ~]# source /etc/profile [root@tomcat01 ~]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/ [root@tomcat01 ~]# cd /usr/local/ [root@tomcat01 local]# mv apache-tomcat-8.5.16/ tomcat [root@tomcat01 local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/ [root@tomcat01 local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/ [root@tomcat01 local]# startup.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8.0_91/jre Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@tomcat01 local]# netstat -ntap | grep 8080 tcp6 0 0 :::8080 :::* LISTEN 17841/java
3.4 configuration process of dynamic and static separation
3.4.1 nginx displays static pages and tomcat displays dynamic pages
1. Add static page to nginx server
[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name localhost;location <span class="token operator">~</span><span class="token operator">.</span><span class="token operator">*</span><span class="token operator">.</span>jsp$ <span class="token punctuation">{<!-- --></span> <span class="token comment">##Add the following lines</span> proxy_pass http<span class="token punctuation">:</span><span class="token operator">//</span><span class="token vstring string">20.0.0.11</span><span class="token punctuation">:</span><span class="token number">8080</span><span class="token punctuation">;</ Span > < span class = "token comment" > ## points to the Tomcat server address</span> proxy_set_header Host <span class="token variable">$host</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
[root@nginx nginx-1.12.2]# cd /usr/local/nginx/html/
[root@nginx html]# vim index.html
<!DOCTYPE html>
<html>
<head>
< title > static page < / Title > ## modify the title
< meta http equiv = "content type" content = "text/html;charset=utf-8" > ## setting supports Chinese character set
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Static website < / H1 > ## modify content theme
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p>< EM > this is a static web page</ EM > < / P > ## set footer content
</body>
</html>
[root@nginx html]# service nginx stop
[root@nginx html]# service nginx start
2. Add dynamic server to tomcat server
[root@tomcat01 local]# cd /usr/local/tomcat/webapps/ [root@tomcat01 webapps]# mkdir test [root@tomcat01 webapps]# vim test/index.jsp <!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.Date" %> <%@ page import="java.text.SimpleDateFormat" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Dynamic page</title> </head> <body> <div>Dynamic page</div> </body> </html>
3.4.2. nginx handles static pictures and Tomcat handles dynamic page configuration. The two are displayed in combination
● Tomcat refers to the picture path, and nginx puts pictures
● the directory name should be the same as the Java project name
[root@tomcat01 webapps]# vim /usr/local/tomcat/webapps/test/index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.Date" %> <%@ page import="java.text.SimpleDateFormat" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ html4/loose.dtd"> <html> <head> <title>it works!</title> </head> <body> <div> This is a dynamic page</div> <img src="a.jpg"/> ##Refers to the picture path </body> </html>
[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf location ~.*\.(gif|jpg|jpeg|png|bmp|swf|css)$ { ##Add this paragraph to match these types of files root html; ##html site directory ' expires 30d; ##Cache time 30 days' } [root@nginx ~]# cd /usr/local/nginx/html [root@nginx html]# mkdir test [root@nginx html]# cp /usr/local/nginx/html/a.jpg /usr/local/nginx/html/test/ [root@nginx html]# cd test/ [root@nginx test]# ll Total consumption 52 -rw-r--r--. 1 root root 52848 12 March 3:10 a.jpg
3.5 Load balancing configuration process
Configure one in tomcat The server is named tomcat02,And before nginxtomcat01 Load balancing together
tomcat02 to configure:[root@tomcat02 ~]# tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/ [root@tomcat02 ~]# vim /etc/profile export JAVA_HOME=/usr/local/jdk1.8.0_91 export JRE_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH [root@tomcat02 ~]# source /etc/profile [root@tomcat02 ~]# tar zxvf apache-tomcat-8.5.16.tar.gz -C /usr/local/ [root@tomcat02 ~]# cd /usr/local/ [root@tomcat02 local]# mv apache-tomcat-8.5.16/ tomcat [root@tomcat02 local]# ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/ [root@tomcat02 local]# ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/ [root@tomcat02 local]# startup.sh Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/jdk1.8.0_91/jre Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started. [root@tomcat02 local]# netstat -ntap | grep 8080 tcp6 0 0 :::8080 :::* LISTEN 17841/java
nginx Profile modification
[root@nginx nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf ...Omit content #gzip on; upstream tomcat-server { 'Add node server address' server 20.0.0.11:8080 weight=1; server 20.0.0.12:8080 weight=1; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://tomcat_server; ' 'set forwarding to node server ' } ...Omit content [root@nginx ~]# systemctl restart nginxtomcat01 configuration file
[root@tomcat01 local]# mkdir -pv /web/webapp1 [root@tomcat01 local]# vim /web/webapp1/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test1 page</title> </head> <body> <% out.println("Welcome tomcat01 Web");%> </body> </html> [root@tomcat01 local]# vim /usr/local/tomcat/conf/server.xml ......148 Modify at line <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context docBase="/web/webapp1" path="" reloadable="false"> ####Add this section of site information </Context> [root@tomcat001 tomcat]# shutdown.sh ##Shut down service [root@tomcat01 tomcat]# startup.sh ##Open servicetomcat02 Profile:
[root@tomcat02 local]# mkdir -pv /web/webapp1 [root@tomcat02 local]# vim /web/webapp1/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test1 page</title> </head> <body> <% out.println("Welcome tomcat02 Web");%> </body> </html> [root@tomcat02 local]# vim /usr/local/tomcat/conf/server.xml ......148 Modify at line <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context docBase="/web/webapp1" path="" reloadable="false"> ####Add this section of site information </Context> [root@tomcat02 tomcat]# shutdown.sh ##Shut down service [root@tomcat02 tomcat]# startup.sh ##Open serviceAuthentication: browser access nginx Address 20.0.0.10 Will poll