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地址的三层负载均衡. ...
随机推荐
- 聊聊spring-boot-starter-data-redis的配置变更
本文主要研究一下spring-boot-starter-data-redis的配置变更 配置变更 以前是spring-boot的1.4.x版本的(spring-data-redis为1.7.x版本), ...
- 深入浅说服务如何以Jar包的方式发布
序言 笔者前段时间在使用自研框架NF( 传送门 )开发一个自动模板生成工具之后,想将他发布到Linux下,之前一直使用IDE直接run as运行,在遇到发布的时候考虑过发布为war或者jar,在一番抉 ...
- PTA编程总结二
7-1 币值转换 (20 分) 输入一个整数(位数不超过9位)代表一个人民币值(单位为元),请转换成财务要求的大写中文格式.如23108元,转换后变成“贰万叁仟壹百零捌”元.为了简化输出,用小写英文字 ...
- Using FileUpload
Using FileUpload FileUpload can be used in a number of different ways, depending upon the requiremen ...
- pycharm2019连接mysql错误08801 ------Connection to django1@localhost failed. [08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.
Error:Connection to django1@localhost failed. [08001] Could not create connection to database server ...
- SpringBoot,用200行代码完成一个一二级分布式缓存
缓存系统的用来代替直接访问数据库,用来提升系统性能,减小数据库复杂.早期缓存跟系统在一个虚拟机里,这样内存访问,速度最快. 后来应用系统水平扩展,缓存作为一个独立系统存在,如redis,但是每次从缓存 ...
- 【后台管理系统】—— Ant Design Pro结合插件(一)
一.富文本braft-editor 安装并引用 npm install braft-editor --save import BraftEditor from 'braft-editor' impor ...
- winform带你玩转rabbitMQ
http://www.cnblogs.com/dubing/p/4017613.html
- Python Module_openpyxl_styles 样式处理
目录 目录 前言 系统软件 Working with styles Styles can be applied to the following aspects Styles模块 Copying st ...
- Java连接Hive使用Zookeeper的方式
Java连接Hive的方式就是通过JDBC的方式来连接,URL为jdbc:hive2://host:port/db;principal=X@BIGDATA.COM等,这种方式是直接连接HiveServ ...