Linux架构之Nginx 七层负载均衡
第50章 Nginx七层负载均衡
一、Nginx负载均衡基本概述
1)为什么要使用负载均衡
当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷。使用多台Web服务器组成集群,前端使用Nginx负载均衡,将请求分散地打到后端服务器集群中,实现负载的分发。可以大大提升系统的吞吐率、请求性能、高容灾能力。


往往我们接触的最多的是SLB(Server Load Balance)负载均衡,实现最多的也是SLB,那么SLB它的调度节点和服务节点通常是在一个地域里面。它在这个小的逻辑地域里面决定了他对部分服务的实时性、响应性是非常好的。
因此,当海量用户请求过来以后,它同样是请求调度节点,调度节点将用户的请求转发给后端对应的服务节点,服务节点处理完请求后再转发给调度节点,调度节点最后响应给用户节点。这样就能实现一个均衡的作用。Nginx就是一个典型的SLB。
2)负载均衡的叫法
2.1)负载均衡的叫法有很多:
负载均衡
负载
Load Balance
LB
2.2)公有云中叫法有:
阿里云负载均衡——SLB
青云负载均衡——QLB
腾讯云负载均衡——CLB
ucloud负载均衡——ULB
2.3)常见的负载均衡的软件有:
Nginx、Haproxy、LVS


2.4)负载均衡能实现的应用场景一: 四层负载均衡
所谓四层负载均衡指的是OSI七层模型中的传输层,那么传输层Nginx已经能支持TCP/IP的控制,所以只需要对客户端的请求进行TCP/IP协议的包转发就可以实现负载均衡,那么它的好处是性能非常快、只需要底层进行应用处理,而不需要进行一些复杂的逻辑。


2.5)负载均衡能实现的应用场景二:七层负载均衡
七层负载均衡是在应用层,它可以完成很多应用方面的协议请求。比如我们说的http应用的负载均衡,它可以实现http信息的改写、头信息的改写、安全应用规则控制、URL匹配规则控制、以及转发、rewrite等等的规则,所以在应用层的服务里面,我们可以做的内容就更多,那么Nginx则是一个典型的七层负载均衡SLB。


2.6)四层负载均衡与七层负载均衡区别
四层负载均衡数据包在底层就进行了分发,而七层负载均衡数据包则是在最顶层进行分发,由此可以看出,七层负载均衡效率没有四负载均衡高。
但七层负载均衡更贴近于服务,如:http协议就是七层协议,我们可以用Nginx可以做会话保持,URL路径规则匹配、head头改写等等,这些是四层负载均衡无法实现的。
注意:四层负载均衡不识别域名,七层负载均衡识别域名。
二、Nginx负载均衡配置场景
Nginx要实现负载均衡需要用到proxy_pass代理模块配置。
Nginx负载均衡与Nginx代理不同地方在于,Nginx的一个location仅能代理一台服务器,而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池。


1)Nginx upstream虚拟配置语法
Syntax: upstream name { ... }
Default: -
Context: http
# upstream例
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
2)Nginx负载均衡示例
2.1)环境准备
| 角色 | 外网IP(NAT) | 内网IP(LAN) | 主机名 |
|---|---|---|---|
| lb01 | eth0:10.0.0.5 | eth1:172.16.1.5 | lb01 |
| Web01 | eth0:10.0.0.7 | eth1:172.16.1.7 | web01 |
| Web02 | eth0:10.0.0.8 | eth1:172.16.1.8 | web02 |
2.2)Web01服务器上配置nginx
[root@web01 images]# cd ~
[root@web01 ~]# cd /etc/nginx/conf.d/
#配置nginx
[root@web01 conf.d]# vim node.conf
server{
listen 80;
server_name node.drz.com;
location / {
root /node;
index index.html;
}
}
[root@web01 ~]# cd /node
-bash: cd: /node: No such file or directory
#创建node目录
[root@web01 ~]# mkdir /node
#新建index.html
[root@web01 ~]# echo "Web01..." > /node/index.html
#重启nginx
[root@web01 ~]# systemctl restart nginx
=========================================================
2.3)Web02服务器上配置nginx
[root@web02 ROOT]# cd ~
[root@web02 ~]# cd /etc/nginx/conf.d/
#配置nginx
[root@web02 conf.d]# vim node.conf
server {
listen 80;
server_name node.drz.com;
location / {
root /node;
index index.html;
}
}
[root@web02 conf.d]# cd ~
#创建node目录
[root@web02 ~]# mkdir /node
#新建index.html
[root@web02 ~]# echo "Web02..." > /node/index.html
#重启nginx
[root@web02 ~]# systemctl restart nginx
=================================================
2.4)配置Nginx负载均衡
[root@lb01 code]# cd /etc/nginx/conf.d/
#配置nginx负载均衡
[root@lb01 conf.d]# vim node_proxy.conf
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name node.drz.com; location / {
proxy_pass http://node;
include proxy_params;
}
}
#准备Nginx负载均衡调度使用的proxy_params
[root@lb01 conf.d]# vim /etc/nginx/proxy_params
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60; proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
window键+R——drivers——etc——hosts, 修改内容为 10.0.0.5 node.drz.com
#重启nginx
[root@lb01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 conf.d]# systemctl restart nginx
打开浏览器,输入 node.drz.com



2.5)负载均衡常见典型故障
如果后台服务连接超时,Nginx是本身是有机制的,如果出现一个节点down掉的时候,Nginx会更据你具体负载均衡的设置,将请求转移到其他的节点上,但是,如果后台服务连接没有down掉,但是返回错误异常码了,如:504、502、500,此时你需要加一个负载均衡的设置,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;意思是,当其中一台返回错误码404,500...等错误时,可以分配到下一台服务器程序继续处理,提高平台访问成功率。
[root@lb01 conf.d]# vim node_proxy.conf
upstream node {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name node.drz.com;
location / {
proxy_pass http://node;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
include proxy_params;
}
}
三、Nginx负载均衡调度算法
| 调度算法 | 概述 |
|---|---|
| 轮询 | 按时间顺序逐一分配到不同的后端服务器(默认) |
| weight | 加权轮询,weight值越大,分配到的访问几率越高 |
| ip_hash | 每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器 |
| url_hash | 按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器 |
| least_conn | 最少链接数,那个机器链接数少就分发 |
Nginx负载均衡[rr]权重轮询具体配置
upstream load_pass {
server 10.0.0.7:80;
server 10.0.0.8:80;
}
=========================================
Nginx负载均衡[wrr]权重轮询具体配置
upstream load_pass {
server 10.0.0.7:80 weight=5;
server 10.0.0.8:80;
}
==========================================
Nginx负载均衡ip_hash
具体配置不能和weight一起使用。
#如果客户端都走相同代理, 会导致某一台服务器连接过多
upstream load_pass {
ip_hash;
server 10.0.0.7:80 weight=5;
server 10.0.0.8:80;
}
四、Nginx负载均衡后端状态
后端Web服务器在前端Nginx负载均衡调度中的状态
| 状态 | 概述 |
|---|---|
| down | 当前的server暂时不参与负载均衡 |
| backup | 预留的备份服务器 |
| max_fails | 允许请求失败的次数 |
| fail_timeout | 经过max_fails失败后,服务暂停时间 |
| max_conns | 限制最大的接收连接数 |
测试down状态测试该Server不参与负载均衡的调度
upstream load_pass {
#不参与任何调度, 一般用于停机维护
server 10.0.0.7:80 down;
}
===================================================
测试backup以及down状态
upstream load_pass {
server 10.0.0.7:80 down;
server 10.0.0.8:80 backup;
server 10.0.0.9:80 max_fails=1 fail_timeout=10s;
}
location / {
proxy_pass http://load_pass;
include proxy_params;
}
====================================================
测试max_fails失败次数和fail_timeout多少时间内失败多少次则标记down
upstream load_pass {
server 10.0.0.7:80;
server 10.0.0.8:80 max_fails=2 fail_timeout=10s;
}
=====================================================
测试max_conns最大TCP连接数
upstream load_pass {
server 10.0.0.7:80;
server 10.0.0.8:80 max_conns=1;
}
五、Nginx负载均衡健康检查
在Nginx官方模块提供的模块中,没有对负载均衡后端节点的健康检查模块,但可以使用第三方模块。 nginx_upstream_check_module来检测后端服务的健康状态。
1)安装依赖包
[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
2)下载nginx源码包以及nginx_upstream_check模块第三方模块
[root@lb02 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@lb02 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
3)解压nginx源码包以及第三方模块
[root@lb02 ~]# tar xf nginx-1.14.2.tar.gz
[root@lb02 ~]# unzip master.zip
4)进入nginx目录,打补丁(nginx的版本是1.14补丁就选择1.14的,p1代表在nginx目录,p0是不在nginx目录)
[root@lb02 ~]# cd nginx-1.14.2/
[root@lb02 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
[root@lb02 nginx-1.14.2]# make && make install
5)在已有的负载均衡上增加健康检查的功能
[root@lb01 conf.d]# cat proxy_web.conf
upstream web {
server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
#interval 检测间隔时间,单位为毫秒
#rise 表示请求2次正常,标记此后端的状态为up
#fall 表示请求3次失败,标记此后端的状态为down
#type 类型为tcp
#timeout 超时时间,单位为毫秒
}
server {
listen 80;
server_name web.drz.com;
location / {
proxy_pass http://web;
include proxy_params;
}
location /upstream_check {
check_status;
}
}
Linux架构之Nginx 七层负载均衡的更多相关文章
- nginx 七层负载均衡
[tcp] nginx 七层负载均衡 nginx负载均衡概述 当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台Web服务器组成集群,前端使用Nginx负载均衡, ...
- 第十五章 nginx七层负载均衡
一.Nginx负载均衡 1.为什么做负载均衡 当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台Web服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到 ...
- Nginx七层负载均衡的几种调度算法
Nginx是一款轻量级的高性能web服务器,同时也是一款非常优秀的负载均衡器和反向代理服务器.由于支持强大的正则匹配规则.动静分离.URLrewrite功能及安装配置简单且对网络稳定性依赖非常小等 ...
- 13、Nginx七层负载均衡
1.Nginx负载均衡基本概述 1.1为什么需要使用负载均衡 当我们的Web服务器直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡, ...
- 四层和七层负载均衡的特点及常用负载均衡Nginx、Haproxy、LVS对比
一.四层与七层负载均衡在原理上的区别 图示: 四层负载均衡与七层负载均衡在工作原理上的简单区别如下图: 概述: 1.四层负载均衡工作在OSI模型中的四层,即传输层.四层负载均衡只能根据报文中目标地址和 ...
- 四层and七层负载均衡
四层负载/七层负载 在常规运维工作中,经常会运用到负载均衡服务.负载均衡分为四层负载和七层负载,那么这两者之间有什么不同? 废话不多说,详解如下: 1. 什么是负载均衡 1)负载均衡(Load ...
- 高可用 & 七层负载均衡与四层负载均衡
内容概要 高可用 七层负载均衡 和 四层负载均衡 内容详细 一.高可用 1.什么是高可用 一般是指2台机器启动着完全相同的业务系统,当有一台机器down机了,另外一台服务器就能快速的接管,对于访问的用 ...
- windows第七层负载均衡--基于IIS的ARR负载均衡
载均衡有很多种方法,有硬件负载均衡,软件负载均衡,还可以从域名解析下手. 不过,今天只讲软件负载均衡 软件负载均衡一般分两种,从网络协议来讲(tcp/ip),主要集中在第四层和第七层进行负载均衡. 第 ...
- Web负载均衡学习笔记之四层和七层负载均衡的区别
0x00 简介 简单理解四层和七层负载均衡: ① 所谓四层就是基于IP+端口的负载均衡:七层就是基于URL等应用层信息的负载均衡:同理,还有基于MAC地址的二层负载均衡和基于IP地址的三层负载均衡. ...
随机推荐
- maven工程项目与项目之间的依赖方式
首先看一下项目结构: 1.需要在父工程中把子工程为坐标引进来,同时标注父工程为pom工程: 2.同时在父工程中把子工程当作一个模块引进来 3.需要在每一个子项目中通过parent标签,标注 ...
- ali之monkey学习
monkey主要用来进行压力测试,稳定性测试 http://www.cnblogs.com/yyangblog/archive/2011/03/10/1980068.html 1,什么是monkey ...
- route Cmd详解
第一条命令,配置外网网关: route -p add 0.0.0.0 mask 0.0.0.0 192.168.1.1 第二条命令,配置内网网关:route -p add 192.168.0.0 ma ...
- MVC、MVP 和 MVVM
MVC Model–View–Controller 模型:管理应用程序的数据.逻辑和规则 视图:展示数据(可以直接从模型中获取数据) 控制器:接收输入并将其转化成模型和视图的命令 MVP Model– ...
- 阶段3 1.Mybatis_12.Mybatis注解开发_5 mybatis注解建立实体类属性和数据库表中列的对应关系
创建新项目,一对多 复制刚才关闭的项目的文件 复制到们的新项目里面 复制包的依赖 删减相关代码.只保留这三个查询的方法 模糊查询改成传统的占位符的方式 之前是可以自定义实体类的属性字段,和数据库的字典 ...
- Python学习之==>json处理
json是一种所有语言都通用的Key-Value数据结构的数据类型,很像Python中的字典,在Python中可以通过json模块对json串和字典进行转换. 1.把字典转换成json串 import ...
- cocos2dx基础篇(9) 滑块控件CCControlSlider
[3.x] (1)去掉 “CC” (2)对象类 CCObject 改为 Ref (3)CCControlEvent 改为强枚举 Control::EventType (4)CCControlEvent ...
- Scratch少儿编程系列:(九)音乐高级技巧
一.程序说明 本程序用来演奏音乐,相对于“Scratch少儿编程系列:(八)演奏简单音乐”而言,本节介绍的方法适用于复杂点的音乐. 二.程序流程图 为了更直观的描述上述过程,采用流程图的方式将猜数字的 ...
- Go语言入门篇-命令 与 语法
一.命令基础 1. go run : 用于运行命令源码文件(如:go run helloworld.go) 只能接受一个命令源码文件以及若干个库源码文件作为文件参数 其内部操作步骤: (1)先编译源码 ...
- 深入理解java:2.3. 并发编程 java.util.concurrent包
JUC java.util.concurrent包, 这个包是从JDK1.5开始引入的,在此之前,这个包独立存在着,它是由Doug Lea开发的,名字叫backport-util-concurrent ...