1.下载nginx_upstream_check_module模块

nginx_upstream_check_module-master.zip

[root@localhost /home ]#wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master

2.解压

将nginx_upstream_check_module-master.zip解压到/usr/local

3.进入nginx源码目录

[root@localhost nginx-1.16.1]#

备注:如果是已安装已启动nginx,请先停止nginx服务。

4.打补丁

[root@localhost nginx-1.16.1]#patch -p1 < /usr/local/nginx_upstream_check_module-master/check_1.14.0+.patch

这里要留意,找寻与nginx版本配套的补丁。我当前安装的nginx是1.16.1,因此补丁为check_1.14.0+.patch
如果patch命令不存在,请先安装此命令。

5.重新配置模块

#配置---加了健康检查模块
[root@localhost nginx-1.16.1]#./configure --prefix=/usr/local/nginx-1.16.1-use --with-pcre=/usr/local/pcre-8.36 --with-zlib=/usr/local/zlib-1.2.8 --with-http_stub_status_module --with-openssl=/usr/bin/openssl --add-module=/usr/local/nginx_upstream_check_module-master

#编译并安装nginx
[root@localhost nginx-1.16.1]#make && make install 
#查看配置是否成功
[root@localhost nginx-1.16.1]#/usr/local/nginx-1.16.1-use/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --prefix=/usr/local/nginx-1.16.1-use --with-pcre=/usr/local/pcre-8.36 --with-zlib=/usr/local/zlib-1.2.8 --with-http_stub_status_module --with-openssl=/usr/bin/openssl --add-module=/usr/local/nginx_upstream_check_module-master

6.修改nginx配置文件

upstream lunxun{
        ip_hash;
        server localhost:9595;
        server localhost:9696;
        #对name这个负载均衡条目中的所有节点,每个3秒检测一次,请求2次正常则标记 realserver状态为up,
        #如果检测 5 次都失败,则标记 realserver的状态为down,超时时间为1秒
        check interval=3000 rise=2 fall=5 timeout=1000 type=http;
    }
    

    #检查状态,可在浏览器访问http://xxxx/nstatus
    location /nstatus {
       check_status;
       access_log off;
       #allow IP;
       #deny all;
    }

生效 nginx_upstream_check_module模块,nginx -s reload

nginx配置健康检查 nginx健康检查模块_服务器

说明配置成功了。

名称解释:

1、语法: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

2、指令后面的参数意义是:
 - 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。    
    

 3、配置一个连接发送的请求数,其默认值为1,表示Tengine完成1次请求后即关闭连接。

语法: check_keepalive_requests request_num
默认值: 1
上下文(命令位置): upstream

 4、配置http健康检查包发送的请求内容。为了减少传输数据量,推荐采用"HEAD"方法。

语法: check_http_send http_packet
默认值: "GET / HTTP/1.0\r\n\r\n"
上下文(命令位置): upstream

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

 5、指定HTTP回复的成功状态,默认认为2XX和3XX的状态是健康的。

语法: check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]
默认值: http_2xx | http_3xx
上下文(命令位置): upstream

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

语法: check_shm_size size
默认值: 1M
上下文(命令位置): http

 7、显示服务器的健康状态页面。该指令需要在http块中配置。

语法: check_status [html|csv|json]
默认值: check_status html
上下文(命令位置): location

在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