首页 > 代码库 > 配置 Nginx 负载均衡监测节点状态

配置 Nginx 负载均衡监测节点状态

淘宝技术团队开发了一个 Tengine ( Nginx 的分支 ) 模块 nginx_upstream_check_module ,用于提供主动式后端服务器健康检查,通过它可以检测后端 realserver 的健康状态,如果后端 realserver 不可用,则所有的请求就不会转发到该节点上,需要通过打补丁的方式将该模块添加到 Nginx 中。

1、安装 nginx_upstream_check_module 模块

cd /usr/local/src
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
yum install -y unzip patch
upzip master
cd nginx-1.6.3
patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --add-module=../nginx_upstream_check_module-master/
make
mv /usr/local/nginx/sbin/nginx{,.ori}
cp ./objs/nginx /usr/local/nginx/sbin/
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -V

 

2、配置 Nginx 健康检查

[root@localhost ~]# cat /usr/local/nginx/conf/nginx.confworker_processes  1;    events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    
upstream static_pools {
server 192.168.123.103:80 weight=1;
server 192.168.123.104:80 weight=1;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
}

# 上面配置的意思是:对 static_pools 这个负载均衡条目中的所有节点,每隔 3 秒检测一次,请求 2 次正常则标记 realserver 状态为 up ,如果检测 5 次都失败,则标记 realserver 状态为 down ,超时时间为 1 秒,检查的协议是 HTTP

upstream default_pools{ server 192.168.123.103:80 weight=1;
}
server { listen 80; server_name bbs.abc.com; location / { root html/bbs; index index.html index.htm;
proxy_pass http://default_pools;
include proxy.conf; }
location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
proxy_pass http://static_pools;
include proxy.conf;
}

location /status {
check_status;
access_log off;
}
}
}
[root@localhost conf]# cat proxy.confproxy_set_header Host $host;proxy_set_header X-Forwarded-For $remote_addr;proxy_connect_timeout 60;proxy_send_timeout 60;proxy_read_timeout 60;proxy_buffer_size 4k;proxy_buffers 4 32k;proxy_busy_buffers_size 64k;proxy_temp_file_write_size 64k;
# 重启 Nginx ,注意不是重新加载/usr/local/nginx/sbin/nginx -t/usr/local/nginx/sbin/nginx -s stop/usr/local/nginx/sbin/nginx

 

3、查看结果

技术分享

 

 

 

 

    

配置 Nginx 负载均衡监测节点状态