nginx_upstream_check_module模块


nginx_upstream_check_module模块
一个更专业的模块,来专门提供负载均衡器内节点的健康检查的。这个就是淘宝技术团队开发的 nginx 模块nginx_upstream_check_module,通过它可以用来检测后端 realserver 的健康状态。如果后端realserver不可用,则所有的请求就不会转发到该节点上。
在淘宝自己的 tengine 上是自带了该模块的,大家可以访问淘宝tengine的官网来获取该版本的nginx,官方地址:http://tengine.taobao.org
如果我们没有使用淘宝的 tengine 的话,可以通过补丁的方式来添加该模块到我们自己的 nginx 中。我们业务线上就是采用该方式进行添加的。
下面是部署流程!

1、下载nginx_upstream_check_module模块
[root@localhost ~]# cd /usr/local/src
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
unzip master
[root@localhost /usr/local/src]# ll -d nginx_upstream_check_module-master
drwxr-xr-x. 6 root root 4096 Dec  1 02:28 nginx_upstream_check_module-master

2、为nginx打补丁
[root@localhost /usr/local/src]# cd nginx-1.6.0 # 进入nginx的源码目录
[root@localhost nginx-1.6.0]# patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch
[root@localhost nginx-1.6.0]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.6.0 --with-http_ssl_module --with-openssl=/usr/local/src/openssl-0.9.8q --with-pcre=/usr/local/src/pcre-8.32 --add-module=/usr/local/src/nginx_concat_module/ --add-module=../nginx_upstream_check_module-master/
make (注意:此处只make,编译参数需要和之前的一样)
[root@localhost nginx-1.6.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-1.6.0.bak
[root@localhost nginx-1.6.0]# cp ./objs/nginx /usr/local/nginx/sbin/
[root@localhost nginx-1.6.0]# /usr/local/nginx/sbin/nginx -t  # 检查下是否有问题
[root@localhost nginx-1.6.0]# kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

3、在nginx.conf配置文件里面的upstream加入健康检查,如下:

upstream name {
       server 192.168.0.21:80;
       server 192.168.0.22:80;
       check interval=3000 rise=2 fall=5 timeout=1000 type=http;
         
}
上面配置的意思是,对name这个负载均衡条目中的所有节点,每个3秒检测一次,请求2次正常则标记 realserver状态为up,如果检测 5 次都失败,则标记 realserver的状态为down,超时时间为1秒。

这里列出nginx_upstream_check_module模块所支持的指令意思:
Syntax: check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true|false] [type=tcp|http|ssl_hello|mysql|ajp] [port=check_port]
Default: 如果没有配置参数,默认值是:interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp
Context: upstream
该指令可以打开后端服务器的健康检查功能。

指令后面的参数意义是:
    - interval:向后端发送的健康检查包的间隔。
    - fall(fall_count): 如果连续失败次数达到fall_count,服务器就被认为是down。
    - rise(rise_count): 如果连续成功次数达到rise_count,服务器就被认为是up。
    - timeout: 后端健康请求的超时时间。
    - 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: 指定后端服务器的检查端口。你可以指定不同于真实服务的后端服务器的端口,比如后端提供的是443端口的应用,你可以去检查80端口的状态来判断后端健康状况。默认是0,表示跟后端server提供真实服务的端口一样。该选项出现于Tengine-1.4.0。

Syntax: check_keepalive_requests request_num
Default: 1
Context: upstream
该指令可以配置一个连接发送的请求数,其默认值为1,表示Tengine完成1次请求后即关闭连接。

Syntax: check_http_send http_packet
Default: "GET / HTTP/1.0\r\n\r\n"
Context: upstream
该指令可以配置http健康检查包发送的请求内容。为了减少传输数据量,推荐采用"HEAD"方法。

当采用长连接进行健康检查时,需在该指令中添加keep-alive请求头,如:"HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n"。 同时,在采用"GET"方法的情况下,请求uri的size不宜过大,确保可以在1个interval内传输完成,否则会被健康检查模块视为后端服务器或网络异常。

Syntax: check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
Default: http_2xx | http_3xx
Context: upstream
该指令指定HTTP回复的成功状态,默认认为2XX和3XX的状态是健康的。

Syntax: check_shm_size size
Default: 1M
Context: http
所有的后端服务器健康检查状态都存于共享内存中,该指令可以设置共享内存的大小。默认是1M,如果你有1千台以上的服务器并在配置的时候出现了错误,就可能需要扩大该内存的大小。

Syntax: check_status [html|csv|json]
Default: check_status html
Context: location
显示服务器的健康状态页面。该指令需要在http块中配置。

在Tengine-1.4.0以后,你可以配置显示页面的格式。支持的格式有: html、csv、 json。默认类型是html。

你也可以通过请求的参数来指定格式,假设‘/status’是你状态页面的URL, format参数改变页面的格式,比如:

/status?format=html
/status?format=csv
/status?format=json
同时你也可以通过status参数来获取相同服务器状态的列表,比如:

/status?format=html&status=down
/status?format=csv&status=up
下面是一个状态也配置的范例:

http {
      server {
            location /nstatus {
                   check_status;
                   access_log off;
                   #allow IP;
                   #deny all;
            }
      }
}
 

nginx.conf配置案例

#user  nobody;
worker_processes  10;
worker_rlimit_nofile 65535;
pid        /data/soft/nginx/nginx.pid;

events {
    use epoll;
    worker_connections 65535;
}


http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$upstream_addr"  "$request_body"';
    access_log /data/soft/nginx/log/access_log main;
    error_log  /data/soft/nginx/log/error_log  debug_http;
    client_max_body_size 30m;
    include       mime.types;
    default_type  application/octet-stream;


    sendfile        on;
    keepalive_timeout  65;


    server {
        listen       8080;
        server_name  localhost;


        location / {
           root html;
           index index.html index.htm;
           proxy_pass http://demo1;
           proxy_redirect off;
           proxy_set_header Host $host:$server_port;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /nstatus {
            check_status;
            access_log off;
            #allow IP;
            #deny all;
        }

    }

    upstream demo1 {
       sticky name=demo01 path=/;
       server 127.0.0.1:18080;
       
       #http健康检查相关配置
       check interval=3000 rise=2 fall=3 timeout=3000 type=http;
       #/health/status为后端健康检查接口
       check_http_send "GET / HTTP/1.0\r\n\r\n";
       check_http_expect_alive http_2xx http_3xx;
      
    }


}

 

 

 

http://192.168.23.101:8080/nstatus

http://192.168.23.101:8080/

 


 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值