当前位置: 首页 > news >正文

nginx upstream健康检测

自带健康检测module

nginx本身提供有健康检测module ngx_http_upstream_module ,利用自身的module可以实现简单的健康检测

下载nginx模块
wget https://github.com/nginx/nginx/archive/release-1.19.3.tar.gz

解压nginx模块
tar -zxvf release-1.19.3.tar.gz

进入nginx模块目录
cd nginx-release-1.19.3

编译nginx带上ngx_http_upstream_module模块
./configure --add-module=../ngx_http_upstream_module

安装nginx
make && make install

重启nginx
nginx -s reload

第三方健康检测module

第三方插件的module,例如openresty的lua-resty-upstream-healthcheck

http {
    lua_package_path "/path/to/lua-resty-upstream-healthcheck/lib/?.lua;;";

    # sample upstream block:
    upstream foo.com {
        server 127.0.0.1:12354;
        server 127.0.0.1:12355;
        server 127.0.0.1:12356 backup;
    }

    # the size depends on the number of servers in upstream {}:
    lua_shared_dict healthcheck 1m;

    lua_socket_log_errors off;

    init_worker_by_lua_block {
        local hc = require "resty.upstream.healthcheck"

        local ok, err = hc.spawn_checker{
            shm = "healthcheck",  -- defined by "lua_shared_dict"
            upstream = "foo.com", -- defined by "upstream"
            type = "http", -- support "http" and "https"

            http_req = "GET /status HTTP/1.0\r\nHost: foo.com\r\n\r\n",
                    -- raw HTTP request for checking

            port = nil,  -- the check port, it can be different than the original backend server port, default means the same as the original backend server
            interval = 2000,  -- run the check cycle every 2 sec
            timeout = 1000,   -- 1 sec is the timeout for network operations
            fall = 3,  -- # of successive failures before turning a peer down
            rise = 2,  -- # of successive successes before turning a peer up
            valid_statuses = {200, 302},  -- a list valid HTTP status code
            concurrency = 10,  -- concurrency level for test requests
            -- ssl_verify = true, -- https type only, verify ssl certificate or not, default true
            -- host = foo.com, -- https type only, host name in ssl handshake, default nil
        }
        if not ok then
            ngx.log(ngx.ERR, "failed to spawn health checker: ", err)
            return
        end

        -- Just call hc.spawn_checker() for more times here if you have
        -- more upstream groups to monitor. One call for one upstream group.
        -- They can all share the same shm zone without conflicts but they
        -- need a bigger shm zone for obvious reasons.
    }

    server {
        ...

        # status page for all the peers:
        location = /status {
            access_log off;
            allow 127.0.0.1;
            deny all;

            default_type text/plain;
            content_by_lua_block {
                local hc = require "resty.upstream.healthcheck"
                ngx.say("Nginx Worker PID: ", ngx.worker.pid())
                ngx.print(hc.status_page())
            }
        }

	# status page for all the peers (prometheus format):
        location = /metrics {
            access_log off;
            default_type text/plain;
            content_by_lua_block {
                local hc = require "resty.upstream.healthcheck"
                st , err = hc.prometheus_status_page()
                if not st then
                    ngx.say(err)
                    return
                end
                ngx.print(st)
            }
        }
    }
}

相关文章:

  • Hello Vue!
  • sqli 靶场 Level23-Level30 wp
  • xshell是什么软件,1000字让你完全了解xshell
  • flink1.15.2 报错 processElement_split
  • Docker学习——④
  • yuv420并转为bgr
  • 【编程语言发展史】Unity开发语言的历史发展
  • ST 任意内核 移植freertos系统
  • 项目实战:中央控制器实现(2)-优化Controller,将共性动作抽取到中央控制器
  • Redis系列-Redis集群模式【8】
  • Qt 4.8.6 的下载与安装
  • VS2010配置gdal1.10.0 gdal1.10.1编译
  • 【原创】java+swing+mysql爱心捐赠管理系统设计与实现
  • mysql---存储引擎
  • 2、音视频基础
  • 配置OSPF特殊区域
  • 边缘计算多角色智能计量插座:用电监测和资产管理的未来智能化引擎
  • log4j CVE-2021-44228 RCE漏洞复现
  • wpf Grid布局详解 `Auto` 和 `*` 是两种常见的设置方式 行或列占多个单元格,有点像excel里的合并单元格。使其余的列平均分配剩余的空间
  • 嵌入式Linux HID多指触控/触摸设备报表描述符
  • 黄仁勋:新一代计算平台GB300三季度上市,AI计算能力每十年提升100万倍
  • 家庭医生可提前5天预约三甲医院号源,上海常住人口签约率达45%,
  • 波兰总统选举投票开始,将是对亲欧路线的一次严峻考验
  • “上海-日喀则”援藏入境旅游包机在沪首航
  • 《风林火山》千呼万唤始出来,戛纳首映后口碑崩盘?
  • 武康大楼再开发:一栋楼火还不够,要带火街区“朋友圈”