大家都知道,前段nginx做反向代理,如果后端服务器宕掉的话,nginx是不能把这台realserver提出upstream的,所以还会有请求转发到后端的这台realserver上面去,虽然nginx可以在localtion中启用proxy_next_upstream来解决返回给用户的错误页面,方法在:http://www.linuxyan.com/web-server/67.html,大家可以参考一下,但这个还是会把请求转发给这台服务器的,然后再转发给别的服务器,这样就浪费了一次转发,这次借助与淘宝技术团队开发的nginx模快nginx_upstream_check_module来检测后方realserver的健康状态,如果后端服务器不可用,则所以的请求不转发到这台服务器。
nginx安装健康检查模块
使用版本
nginx-1.2.9
下载补丁
https://github.com/yaoweibin/nginx_upstream_check_module
解压缩源码
cd /application/search/tmp/nginx-1.2.9
patch -p1 < /application/search/tmp/nginx_upstream_check_module-0.1.9/check_1.2.6+.patch
./configure \
--prefix=/application/search/usr/nginx-1.2.9 \
--with-http_ssl_module \
--add-module=/application/search/tmp/nginx_upstream_check_module-0.1.9
make && make install
配置方式
upstream myserver {
server 127.0.0.1:81;
server 127.0.0.1:82;
check interval=3000 rise=2 fall=1 timeout=1000 type=http;
#必须是HTTP/1.0
check_http_send "GET /_.gif HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server节点配置
location /load {
proxy_pass http://myserver/;
proxy_redirect http://myserver/ /;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
}
location /nstatus {
check_status;
access_log off;
#allow SOME.IP.ADD.RESS;
#deny all;
}
nginx安装健康检查模块
使用版本
nginx-1.2.9
下载补丁
https://github.com/yaoweibin/nginx_upstream_check_module
解压缩源码
cd /application/search/tmp/nginx-1.2.9
patch -p1 < /application/search/tmp/nginx_upstream_check_module-0.1.9/check_1.2.6+.patch
./configure \
--prefix=/application/search/usr/nginx-1.2.9 \
--with-http_ssl_module \
--add-module=/application/search/tmp/nginx_upstream_check_module-0.1.9
make && make install
配置方式
upstream myserver {
server 127.0.0.1:81;
server 127.0.0.1:82;
check interval=3000 rise=2 fall=1 timeout=1000 type=http;
#必须是HTTP/1.0
check_http_send "GET /_.gif HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
server节点配置
location /load {
proxy_pass http://myserver/;
proxy_redirect http://myserver/ /;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
}
location /nstatus {
check_status;
access_log off;
#allow SOME.IP.ADD.RESS;
#deny all;
}