nginx学习(八)——nginx的配置系统4之healthcheck module

ngx_http_healthcheck_module

参考自:nginx负载集群解决方案

1.upstream模块

upstream provider {
        server 192.168.237.186:10086;
        healthcheck_enabled;
        healthcheck_delay 10000;
        healthcheck_timeout 1000;
        healthcheck_failcount 2;
        healthcheck_send 'GET /test HTTP/1.0' 'Host: xxcupid1.qunar.com' 'Connection: close';
    }
healthcheck_enabled ##启用此模块
healthcheck_delay ##对同一台后端服务器两次检测之间的时间间隔,单位毫秒,默认为1000;
healthcheck_timeout ##进行一次健康检测的超时时间,单位为毫秒,默认值2000;
healthcheck_failcount ##对一台后端服务器检测成功或失败多少次之后方才确定其为成功或失败,并实现启用或禁用此服务器;
healthcheck_send ##为了检测后端服务器的健康状态所发送的检测请求;如:healthcheck_send "GET /health HTTP/1.0" 'Host: coolinuz.9966.org';
healthcheck_expected ##期望从后端服务器收到的响应内容;如果未设置,则表示从后端服务器收到200状态码即为正确;
healthcheck_buffer ##健康状态检查所使用的buffer空间大小;
 
healthcheck_status
通过类似stub_status的方式输出检测信息,使用方法如下:
location /stat {
  healthcheck_status;


如果healthcheck_send配置的地址无法访问,nginx的error日志中会打印如下内容:

2016/09/12 15:48:06 [info] 5831#0: check protocol qunar_http error with peer: 192.168.237.186:10086 

如果能正常访问的话

2016/09/12 15:51:02 [info] 6974#0: enable check peer: 192.168.237.186:10086

nginx关闭启动日志:

2016/09/12 15:50:39 [error] 5831#0: ngx_exiting...
2016/09/12 15:50:39 [error] 5831#0: Pending timer: 1616 handler addr: (4bc6d7)
2016/09/12 15:50:41 [notice] 5831#0: clear all the events on 5831 
2016/09/12 15:50:41 [error] 5831#0: ngx_exiting...
2016/09/12 15:50:41 [error] 5831#0: Pending timer: -1 handler addr: (0)
2016/09/12 15:50:41 [notice] 5831#0: exiting
2016/09/12 15:50:41 [notice] 5831#0: exit
2016/09/12 15:50:41 [notice] 18102#0: signal 17 (SIGCHLD) received
2016/09/12 15:50:41 [notice] 18102#0: worker process 5831 exited with code 0
2016/09/12 15:50:41 [notice] 18102#0: signal 29 (SIGIO) received


2.server location模块

location /name {
            #proxy_pass http://provider;
            proxy_pass http://192.168.237.186:10086/;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-Scheme    $scheme;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header
Allows redefining or appending fields to the request header passed to the proxied server. The value can contain text, variables, and their combinations. These directives are inherited from the previous level if and only if there are no proxy_set_header directives defined on the current level. By default, only two fields are redefined:
允许重定义或者向请求头(request header)增加域内容。该值可以包含文本,变量或者他们两者的组合。这些指会可以继承自上一个层级,当且仅当当前层级上没有定义proxy_set_header指令。默认情况下,只有两个域被定义了:
proxy_set_header Host       $proxy_host;
proxy_set_header Connection close;
If caching is enabled, the header fields “If-Modified-Since”, “If-Unmodified-Since”, “If-None-Match”, “If-Match”, “Range”, and “If-Range” from the original request are not passed to the proxied server.
An unchanged “Host” request header field can be passed like this:
proxy_set_header Host       $http_host;
However, if this field is not present in a client request header then nothing will be passed. In such a case it is better to use the $host variable - its value equals the server name in the “Host” request header field or the primary server name if this field is not present:
如果http_host这个域在request_header中没有提供的话,那么请求将不会被分发。在这种情况下,最好使用$host变量,这个值会取服务器请求头的host字段;如果这个域没有被提供的话,会取主服务器的名字。
proxy_set_header Host       $host;
In addition, the server name can be passed together with the port of the proxied server:
proxy_set_header Host       $host:$proxy_port;
If the value of a header field is an empty string then this field will not be passed to a proxied server:
proxy_set_header Accept-Encoding "";

3.http request header

参见 http request header
nginx学习(八)——nginx的配置系统4之healthcheck module_第1张图片
nginx学习(八)——nginx的配置系统4之healthcheck module_第2张图片



你可能感兴趣的:(nginx)