nginx_upstream_check_module 是专门提供负载均衡器内节点的健康检查的外部模块,由淘宝的姚伟斌大神开发,通过它可以用来检测后端 realserver 的健康状态。如果后端 realserver 不可用,则后面的请求就不会转发到该节点上,并持续检查几点的状态。在淘宝自己的 tengine 上是自带了该模块。项目地址:https://github.com/yaoweibin/nginx_upstream_check_module
参考文档:https://www.cnblogs.com/paul8339/p/8124739.html
(1)源码编译安装tengine2.1.2版本
yum install pcre pcre-devel openssl openssl-devel gcc make zlib-devel wget -y
mkdir /tools
cd /tools/
wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz
useradd nginx -s /sbin/nologin
tar xf tengine-2.1.2.tar.gz
cd tengine-2.1.2
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx2.1.2 --with-http_stub_status_module --with-http_ssl_module --with-http_upstream_check_module --with-http_gzip_static_module
make && make install
ln -sv /usr/local/nginx2.1.2/ /usr/local/nginx
echo "export PATH=/usr/local/nginx/sbin:$PATH" >>/etc/profile
source /etc/profile
echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
nginx
cd /usr/local/nginx/conf
egrep -v "#|^$" nginx.conf.default >nginx.conf
(2)nginx配置
#vim /usr/local/nginx/conf.d/www.test.com.conf
upstream node {
ip_hash;
server 192.9.191.31:8001;
server 192.9.191.31:8002;
server 192.9.191.31:8003;
check interval=1000 rise=1 fall=1 timeout=1000 type=http;
check_http_send "GET /index.html HTTP/1.0\\r\\n\\r\\n";
check_http_expect_alive http_2xx http_3xx;
}
server {
listen 80;
server_name www.test.com;
location / {
proxy_pass http://node;
}
location /status { //开启状态页面
check_status;
access_log off;
allow 192.9.191.0/24;
deny all;
}
}
参数详解
check interval=1000 rise=1 fall=1 timeout=1000 type=http;
interval检测间隔时间,单位毫秒
rise请求1次正常的话,标记此realserver的状态为up
fall表示请求1次都失败的请求,标记此realserver的状态为down
timeout超时时间,单位毫秒
type是http类型
check_http_send "GET /index.html HTTP/1.0\\r\\n\\r\\n";
可以使用GET方法,POST,HEAD等方法获取资源 /index.html表示请求的资源,
check_http_expect_alive http_2xx http_3xx;
状态码是2xx和3xx就认为后端服务是正常的
(3)验证
http://www.test.com/status
断开1个服务