Nginx中文文档
DocsHome
  • 贡献导引
  • 介绍
    • 安装 nginx
    • 从源码构建 nginx
    • 初学者指南
    • 管理指南
    • 控制 nginx
    • 连接处理方式
    • 设置哈希
    • 调试日志
    • 记录日志到 syslog
    • 配置文件度量单位
    • 命令行参数
    • Windows 下的 nginx
    • QUIC 和 HTTP/3 支持
    • nginx 如何处理请求
    • 服务器名称
    • 使用 nginx 作为 HTTP 负载均衡器
    • 配置 HTTPS 服务器
    • nginx 如何处理 TCP/UDP 会话
    • 关于 nginScript
  • 其他
    • Linux 软件包
  • How-To
    • 在 Win32 平台上使用 Visual C 构建 nginx
    • 设置 Amazon EC2 的 Nginx Plus 环境
    • 使用 DTrace pid 提供程序调试 nginx
    • 转换重写规则
    • WebSocket 代理
  • 开发
    • 贡献指南
    • 开发指南
  • 模块参考
  • 核心功能
    • HTTP
      • ngx_http_core_module
      • ngx_http_access_module
      • ngx_http_addition_module
      • ngx_http_api_module
      • ngx_http_auth_basic_module
      • ngx_http_auth_jwt_module
      • ngx_http_auth_request_module
      • ngx_http_autoindex_module
      • ngx_http_browser_module
      • ngx_http_charset_module
      • ngx_http_dav_module
      • ngx_http_empty_gif_module
      • ngx_http_f4f_module
      • ngx_http_fastcgi_module
      • ngx_http_flv_module
      • ngx_http_geo_module
      • ngx_http_geoip_module
      • ngx_http_grpc_module
      • ngx_http_gunzip_module
      • ngx_http_gzip_module
      • ngx_http_gzip_static_module
      • ngx_http_headers_module
      • ngx_http_hls_module
      • ngx_http_image_filter_module
      • ngx_http_index_module
      • ngx_http_js_module
      • ngx_http_keyval_module
      • ngx_http_limit_conn_module
      • ngx_http_limit_req_module
      • ngx_http_log_module
      • ngx_http_map_module
      • ngx_http_memcached_module
      • ngx_http_mirror_module
      • ngx_http_mp4_module
      • ngx_http_perl_module
      • ngx_http_proxy_module
      • ngx_http_random_index_module
      • ngx_http_realip_module
      • ngx_http_referer_module
      • ngx_http_rewrite_module
      • ngx_http_scgi_module
      • ngx_http_secure_link_module
      • ngx_http_session_log_module
      • ngx_http_slice_module
      • ngx_http_spdy_module(过时)
      • ngx_http_split_clients_module
      • ngx_http_ssi_module
      • ngx_http_ssl_module
      • ngx_http_status_module(过时)
      • ngx_http_stub_status_module
      • ngx_http_sub_module
      • ngx_http_upstream_module
      • ngx_http_upstream_conf_module
      • ngx_http_upstream_hc_module
      • ngx_http_userid_module
      • ngx_http_uwsgi_module
      • ngx_http_v2_module
      • ngx_http_xslt_module
    • Mail
      • ngx_mail_core_module
      • ngx_mail_auth_http_module
      • ngx_mail_proxy_module
      • ngx_mail_ssl_module
      • ngx_mail_imap_module
      • ngx_mail_pop3_module
      • ngx_mail_smtp_module
    • Stream
      • ngx_stream_core_module
      • ngx_stream_access_module
      • ngx_stream_geo_module
      • ngx_stream_geoip_module
      • ngx_stream_js_module
      • ngx_stream_keyval_module
      • ngx_stream_limit_conn_module
      • ngx_stream_log_module
      • ngx_stream_map_module
      • ngx_stream_proxy_module
      • ngx_stream_realip_module
      • ngx_stream_return_module
      • ngx_stream_split_clients_module
      • ngx_stream_ssl_module
      • ngx_stream_ssl_preread_module
      • ngx_stream_upstream_module
      • ngx_stream_upstream_hc_module
      • ngx_stream_zone_sync_module
    • 其他
      • ngx_google_perftools_module
由 GitBook 提供支持
在本页
  • 示例配置
  • 指令
  • health_check
  • match
  • 原文档
  1. 核心功能
  2. HTTP

ngx_http_upstream_hc_module

上一页ngx_http_upstream_conf_module下一页ngx_http_userid_module

最后更新于5年前

ngx_http_upstream_hc_module 模块允许对周边 location 中引用的中的服务器进行定期健康检查。服务器组必须驻留在中。

如果健康检查失败,则服务器将被视为运行状况不佳。如果为同一组服务器定义了多个健康检查,则任何检查的单个故障都将使相应的服务器被视为运行状况不佳。客户端请求不会传递到处于 checking 状态的服务器和不健康的服务器。

请注意,与健康检查一起使用时,大多数变量都将为空值。

该模块作为部分提供。

示例配置

upstream dynamic {
    zone upstream_dynamic 64k;

    server backend1.example.com      weight=5;
    server backend2.example.com:8080 fail_timeout=5s slow_start=30s;
    server 192.0.2.1                 max_fails=3;

    server backup1.example.com:8080  backup;
    server backup2.example.com:8080  backup;
}

server {
    location / {
        proxy_pass http://dynamic;
        health_check;
    }
}

使用此配置,nginx 将每隔五秒向 backend 组中的每个服务器发送 / 请求。如果发生任何通信错误或超时,或代理服务器以 2xx 或 3xx 以外的状态码响应,则健康检查将失败,并且服务器将被视为运行状况不佳。

http {
    server {
    ...
        location / {
            proxy_pass http://backend;
            health_check match=welcome;
        }
    }

    match welcome {
        status 200;
        header Content-Type = text/html;
        body ~ "Welcome to nginx!";
    }
}

此配置显示,发起传递一个健康检查,健康检查请求的响应应该为成功,状态码为 200,并在正文中包含 Welcome to nginx!。

指令

health_check

-

说明

语法

health_check [parameters];

默认

——

上下文

location

支持以下可选参数:

  • interval=time

    设置两次连续健康检查之间的间隔时间,默认为 5 秒

  • jitter=time

    设置每个健康检查随机延迟的时间,默认情况下,没有延迟

  • fails=number

    设置特定服务器的连续失败健康检查的数量,在此之后,此服务器将被视为不健康,默认情况下为 1。

  • passes=number

    设置特定服务器的连续通过健康检查的数量,在此之后服务器将被视为健康,默认情况下为 1。

  • uri=uri

    定义健康检查请求中使用的 URI,默认情况下为 /。

  • mandatory

    设置服务器的初始 checking 状态,直到第一次运行健康检查完成(1.11.7)。客户端请求不会传递到处于 checking 状态的服务器。如果未指定参数,则服务器开始将被视为健康。

  • match=name

    指定 match 块配置测试,为响应通过测试做参照,以便通过健康检查。默认情况下,响应的状态码应为 2xx 或 3xx。

  • port=number

match

-

说明

语法

match name { ... };

默认

——

上下文

http

定义用于验证健康检查请求的响应的具名测试集。

可以在响应中测试以下项目:

  • status 200;

    状态码为 200

  • status ! 500;

    状态码非 500

  • status 200 204;

    状态码为 200 或 204

  • status ! 301 302;

    状态码既不是 301 也不是 302

  • status 200-399;

    状态码在 200 到 399 之间

  • status ! 400-599;

    状态码不在 400 到 599 之间

  • status 301-303 307;

    状态码为 301、302、303 或 307

  • header Content-Type = text/html;

    header 包含值为 text/html 的 Content-Type

  • header Content-Type != text/html;

    header 包含 Content-Type,其值不是 text/html

  • header Connection ~ close;

    header 包含 Connection ,其值与正则表达式 close 匹配

  • header Connection !~ close;

    header 包含 Connection,其值与正则表达式 close 不匹配

  • header Host;

    header 包含 Host

  • header ! X-Accel-Redirect;

    header 没有 X-Accel-Redirect

  • body ~ "Welcome to nginx!";

    正文内容匹配正则表达式 Welcome to nginx!

  • body !~ "Welcome to nginx!";

    正文内容不符合正则表达式 Welcome to nginx!

  • require $variable ...;

    所有指定的变量都不为空且不等于 "0"(1.15.9)。

如果指定了多个测试,则响应仅在匹配所有测试时才匹配。

仅检查响应体的前 256k 数据。

示例:

# status is 200, content type is "text/html",
# and body contains "Welcome to nginx!"
match welcome {
    status 200;
    header Content-Type = text/html;
    body ~ "Welcome to nginx!";
}
# status is not one of 301, 302, 303, or 307, and header does not have "Refresh:"
match not_redirect {
    status ! 301-303 307;
    header ! Refresh;
}
# status ok and not in maintenance mode
match server_ok {
    status 200-399;
    body !~ "maintenance mode";
}
# status is 200 or 204
map $upstream_status $good_status {
    200 1;
    204 1;
}

match server_ok {
    require $good_status;
}

原文档

可以配置健康检查以测试响应的状态码、某些头字段及其值的存在以及报文体。测试使用 指令单独配置,并在 指令的 match 参数中引用:

启用对周边 location 中引用的中的服务器定期健康检查。

定义连接到服务器以执行健康检查时使用的端口(1.9.7)。默认情况下,等于 端口。

http://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html
match
health_check
商业订阅
示例配置
指令
health_check
match
组
共享内存
组
server