nginx通过nginx_upstream_check_module实现后端健康检查

 更新时间:2024年08月08日 10:44:07   作者:long_2145  
nginx的健康检查有两种,一种是被动健康检查,也就是nginx自带健康检查模块ngx_http_upstream_module,另一种就是主动健康检查,使用第三方模块nginx_upstream_check_module,下面就来介绍一下,感兴趣的可以了解一下

1、简介说明

nginx是常用的反向代理和负载均衡服务,具有强大并发能力、稳定性、丰富的功能集、低资源的消耗。

nginx自身是没有针对后端节点健康检查的,但是可以通过默认自带的ngx_http_proxy_module 模块和ngx_http_upstream_module模块中的相关指令来完成当后端节点出现故障时,自动切换到健康节点来提供访问。

nginx的健康检查有两种,一种是被动健康检查,也就是nginx自带健康检查模块ngx_http_upstream_module,另一种就是主动健康检查,使用第三方模块nginx_upstream_check_module

nginx被动健康检查的缺点:

  • nginx只有被访问时,才会发起对后端节点探测。如果本次请求中,节点正好出现故障,nginx依然会将请求转交给故障的节点,然后再转交给健康的节点处理。所以不会影响到这次请求的正常进行。由于多了一次转发,会影响效率。
  • 无法做到预警。

nginx主动健康检查

  • 淘宝开发的tengine自带心跳检测模块,若健康检查包类型为http,在开启健康检查功能后,nginx会根据设置的间隔向后端服务器端口发送健康检查包,并根据期望的HTTP状态码来判断服务是否健康。后端节点不可用,则请求不会转发到故障节点。
  • 故障节点恢复后,请求正常转发

nginx_upstream_check_module是一个专门提供负载均衡器内节点的健康检查的,这个是淘宝技术团队开发的 nginx 模块 ,通过它可以用来检测后端 realserver 的健康状态。如果后端 realserver 不可用,则所以的请求就不会转发到该节点上。

淘宝的 tengine 自带了该模块,官方地址:http://tengine.taobao.org。如果是 nginx,可以通过补丁的方式来添加该模块到 nginx 中(https://github.com/yaoweibin/nginx_upstream_check_module)。

2、安装配置

2.1 下载nginx 和 nginx_upstream_check_module

# cd /usr/local/src
# wget https://nginx.org/download/nginx-1.20.2.tar.gz
# tar zxf nginx-1.20.2.tar.gz
# git clone https://github.com/yaoweibin/nginx_upstream_check_module.git
# ls -l
total 1044
drwxr-xr-x 8 1001 1001     158 Nov 16  2021 nginx-1.20.2
-rw-r--r-- 1 root root 1062124 Nov 16  2021 nginx-1.20.2.tar.gz
drwxr-xr-x 7 root root    4096 Jul 20 23:50 nginx_upstream_check_module

2.2 为nginx打补丁并编译安装

# cd nginx-1.20.2
# patch -p1 < /usr/local/src/nginx_upstream_check_module/check_1.20.1+.patch
patching file src/http/modules/ngx_http_upstream_hash_module.c
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h

# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_geoip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-pcre --with-pcre-jit --with-stream_geoip_module --add-module=/usr/local/src/nginx_upstream_check_module
# make && make install

2.3 配置案例及效果

# cat conf/conf.d/nginx_upstream_check.conf
upstream cluster{
    server 192.168.100.210:9091;
    server 192.168.100.210:9092;
    check interval=3000 rise=2 fall=2 timeout=3000 type=http;
    check_http_send "GET /ops/v1/check HTTP/1.0\r\n\r\n ";
    check_http_expect_alive http_2xx http_3xx;
}


server {
    listen       8888;
    server_name  localhost;
    #charset koi8-r;
    access_log  logs/nginx_upstream_check.log  main;

    location / {
        root   html;
        index  index.html;
    }

    location ^~ /nginxServer/ {
        proxy_pass http://cluster/;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass_request_body on;
        proxy_set_header   Cookie $http_cookie;
        real_ip_header X-Real-IP;
    }

    location /nginx_status {
        check_status;
        access_log off;
    }
}

2.4 语法及指令介绍

check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true|false] [type=tcp|http|ssl_hello|mysql|ajp] [port=check_port]
  • interval: 向后端发送的健康检查包的间隔,单位为毫秒
  • rise: 如果连续成功次数达到rise_count,服务器就被认为是up
  • fall: 如果连续失败次数达到fall_count,服务器就被认为是down
  • timeout: 后端健康请求的超时时间,单位为毫秒
  • type: 健康检查包的类型,支持类型如下:

    tcp:简单的tcp连接,如果连接成功,就说明后端正常

    ssl_hello:发送一个初始的SSL hello包并接受服务器的SSL hello包

    http:发送HTTP请求,通过后端的回复包的状态来判断后端是否存活

    mysql: 向mysql服务器连接,通过接收服务器的greeting包来判断后端是否存活

    ajp:向后端发送AJP协议的Cping包,通过接收Cpong包来判断后端是否存活

  • default_down: 设定初始时服务器的状态,如果是true,就说明默认是down的,如果是false,就是up的。默认值是true,也就是一开始服务器认为是不可用,要等健康检查包达到一定成功次数以后才会被认为是健康的
  • port: 指定后端服务器的检查端口。你可以指定不同于真实服务的后端服务器的端口,比如后端提供的是443端口的应用,你可以去检查80端口的状态来判断后端健康状况。默认是0,表示跟后端server提供真实服务的端口一样

2.5 check_http_send功能

用法:check_http_send "GET / HTTP/1.0\r\n\r\n"
默认值: "GET / HTTP/1.0\r\n\r\n"
位置:upstream块
说明:http://ip:port/做健康检测

2.6 监控

You can specify the default display format. The formats can be `html`,
    `csv` or `json`. The default type is `html`. It also supports to specify
    the format by the request argument. Suppose your `check_status` location
    is '/status', the argument of `format` can change the display page's
    format. You can do like this:

        /status?format=html
        /status?format=csv
        /status?format=json

    At present, you can fetch the list of servers with the same status by
    the argument of `status`. For example:

        /status?format=html&status=down
        /status?format=csv&status=up

到此这篇关于nginx通过nginx_upstream_check_module实现后端健康检查的文章就介绍到这了,更多相关nginx 后端健康检查内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • Windows设置nginx开机自启动的方法

    Windows设置nginx开机自启动的方法

    这篇文章主要介绍了Windows设置nginx开机自启动的方法,通过两种方式实现nginx的开机自启动:winws和window计划程序,每种方式给大家介绍的非常详细需要的朋友可以参考下
    2022-11-11
  • Nginx高可用Keepalived的具体使用

    Nginx高可用Keepalived的具体使用

    Keepalived是一种实现高可用的软件,基于VRRP协议,可以自动将虚拟IP地址切换到备用服务器,具有一定的参考价值,感兴趣的可以了解一下
    2024-12-12
  • nginx如何通过proxy_pass设置反向代理,隐藏端口号

    nginx如何通过proxy_pass设置反向代理,隐藏端口号

    这篇文章主要介绍了nginx如何通过proxy_pass设置反向代理,隐藏端口号方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-01-01
  • Nginx中IF语句实现数学比较功能

    Nginx中IF语句实现数学比较功能

    这篇文章主要介绍了Nginx中IF语句实现数学比较功能,即在Nginx中用if判断数字大小,类似编程语言中的逻辑比较,需要的朋友可以参考下
    2015-02-02
  • nginx 服务绑定域名的方法

    nginx 服务绑定域名的方法

    这篇文章主要介绍了nginx 服务绑定域名的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • nginx配置解决跨域访问的方法详解

    nginx配置解决跨域访问的方法详解

    这篇文章主要为大家详细介绍了nginx如何配置解决跨域访问的方法,文中的示例代码讲解详细,具有一定的参考价值,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-10-10
  • Nginx http升级到https的完整步骤

    Nginx http升级到https的完整步骤

    这篇文章主要给大家介绍了关于Nginx http升级到https的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用Nginx具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-06-06
  • Nginx HTTPS实现原理及配置实践

    Nginx HTTPS实现原理及配置实践

    本文主要介绍了Nginx HTTPS实现原理及配置实践,详细的介绍了HTTPS原理,实现及其HTTPS单台配置实践,集群配置、优化等,具有一定的参考价值,感兴趣的可以了解一下
    2023-09-09
  • nginx ssl免密码重启教程详解

    nginx ssl免密码重启教程详解

    这篇文章给大家介绍了nginx 如何启动以及nginx ssl 免密码重启 的方法,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-01-01
  • nginx 如何实现读写限流的方法

    nginx 如何实现读写限流的方法

    这篇文章主要介绍了nginx 如何实现读写限流的方法的相关资料,这里提供实例代码及如何配置,需要的朋友可以参考下
    2016-12-12

最新评论