一、Nginx负载均衡实现原理

1、Nginx实现负载均衡是通过反向代理实现

2、反向代理原理

配置Nginx+Tomcat负载均衡、动静分离集群_tomcat

3、Nginx 配置反向代理的主要参数

  • upstream 服务池名 {}
    ♢ 配置后端服务器池,比提供相应数据
  • proxy_pass ​​http://服务池名​​ ♢ 配置将访问请求转发给后端服务器池的服务器处理
二、Nginx动静分离实现原理

① 动静分离原理

服务端接收来自客户端的请求中。既有静态资源也有动态资源,静态资源由Nginx提供服务,动态资源Nginx转发至后端

配置Nginx+Tomcat负载均衡、动静分离集群_tomcat_02

② Nginx 静态处理优势

  • Nginx处理静态页面的效率远高于Tomcat的处理能力
  • 若Tomcat的请求量为1000次,则Nginx的请求量为6000次
  • Tomcat每秒的吞吐量为0.6M,Nginx的每秒吞吐量为3.6M
  • Nginx处理静态资源的能力是Tomcat处理的6倍

1、准备三台服务器,Nginx作为负载均衡器,Tomcat作为应用服务器

Nginx 服务器:192.168.237.60:80
Tomcat服务器1:192.168.237.70:8080
Tomcat服务器2:192.168.237.80:8080 192.168.237.80:8081


 

2、部署Nginx 负载均衡器

systemctl stop firewalld
setenforce 0

yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make

useradd -M -s /sbin/nologin nginx


 

配置Nginx+Tomcat负载均衡、动静分离集群_html_03

配置Nginx+Tomcat负载均衡、动静分离集群_负载均衡_04

配置Nginx+Tomcat负载均衡、动静分离集群_html_05

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 \ #启用 gzip静态压缩
--with-http_flv_module \ #启用 flv模块,提供对 flv 视频的伪流支持
--with-http_ssl_module #启用 SSL模块,提供SSL加密功能
----------------------------------------------------------------------------------------------------------
./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


 

配置Nginx+Tomcat负载均衡、动静分离集群_html_06

配置Nginx+Tomcat负载均衡、动静分离集群_tomcat_07

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
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/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


 

配置Nginx+Tomcat负载均衡、动静分离集群_html_08

配置Nginx+Tomcat负载均衡、动静分离集群_负载均衡_09

配置Nginx+Tomcat负载均衡、动静分离集群_html_10

配置Nginx+Tomcat负载均衡、动静分离集群_html_11

配置Nginx+Tomcat负载均衡、动静分离集群_html_12

3、部署2台Tomcat 应用服务器

systemctl stop firewalld
setenforce 0

tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/

vim /etc/profile.d/java.sh
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

source /etc/profile.d/java.sh


 

配置Nginx+Tomcat负载均衡、动静分离集群_java_13

配置Nginx+Tomcat负载均衡、动静分离集群_java_14

配置Nginx+Tomcat负载均衡、动静分离集群_nginx_15

配置Nginx+Tomcat负载均衡、动静分离集群_java_16

配置Nginx+Tomcat负载均衡、动静分离集群_tomcat_17

tar zxvf apache-tomcat-8.5.16.tar.gz

mkdir -p /usr/local/tomcat
mv apache-tomcat-9.0.16 /usr/local/tomcat/

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

netstat -ntap | grep 8080


 

配置 Tomcat1

配置Nginx+Tomcat负载均衡、动静分离集群_java_18

配置Nginx+Tomcat负载均衡、动静分离集群_html_19

配置Nginx+Tomcat负载均衡、动静分离集群_java_20

配置Nginx+Tomcat负载均衡、动静分离集群_java_21

4、动静分离配置

Tomcat1 server 配置

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> #指定为 test1 页面
</head>
<body>
<% out.println("动态页面 1,http://www.test1.com");%>
</body>
</html>


 

配置Nginx+Tomcat负载均衡、动静分离集群_java_22

配置Nginx+Tomcat负载均衡、动静分离集群_nginx_23

vim /usr/local/tomcat/conf/server.xml
#由于主机名 name 配置都为 localhost,需要删除前面的 HOST 配置
<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


 

配置Nginx+Tomcat负载均衡、动静分离集群_nginx_24

配置Nginx+Tomcat负载均衡、动静分离集群_负载均衡_25

配置Nginx+Tomcat负载均衡、动静分离集群_tomcat_26

配置Nginx+Tomcat负载均衡、动静分离集群_java_27

配置Nginx+Tomcat负载均衡、动静分离集群_java_28

Tomcat2 server 配置(多实例)

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> #指定为 test2 页面
</head>
<body>
<% out.println("动态页面 2,http://www.test2.com");%>
</body>
</html>


 

配置Nginx+Tomcat负载均衡、动静分离集群_html_29

配置Nginx+Tomcat负载均衡、动静分离集群_nginx_30

vim /usr/local/tomcat/tomcat1/conf/server.xml
#删除前面的 HOST 配置
<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


 

配置Nginx+Tomcat负载均衡、动静分离集群_负载均衡_31

配置Nginx+Tomcat负载均衡、动静分离集群_tomcat_32

配置Nginx+Tomcat负载均衡、动静分离集群_负载均衡_33

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> #指定为 test3 页面
</head>
<body>
<% out.println("动态页面 3,http://www.test3.com");%>
</body>
</html>


 

配置Nginx+Tomcat负载均衡、动静分离集群_tomcat_34

vim /usr/local/tomcat/tomcat2/conf/server.xml
#删除前面的 HOST 配置
<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/bin/startup.sh


 

配置Nginx+Tomcat负载均衡、动静分离集群_负载均衡_35

配置Nginx+Tomcat负载均衡、动静分离集群_java_36

配置Nginx+Tomcat负载均衡、动静分离集群_java_37

Nginx server 配置

#准备静态页面和静态图片
echo '<html><body><h1>这是静态页面</h1></body></html>' > /usr/local/nginx/html/index.html
mkdir /usr/local/nginx/html/img
cp /root/game.jpg /usr/local/nginx/html/img

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

#配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大
upstream tomcat_server {
server 192.168.237.70:8080 weight=1;
server 192.168.237.80:8080 weight=1;
server 192.168.237.80:8081 weight=1;
}

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

charset utf-8;

#access_log logs/host.access.log main;

#配置Nginx处理动态页面请求,将 .jsp文件请求转发到Tomcat 服务器处理
location ~ .*\.jsp$ {
proxy_pass http://tomcat_server;
#设置后端的Web服务器可以获取远程客户端的真实IP
##设定后端的Web服务器接收到的请求访问的主机名(域名或IP、端口),默认HOST的值为proxy_pass指令设置的主机名。如果反向代理服务器不重写该请求头的话,那么后端真实服务器在处理时会认为所有的请求都来在反向代理服务器,如果后端有防攻击策略的话,那么机器就被封掉了。
proxy_set_header HOST $host;
##把$remote_addr赋值给X-Real-IP,来获取源IP
proxy_set_header X-Real-IP $remote_addr;
##在nginx 作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

#配置Nginx处理静态图片请求
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css)$ {
root /usr/local/nginx/html;
expires 10d;
}

location / {
root html;
index index.html index.htm;
}
......
}
......
}


 

配置Nginx+Tomcat负载均衡、动静分离集群_java_38

配置Nginx+Tomcat负载均衡、动静分离集群_nginx_39

配置Nginx+Tomcat负载均衡、动静分离集群_负载均衡_40

配置Nginx+Tomcat负载均衡、动静分离集群_html_41

测试效果

测试静态页面效果
浏览器访问 http://192.168.237.60/
浏览器访问 http://192.168.237.60/game.jpg

测试负载均衡效果,不断刷新浏览器测试
浏览器访问 http://192.168.237.60/index.jsp


 

配置Nginx+Tomcat负载均衡、动静分离集群_nginx_42

配置Nginx+Tomcat负载均衡、动静分离集群_java_43

配置Nginx+Tomcat负载均衡、动静分离集群_负载均衡_44

配置Nginx+Tomcat负载均衡、动静分离集群_负载均衡_45

配置Nginx+Tomcat负载均衡、动静分离集群_html_46

补充

Nginx 负载均衡模式:

●rr 负载均衡模式:
每个请求按时间顺序逐一分配到不同的后端服务器,如果超过了最大失败次数后(max_fails,默认1),在失效时间内(fail_timeout,默认10秒),该节点失效权重变为0,超过失效时间后,则恢复正常,或者全部节点都为down后,那么将所有节点都恢复为有效继续探测,一般来说rr可以根据权重来进行均匀分配。

●least_conn 最少连接:
优先将客户端请求调度到当前连接最少的服务器。

●ip_hash 负载均衡模式:
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题,但是ip_hash会造成负载不均,有的服务请求接受多,有的服务请求接受少,所以不建议采用ip_hash模式,session 共享问题可用后端服务的 session 共享代替 nginx 的 ip_hash。

●fair(第三方)负载均衡模式:
按后端服务器的响应时间来分配请求,响应时间短的优先分配。

●url_hash(第三方)负载均衡模式:
和ip_hash算法类似,是对每个请求按url的hash结果分配,使每个URL定向到一个同 一个后端服务器,但是也会造成分配不均的问题,这种模式后端服务器为缓存时比较好


 

Nginx 四层代理配置:


和http同等级:所以一般只在http上面一段设置,
stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
access_log /var/log/nginx/k8s-access.log main;

upstream appserver{
server 192.168.80.100:8080 weight=1;
server 192.168.80.101:8080 weight=1;
server 192.168.80.101:8081 weight=1;
}
server {
listen 8080;
proxy_pass appserver;
}
}

http {
......


 

注:配置4层代理,记得在Nginx中添加 --with-stream 模块