Nginx被动健康检查和主动健康检查
1.被动健康检查
Nginx自带有健康检查模块:ngx_http_upstream_module,可以做到基本的健康检查,配置如下:
upstream cluster{
server 172.16.0.23: max_fails= fail_timeout=10s;
server 172.16.0.24: max_fails= fail_timeout=10s;
# max_fails=1和fail_timeout=10s 表示在单位周期为10s钟内,中达到1次连接失败,那么接将把节点标记为不可用,并等待下一个周期(同样时常为fail_timeout)再一次去请求,判断是否连接是否成功。
# fail_timeout为10s,max_fails为1次。
}
server {
listen ;
server_name xxxxxxx.com;
location / {
proxy_pass http://cluster;
}
}
Nginx只有当有访问时后,才发起对后端节点探测。如果本次请求中,节点正好出现故障,Nginx依然将请求转交给故障的节点,然后再转交给健康的节点处理。所以不会影响到这次请求的正常进行。但是会影响效率,因为多了一次转发,而且自带模块无法做到预警。
2.主动健康检查(需使用第三方模块)
主动地健康检查,nignx定时主动地去ping后端的服务列表,当发现某服务出现异常时,把该服务从健康列表中移除,当发现某服务恢复时,又能够将该服务加回健康列表中。淘宝有一个开源的实现nginx_upstream_check_module模块
官网:http://tengine.taobao.org/document_cn/http_upstream_check_cn.html
http {
upstream cluster1 {
# simple round-robin
server 192.168.0.1:;
server 192.168.0.2:;
check interval= rise= fall= timeout= type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
upstream cluster2 {
# simple round-robin
server 192.168.0.3:;
server 192.168.0.4:;
check interval= rise= fall= timeout= type=http;
check_keepalive_requests ;
check_http_send "HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen ;
location / {
proxy_pass http://cluster1;
}
location / {
proxy_pass http://cluster2;
}
location /status {
check_status;
access_log off;
allow SOME.IP.ADD.RESS;
deny all;
}
}
}
3.集成第三方模块部署
3.1、下载nginx_upstream_check_module模块
#进入nginx安装目录
cd /usr/local/nginx #下载nginx_upstream_check_module模块
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
#wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip #解压
unzip master cd nginx-1.12 # 进入nginx的源码目录
# -p0,是“当前路径” -p1,是“上一级路径”
patch -p1 < ../nginx_upstream_check_module-master/check_1.11.5+.patch
#nginx -V 可以查看原有配置 输出 ./configure --prefix=/usr/local/nginx
#增加upstream_check模块
./configure --prefix=/usr/local/nginx --add-module=../nginx_upstream_check_module-master
make
/usr/local/nginx/sbin/nginx -t # 检查下是否有问题 注意 check版本和Nginx版本要求有限制 .12以上版本的nginx,补丁为check_1.11.5+.patch 具体参考github https://github.com/yaoweibin/nginx_upstream_check_module
3.2.修改配置文件,让nginx_upstream_check_module模块生效
http {
upstream cluster1 {
# simple round-robin
server 192.168.0.1:;
server 192.168.0.2:;
check interval= rise= fall= timeout= type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
upstream cluster2 {
# simple round-robin
server 192.168.0.3:;
server 192.168.0.4:;
check interval= rise= fall= timeout= type=http;
check_keepalive_requests ;
check_http_send "HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen ;
location / {
proxy_pass http://cluster1;
}
location / {
proxy_pass http://cluster2;
}
location /status {
check_status;
access_log off;
allow SOME.IP.ADD.RESS;
deny all;
}
}
}
3.3重载nginx
访问http://nginx/nstatus
人为把其中的一个节点关掉刷新http://nginx/nstatus
udp反向代理时健康检查的问题,另一位大神在上面nginx_upstream_check_module的基础上作了修改,实现了在第4层的代理tcp和udp时的健康检查。
https://github.com/zhouchangxun/ngx_healthcheck_module
Nginx被动健康检查和主动健康检查的更多相关文章
- Pod 健康检查和服务可用性检查
Kubernetes 对 Pod 的健康状态可以通过两类探针来检查:LivenessProbe 和 ReadinessProbe,kubelet 定期执行这两类探针来针对容器的健康状况. Livene ...
- nginx 健康检查和负载均衡机制分析
nginx 是优秀的反向代理服务器,这里主要讲它的健康检查和负载均衡机制,以及这种机制带来的问题.所谓健康检查,就是当后端出现问题(具体什么叫出现问题,依赖 于具体实现,各个实现定义不一样),不再往这 ...
- 分析NGINX 健康检查和负载均衡机制
nginx 是优秀的反向代理服务器,这里主要讲它的健康检查和负载均衡机制,以及这种机制带来的问题.所谓健康检查,就是当后端出现问题(具体什么叫出现问题,依赖于具体实现,各个实现定义不一样),不再往这个 ...
- Kong05-Kong 的健康检查和监控
您可以让 Kong 代理的 API 使用 ring-balancer , 通过添加包含一个或多个目标实体的upstream 实体来配置,每个目标指向不同的IP地址(或主机名)和端口.ring-bala ...
- Tengine笔记3:Nginx的反向代理和健康状态检查
通常代理服务器只用于处理内部网络对Intenet的请求,客户端必须通过代理服务器把本来要发送到Web服务器上的请求通过代理服务器分发给Web服务器,Web服务器响应时再通过代理服务器把响应发给客户端: ...
- nginx_upstream_check_module-master对nginx的后端机器进行健康状态检查报403错误【转】
在nginx.conf配置文件中 在server添加 location /nstatus { check_status; access_log off; #allow 192.168.2.11; #d ...
- nginx自动检测后台服务器健康状态
转自http://www.iyunv.com/thread-38535-1-1.html 公司业务线上对后端节点的健康检查是通过nginx_upstream_check_module模块做的,这里我将 ...
- nginx下后端节点realserverweb健康检测模块ngx_http_upstream_check_module
本文章收录做资料使用,非本人原创,特此说明. 公司前一段对业务线上的nginx做了整理,重点就是对nginx上负载均衡器的后端节点做健康检查.目前,nginx对后端节点健康检查的方式主要有3种,这里列 ...
- [Kubernetes]容器健康检查和恢复机制
在Kubernetes中,可以为Pod里的容器定义一个健康检查探针(Probe),这样Kubernetes会根据这个Probe的返回值决定这个容器的状态,而不是直接以容器是否允许(来自Docker返回 ...
随机推荐
- leetcode-12双周赛-1244-力扣排行榜
题目描述: class Leaderboard: def __init__(self): self.map = collections.Counter() def addScore(self, pla ...
- HTML标签分类总结
块级元素和行内元素块级元素address - 地址blockquote - 块引用center - 举中对齐块dir - 目录列表div - 常用块级容易,也是css layout的主要标签dl - ...
- php 实现的功能
1.php写日志函数 (如:前端请求日志记录) : https://www.cnblogs.com/lvchenfeng/p/6794822.html 2.php中(服务器)使用CURL实现GET和P ...
- Chrome 调试跨域问题解决方案之插件篇
跨域,就是A域名下的js,想请求B域名下的接口数据.跨域,只存在于浏览器端.App和小程序不存在跨域问题.跨域,分浏览器策略和服务器策略. 如果服务器配置了允许跨域,那就没有跨域问题 如果uni-ap ...
- HDU2586---How far away ?(lca算法)
Problem Description There are n houses in the village and some bidirectional roads connecting them. ...
- php开发面试题---创建型设计模式1(创建型设计模式有哪几种)
php开发面试题---创建型设计模式1(创建型设计模式有哪几种) 一.总结 一句话总结: 共五种:(简单工厂模式).工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 1.学设计模式最好的方 ...
- 关于cefsharp 获取js动态加载后的信息
IFrame frame = null; var identifiers = Browser.GetBrowser().GetFrameIdentifiers(); foreach (var i in ...
- 这是<一起找打的约定>的改良版本
-- CREATE TABLE class ( -- cid INT(25)auto_increment PRIMARY KEY, -- caption VARCHAR(50) not NULL -- ...
- XVIII Open Cup named after E.V. Pankratiev. GP of SPb
contest Link A. Base i − 1 Notation solved by sdcgvhgj 238 求出a+b的2进制后从低位到高两位两位地转化为i-1进制 i-1进制的第2k位和第 ...
- Java中static关键字,this关键字
static修饰的成员方法和成员变量都是类方法和类变量,随类的加载而加载 static方法可以直接调用另一个static方法 static中调用普通方法可以通过类的实例对象调用 static不可以修饰 ...