非常教程

Nginx参考手册

ngx_stream_upstream_hc_module

ngx_stream_upstream_hc_module

  • 示例配置
  • 指令
  • health_check
  • health_check_timeout
  • match

所述ngx_stream_upstream_hc_module模块(1.9.0)允许使服务器定期健康检查在一组。服务器组必须驻留在共享内存中。

如果健康检查失败,服务器将被视为不健康。如果为同一组服务器定义了多个运行状况检查,则任何检查的单个故障都会导致相应的服务器被认为不健康。客户端连接不会以“检查”状态传递给不健康的服务器和服务器。

此模块可作为我们商业订阅的一部分。

示例配置

upstream tcp {
    zone upstream_tcp 64k;

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

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

server {
    listen     12346;
    proxy_pass tcp;
    health_check;
}

使用此配置,nginx将tcp每5秒检查一次建立到组中每个服务器的TCP连接的能力。当无法建立与服务器的连接时,运行状况检查将失败,并且服务器将被视为不健康。

可以为UDP协议配置健康检查:

upstream dns_upstream {

    zone   dns_zone 64k;

    server dns1.example.com:53;
    server dns2.example.com:53;
    server dns3.example.com:53;
}

server {
    listen       53 udp;
    proxy_pass   dns_upstream;
    health_check udp;
}

在这种情况下,ICMP“ Destination Unreachable”消息的缺失预计会回复发送的字符串“ nginx health check”。

运行状况检查也可以配置为测试从服务器获取的数据。测试使用match指令单独配置,并在matchhealth_check指令的参数中引用。

指令

句法:

health_check参数;

默认:

语境:

服务器

启用组中服务器的定期运行状况检查。

支持以下可选参数:

interval= time设置两次连续运行状况检查之间的间隔,默认情况下为5秒。jitter= time设置每个健康检查将随机延迟的时间,默认情况下不会有延迟。fails= number设置特定服务器的连续失败运行状况检查次数,在此之后,该服务器将被视为不健康,默认情况下为1. passes= number设置特定服务器连续通过的运行状况检查的次数,在此之后服务器将被视为健康,默认情况下,1. mandatory设置服务器的初始“检查”状态,直到第一次健康检查完成(1.11.7)。客户端连接不会以“检查”状态传递给服务器。如果未指定参数,则服务器最初将被视为健康。match=name指定match配置成功连接应通过以便运行状况检查通过的测试的块。默认情况下,对于TCP,只检查与服务器建立TCP连接的能力。对于UDP,ICMP“ Destination Unreachable”消息的缺失预计会回复发送的字符串“ nginx health check”。

在版本1.11.7之前,默认情况下,UDP运行状况检查需要一个包含send和expect参数的匹配块。

port= number定义连接到服务器以执行健康检查时使用的端口(1.9.7)。默认情况下,等于服务器端口。udp指定UDP协议应该用于运行状况检查而不是默认TCP协议(1.9.13)。

句法:

health_check_timeout超时;

默认:

health_check_timeout 5s;

语境:

流,服务器

覆盖健康检查的proxy_timeout值。

句法:

匹配名称{...}

默认:

语境:

定义用于验证服务器对运行状况检查的响应的指定测试集。

可以配置以下参数:

sendstring; 发送一个string到服务器; expectstring| ~regex; 一个文字字符串(1.9.12)或从服务器获得的数据应匹配的正则表达式。正则表达式使用前面的“ ~*”修饰符(用于不区分大小写的匹配)或“ ~”修饰符(用于区分大小写的匹配)指定。

两者sendexpect参数都可以包含十六进制文字,前缀“ \x”后跟两个十六进制数字,例如“ \x80”(1.9.12)。

健康检查通过如果:

  • TCP连接成功建立;
  • stringsend参数,如果指定,被送往;
  • expect如果指定,从服务器获得的数据与参数中的字符串或正则表达式匹配;
  • 所用时间不超过health_check_timeout指令中指定的值。

例:

upstream backend {
    zone     upstream_backend 10m;
    server   127.0.0.1:12345;
}

match http {
    send     "GET / HTTP/1.0\r\nHost: localhost\r\n\r\n";
    expect ~ "200 OK";
}

server {
    listen       12346;
    proxy_pass   backend;
    health_check match=http;
}

只检查从服务器获取的第一个proxy_buffer_size字节数据。

ngx_stream_upstream_hc_module
Nginx

Nginx是一款轻量级的 Web 服务器/反向代理服务器及电子邮件代理服务器,可在 BSD-like 协议下发行。其特点是占有内存少,并发能力强。

主页 https://nginx.org/
源码 http://hg.nginx.org/nginx
发布版本 1.13.6

Nginx目录

1.指南 | Guides
2.核心 | Core
3.ngx_google_perftools_module
4.ngx_http_access_module
5.ngx_http_addition_module
6.ngx_http_api_module
7.ngx_http_auth_basic_module
8.ngx_http_auth_jwt_module
9.ngx_http_auth_request_module
10.ngx_http_autoindex_module
11.ngx_http_browser_module
12.ngx_http_charset_module
13.ngx_http_core_module
14.ngx_http_dav_module
15.ngx_http_empty_gif_module
16.ngx_http_f4f_module
17.ngx_http_fastcgi_module
18.ngx_http_flv_module
19.ngx_http_geoip_module
20.ngx_http_geo_module
21.ngx_http_gunzip_module
22.ngx_http_gzip_module
23.ngx_http_gzip_static_module
24.ngx_http_headers_module
25.ngx_http_hls_module
26.ngx_http_image_filter_module
27.ngx_http_index_module
28.ngx_http_js_module
29.ngx_http_keyval_module
30.ngx_http_limit_conn_module
31.ngx_http_limit_req_module
32.ngx_http_log_module
33.ngx_http_map_module
34.ngx_http_memcached_module
35.ngx_http_mirror_module
36.ngx_http_mp4_module
37.ngx_http_perl_module
38.ngx_http_proxy_module
39.ngx_http_random_index_module
40.ngx_http_realip_module
41.ngx_http_referer_module
42.ngx_http_rewrite_module
43.ngx_http_scgi_module
44.ngx_http_secure_link_module
45.ngx_http_session_log_module
46.ngx_http_slice_module
47.ngx_http_spdy_module
48.ngx_http_split_clients_module
49.ngx_http_ssi_module
50.ngx_http_ssl_module
51.ngx_http_status_module
52.ngx_http_stub_status_module
53.ngx_http_sub_module
54.ngx_http_upstream_conf_module
55.ngx_http_upstream_hc_module
56.ngx_http_upstream_module
57.ngx_http_userid_module
58.ngx_http_uwsgi_module
59.ngx_http_v2_module
60.ngx_http_xslt_module
61.ngx_mail_auth_http_module
62.ngx_mail_core_module
63.ngx_mail_imap_module
64.ngx_mail_pop3_module
65.ngx_mail_proxy_module
66.ngx_mail_smtp_module
67.ngx_mail_ssl_module
68.ngx_stream_access_module
69.ngx_stream_core_module
70.ngx_stream_geoip_module
71.ngx_stream_geo_module
72.ngx_stream_js_module
73.ngx_stream_limit_conn_module
74.ngx_stream_log_module
75.ngx_stream_map_module
76.ngx_stream_proxy_module
77.ngx_stream_realip_module
78.ngx_stream_return_module
79.ngx_stream_split_clients_module
80.ngx_stream_ssl_module
81.ngx_stream_ssl_preread_module
82.ngx_stream_upstream_hc_module
83.ngx_stream_upstream_module