Skip to content

主动健康检查

  • tengine 版

https://github.com/yaoweibin/nginx_upstream_check_module

  • nginx 商业版

http://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html

下载 Nginx 1.20.2

wget https://nginx.org/download/nginx-1.20.2.tar.gz

解压

tar -xzvf nginx-1.20.2.tar.gz

打补丁

复制 check_1.20.1+.patch 内容到文件 patch 中

cd ./nginx-1.20.2 yum install -y patch patch -p1 < /root/patch

添加模块

cd ~ wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/refs/tags/v0.3.0.tar.gz tar -xzvf v0.3.0.tar.gz

cd ./nginx-1.20.2 ./configure --prefix=/usr/local/nginx --add-module=/root/nginx_upstream_check_module-0.3.0 make && make install

配置

nginx
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    upstream httpds {
        server 120.78.79.181:3000;
        # interval=3000 表示每隔 3s 检测一次
        # rise=2 表示请求 2 次都成功了状态为 up
        # fall=5 表示请求 5 次都失败了状态为 down
        # timeout=1000 表示请求超时时间
        check interval=3000 rise=2 fall=5 timeout=1000 type=http;
        check_http_send "HEAD / HTTP/1.0\r\n\r\n";
        check_http_expect_alive http_2xx http_3xx;
    }

    server {
        listen       808;
        server_name  localhost;

        # 访问 http://192.168.44.101:808/status
        location /status {
            check_status;
            access_log off;
        }
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://httpds;
        }

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