Nginx原生模快和Nginx平滑升级
本文介绍了Nginx后端节点的健康检查机制,重点讲解了Nginx原生模块和淘宝开发的nginx_upstream_check_module模块。在原生模块部分,详细说明了ngx_http_proxy_module和ngx_http_upstream_module的关键指令配置,包括proxy_pass、proxy_timeout等参数设置,以及通过max_fails和fail_timeout实现基
简介
本文主要介绍nginx后端节点的健康检查,在此之前我们先来介绍下nignx反向代理主要使用的模块。
一、nginx原生模块介绍
我们在使用nginx做反向代理都会使用到以下两个模块:
1、ngx_http_proxy_module
定义允许将请求传递到另一台服务器。此模块下常用指令如下:
proxy_pass
proxy_cache
proxy_connect_timeout
proxy_read_timeout
proxy_send_timeout
proxy_next_upstream
2、ngx_http_upstream_module
用于定义可由proxy_pass,fastcgi_pass等指令引用的服务器组。此模块下常用指令如下:
upstream
server
ip_hash
默认负载均衡配置
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}server {
listen 80;location / {
proxy_pass http://myapp1;
}
}
}
此时nginx默认的负载均衡策略是轮询外,还有其他默认参数,如下:
http {
upstream myapp1 {
server srv1.example.com weight=1 max_fails=1 fail_timeout=10;
server srv2.example.com weight=1 max_fails=1 fail_timeout=10;
server srv3.example.com weight=1 max_fails=1 fail_timeout=10;
}server {
listen 80;
proxy_send_timeout=60;
proxy_connect_timeout=60;
proxy_read_timeout=60;
proxy_next_upstream=error timeout;location / {
proxy_pass http://myapp1;
}
}
}
其中:
1.故障转移(了解)
Syntax(语法): proxy_read_timeout time;
Default(默认):
proxy_read_timeout 60s;
Context(上下文,能够书写该字段的位置): http, server, location
#定义从代理服务器读取响应的超时。 仅在两个连续的读操作之间设置超时,而不是为整个响应的传输。 如果代理服务器在此时间内未传输任何内容,则关闭连接。Syntax: proxy_connect_timeout time;
Default:
proxy_connect_timeout 60s;
Context: http, server, location
#定义与代理服务器建立连接的超时。 应该注意,此超时通常不会超过75秒。
Syntax: proxy_send_timeout time;
Default:
proxy_send_timeout 60s;
Context: http, server, location
#设置将请求传输到代理服务器的超时。 仅在两个连续的写操作之间设置超时,而不是为整个请求的传输。 如果代理服务器在此时间内未收到任何内容,则关闭连接Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...;
Default:
proxy_next_upstream error timeout;
Context: http, server, location
#指定在何种情况下一个失败的请求应该被发送到下一台后端服务器:
#error 和后端服务器建立连接时,或者向后端服务器发送请求时,或者从后端服务器接收响应头时,出现错误
#timeout 和后端服务器建立连接时,或者向后端服务器发送请求时,或者从后端服务器接收响应头时,出现超时
#invalid_header 后端服务器返回空响应或者非法响应头
#http_500 后端服务器返回的响应状态码为500
#http_502 后端服务器返回的响应状态码为502
#http_503 后端服务器返回的响应状态码为503
#http_504 后端服务器返回的响应状态码为504
#http_404 后端服务器返回的响应状态码为404
#off 停止将请求发送给下一台后端服务器
从以上几个指令可以看出,在默认配置下,后端节点一旦出现error和timeout情况时,nginx会通过proxy_next_upstream进行故障转移,将发往不健康节点的请求,自动转移至健康节点。其中timeout设置和proxy_send_timeout time、proxy_connect_timeout time、proxy_read_timeout time有关。除了error、timeout,我们可以设置更详细的触发条件,如http_502、http_503等。
注意:只有在没有向客户端发送任何数据以前,将请求转给下一台后端服务器才是可行的。也就是说,如果在传输响应到客户端时出现错误或者超时,这类错误是不可能恢复的。
2.健康检查
Syntax: server address [parameters];
Default: —
Context: upstream
max_fails=number 设定Nginx与服务器通信的尝试失败的次数。在fail_timeout参数定义的时间段内,如果失败的次数达到此值,Nginx就认为服务器不可用。此时在接下来的fail_timeout时间段,服务器不会再被尝试。失败的尝试次数默认是1。设为0就会停止统计尝试次数,即不对后端节点进行健康检查。认为服务器是一直可用的。fail_timeout=time 设定服务器被认为不可用的时间段以及统计失败尝试次数的时间段。在这段时间中,服务器失败次数达到指定的尝试次数,服务器就被认为不可用。
默认情况下,该超时时间是10秒。
二、nginx_upstream_check_module模块
借助淘宝技术团队开发的nginx模快nginx_upstream_check_module来检测后方realserver的健康状态,如果后端服务器不可用,则会将其踢出upstream,所有的请求不转发到这台服务器。当期恢复正常时,将其加入upstream。
在淘宝自己的tengine上是自带了该模块的,大家可以访问淘宝Tengine官网来获取该版本的nginx,也可以到Gitbub
如果没有使用淘宝的tengine的话,可以通过补丁的方式来添加该模块到我们自己的nginx中。
下载地址1:https://github.com/yaoweibin/nginx_upstream_check_module
下载地址2:https://download.csdn.net/download/m0_60821938/89913605
#打补丁
#注意不同版本对应的补丁
cd nginx-1.6.0
patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx1.6 --sbin-path=/usr/local/nginx1.6 --conf-path=/usr/local/nginx1.6/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_sub_module --with-pcre=/usr/local/src/nginx/pcre-8.36 --with-zlib=/usr/local/src/nginx/zlib-1.2.8 --add-module=/usr/local/src/nginx/ngx_cache_purge-2.1 --add-module=/usr/local/src/nginx/headers-more-nginx-module-master --add-module=/usr/local/src/nginx/nginx_upstream_check_module-mastermake
#不要执行make install命令cd /usr/local/nginx1.6
#备份命令
cp nginx nginx.bak
nginx -s stop
cp -r /usr/local/src/nginx/nginx-1.6.0/objs/nginx .
更多推荐
所有评论(0)