淘宝技术团队开发的nginx模快nginx_upstream_check_module能够检测后方realserver的健康状态,若是后端服务器不可用,则因此的请求不转发到这台服务器。mysql
[root@localhost src]# cd nginx-1.8.0/ [root@localhost nginx-1.8.0]# patch -p1 < /usr/local/src/nginx_upstream_check_module/check_1.7.2+.patch patching file src/http/modules/ngx_http_upstream_ip_hash_module.c patching file src/http/modules/ngx_http_upstream_least_conn_module.c
从新编译nginx 添加 –add-module=/usr/local/src/nginx_upstream_check_module/ [root@localhost nginx-1.8.0]# ./configure \ --prefix=/usr/local/nginx \ --user=www \ --group=www \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-file-aio \ --with-http_dav_module \ --with-pcre=/usr/local/src/pcre-8.37 \ --add-module=/usr/local/src/nginx_upstream_check_module/ [root@localhost nginx-1.8.0]# make #只Make不进行make install 4.配置健康检查 1 upstream resinserver_web{ 2 ip_hash; 3 server 127.0.0.1:8095; 4 server 127.0.0.1:8080; 5 check interval=3000 rise=2 fall=5 timeout=1000 type=http; 6 check_http_send "GET / HTTP/1.0\r\n\r\n"; #发送GET请求 7 check_http_expect_alive http_2xx http_3xx; #返回码是2xx,3xx表示UP 8 } 说明: interval:向后端发送的健康检查包的间隔,3000为3s。 fall(fall_count): 若是连续失败次数达到fall_count,服务器就被认为是down。 rise(rise_count): 若是连续成功次数达到rise_count,服务器就被认为是up。 timeout: 后端健康请求的超时时间,1000为1s。 default_down: 设定初始时服务器的状态,若是是true,就说明默认是down的,若是是false,就是up的。默认值是true,也就是一开始服务器认为是不可用,要等健康检查包达到必定成功次数之后才会被认为是健康的。 type:健康检查包的类型,如今支持如下多种类型 tcp:简单的tcp链接,若是链接成功,就说明后端正常。 ssl_hello:发送一个初始的SSL hello包并接受服务器的SSL hello包。 http:发送HTTP请求,经过后端的回复包的状态来判断后端是否存活。 mysql: 向mysql服务器链接,经过接收服务器的greeting包来判断后端是否存活。 ajp:向后端发送AJP协议的Cping包,经过接收Cpong包来判断后端是否存活。 port: 指定后端服务器的检查端口。 check_http_send 指令 该指令能够让负载均衡器模拟向后端realserver发送,监控检测的http包,模拟LVS的检测。 check_http_expect_alive 指令 check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ] 返回指定HTTP code,符合预期就算检测成功 5.配置状态查看 51 location /nstatus { 52 check_status; 53 access_log off; 54 allow 182.xx.xx.xx; 55 deny all; 56 } 从新reload下nginx而后查看页面就能看到后端服务器的状态