资讯专栏INFORMATION COLUMN

ngx_healthcheck_module intro

wenshi11019 / 360人阅读

摘要:相比之下,这个第三方模块可以对后端服务器提供主动式的健康状态检测。向后端发送的健康检查包的间隔。如果连续成功次数达到,服务器就被认为是。只有配置块中支持类型检查发送请求,通过后端的回复包的状态来判断后端是否存活。指定后端服务器的检查端口。

ngx-healthcheck-module

Health-checker for Nginx upstream servers (support http upstream && stream upstream)

该模块可以为Nginx提供主动式后端服务器健康检查的功能(同时支持四层和七层后端服务器的健康检测)。

Table of Contents

Status

Description

Installation

Usage

Synopsis && Directive

healthcheck

check

Bugs and Patches

Author

Copyright and License

See Also

Status

This nginx module is still under development, you can help improve and it.

这个项目还在开发中完善中,欢迎贡献代码,或报告bug。一起使它变得更好。

Description

当你使用nginx作为负载均衡器时,nginx原生只提供了基本的重试方式来保证访问到正常的后端服务器。

相比之下,这个nginx第三方模块可以对后端服务器提供主动式的健康状态检测。
它维护了一个后端服务器列表,保证新的请求直接发送到一个健康的后端服务器。

主要特性:

同时支持四层和七层后端服务器的健康检测

四层支持的检测类型:tcp / udp / http

七层支持的检测类型:http / fastcgi

提供一个统一的http状态查询接口,输出格式:html / json / csv

Installation
git clone https://github.com/nginx/nginx/nginx.git
git clone https://github.com/zhouchangxun/ngx_healthcheck_module.git

cd nginx/; 
git apply ../ngx_healthcheck_module/nginx-stable-1.12+.patch

./auto/configure --with-stream --add-module=../ngx_healthcheck_module/
make && make install

Back to TOC

Usage nginx.conf example
user  root;
worker_processes  1;
error_log  logs/error.log  info;
#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    server {
        listen 80;
        # status interface
        location /status {
            healthcheck_status;
        }
        # http front
        location / { 
          proxy_pass http://http-cluster;
        }   
    }
    # as a backend server.
    server {
        listen 8080;
        location / {
          root html;
        }
    }
    
    upstream http-cluster {
        # simple round-robin
        server 127.0.0.1:8080;
        server 127.0.0.2:81;

        check interval=3000 rise=2 fall=5 timeout=5000 type=http;
        check_http_send "GET / HTTP/1.0

";
        check_http_expect_alive http_2xx http_3xx;
    }
}

stream {
    upstream tcp-cluster {
        # simple round-robin
        server 127.0.0.1:22;
        server 192.168.0.2:22;
        check interval=3000 rise=2 fall=5 timeout=5000 default_down=true type=tcp;
    }
    server {
        listen 522;
        proxy_pass tcp-cluster;
    }
    
    upstream udp-cluster {
        # simple round-robin
        server 127.0.0.1:53;
        server 8.8.8.8:53;
        check interval=3000 rise=2 fall=5 timeout=5000 default_down=true type=udp;
    }
    server {
        listen 53;
        proxy_pass udp-cluster;
    }
    
}
status interface

One typical output is

root@changxun-PC:~/nginx-dev/ngx_healthcheck_module# curl localhost/status
{"servers": {
  "total": 6,
  "generation": 3,
  "http": [
    {"index": 0, "upstream": "http-cluster", "name": "127.0.0.1:8080", "status": "up", "rise": 119, "fall": 0, "type": "http", "port": 0},
    {"index": 1, "upstream": "http-cluster", "name": "127.0.0.2:81", "status": "down", "rise": 0, "fall": 120, "type": "http", "port": 0}
  ],
  "stream": [
    {"index": 0, "upstream": "tcp-cluster", "name": "127.0.0.1:22", "status": "up", "rise": 22, "fall": 0, "type": "tcp", "port": 0},
    {"index": 1, "upstream": "tcp-cluster", "name": "192.168.0.2:22", "status": "down", "rise": 0, "fall": 7, "type": "tcp", "port": 0},
    {"index": 2, "upstream": "udp-cluster", "name": "127.0.0.1:53", "status": "down", "rise": 0, "fall": 120, "type": "udp", "port": 0},
    {"index": 3, "upstream": "udp-cluster", "name": "8.8.8.8:53", "status": "up", "rise": 3, "fall": 0, "type": "udp", "port": 0}
  ]
}}
root@changxun-PC:~/nginx-dev/ngx_healthcheck_module# 

Back to TOC

Synopsis check

Syntax:

check interval=milliseconds  
[fall=count] [rise=count] [timeout=milliseconds]
[default_down=true|false] [type=tcp|udp|http] [port=check_port]

Default: interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp

Context: http/upstream || stream/upstream

该指令可以打开后端服务器的健康检查功能。

Detail:

interval:向后端发送的健康检查包的间隔。

fall(fall_count): 如果连续失败次数达到fall_count,服务器就被认为是down。

rise(rise_count): 如果连续成功次数达到rise_count,服务器就被认为是up。

timeout: 后端健康请求的超时时间。

default_down: 设定初始时服务器的状态,如果是true,就说明默认是down的,如果是false,就是up的。
默认值是true,也就是一开始服务器认为是不可用,要等健康检查包达到一定成功次数以后才会被认为是健康的。

type:健康检查包的类型,现在支持以下多种类型

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

udp:简单的发送udp报文,如果收到icmp error(主机或端口不可达),就说明后端异常。(只有stream配置块中支持udp类型检查)

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

port: 指定后端服务器的检查端口。你可以指定不同于真实服务的后端服务器的端口,

比如后端提供的是443端口的应用,你可以去检查80端口的状态来判断后端健康状况。默认是0,表示跟后端server提供真实服务的端口一样。

A example as followed:

stream {
    upstream tcp-cluster {
        # simple round-robin
        server 127.0.0.1:22;
        server 192.168.0.2:22;
        check interval=3000 rise=2 fall=5 timeout=5000 default_down=true type=tcp;
    }
    server {
        listen 522;
        proxy_pass tcp-cluster;
    }
    ...
}
healthcheck

Syntax: healthcheck_status [html|csv|json]

Default: healthcheck_status html

Context: http/server/location

A example as followed:

http {
    server {
        listen 80;
        
        # status interface
        location /status {
            healthcheck_status;
        }
     ...
}

Back to TOC

Todo List

增加测试用例

整理、优化代码

规范代码中的log输出

Back to TOC

Bugs and Patches

Please report bugs

create GitHub Issue,

or submit patches by

new Pull request

Back to TOC

Author

Chance Chou (周长勋) .

Back to TOC

Copyright and License

The health check part is based on Yaoweibin"s

healthcheck module nginx_upstream_check_module
();

This module is licensed under the BSD license.

Copyright (C) 2017-, by Changxun Zhou

Copyright (C) 2014 by Weibin Yao

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Back to TOC

See Also

nginx: http://nginx.org

Back to TOC

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/39889.html

相关文章

  • Linkedin Intro的钓鱼研究

    摘要:年月日,我联系了的安全团队,并会在近期发布修复补丁来解决下面的问题。有关月日,推出一个名为的应用程序。会对你的做些什么为了更好地观察的行为,目前我正对其进行更深入的分析研究,并很快就会发布。钓鱼去啦为达教学目的,我已经建立了一个基本的模板。 2013年10月28日,我联系了Linkedin的安全团队,并会在近期发布修复补丁来解决下面的问题。这个修复程序适用于随机生成ID的styling...

    lykops 评论0 收藏0
  • Linkedin Intro的钓鱼研究

    摘要:年月日,我联系了的安全团队,并会在近期发布修复补丁来解决下面的问题。有关月日,推出一个名为的应用程序。会对你的做些什么为了更好地观察的行为,目前我正对其进行更深入的分析研究,并很快就会发布。钓鱼去啦为达教学目的,我已经建立了一个基本的模板。 2013年10月28日,我联系了Linkedin的安全团队,并会在近期发布修复补丁来解决下面的问题。这个修复程序适用于随机生成ID的styling...

    wenshi11019 评论0 收藏0
  • 由一道题图解JavaScript的作用域

    摘要:当我将此题的作用域链画出来之后,终于感觉作用域入门了。创建函数的作用域链,并初始化为函数的所包含的对象,即包含了的作用域链。 作用域 为了理解作用域,跪看了好几篇大神的博文,终于略知一二。 1.题目 其中,看到这样一道题(稍作修改): function factory() { var name = laruence; var intro = function(){...

    nevermind 评论0 收藏0
  • JQuery基础——选择器

    摘要:通过,可以选取查询,元素,并对它们执行操作可以简写成元素选择器使用选择器来选取元素。属性选择器使用表达式来选择带有给定属性的元素。选择器选择器可用于改变元素的属性。 通过 jQuery,可以选取(查询,query) HTML 元素,并对它们执行操作(actions) $(document).ready(function(){})可以简写成$(funtion(){}) jQuery 元...

    taowen 评论0 收藏0
  • JQuery基础——选择器

    摘要:通过,可以选取查询,元素,并对它们执行操作可以简写成元素选择器使用选择器来选取元素。属性选择器使用表达式来选择带有给定属性的元素。选择器选择器可用于改变元素的属性。 通过 jQuery,可以选取(查询,query) HTML 元素,并对它们执行操作(actions) $(document).ready(function(){})可以简写成$(funtion(){}) jQuery 元...

    GraphQuery 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<