HTTP Health Checks
This article describes how to configure and use HTTP health checks in NGINX Plus and open source NGINX.
Overview
NGINX and NGINX Plus can continually test your upstream servers, avoid the servers that have failed, and gracefully add the recovered servers into the load‑balanced group.
Prerequisites
- For passive health checks, NGINX Open Source or NGINX Plus
- For active health checks and the live activity monitoring dashboard, NGINX Plus
- A load‑balanced group of HTTP upstream servers
Passive Health Checks
For passive health checks, NGINX and NGINX Plus monitor transactions as they happen, and try to resume failed connections. If the transaction still cannot be resumed, NGINX and NGINX Plus mark the server as unavailable and temporarily stop sending requests to it until it is marked active again.
The conditions under which an upstream server is marked unavailable are defined for each upstream server with parameters to the server directive in the upstream block:
fail_timeout– Sets the time during which a number of failed attempts must happen for the server to be marked unavailable, and also the time for which the server is marked unavailable (default is 10 seconds).max_fails– Sets the number of failed attempts that must occur during thefail_timeoutperiod for the server to be marked unavailable (default is 1 attempt).
In the following example, if NGINX fails to send a request to a server or does not receive a response from it 3 times in 30 seconds, it marks the server as unavailable for 30 seconds:
upstream backend {
server backend1.example.com;
server backend2.example.com max_fails=3 fail_timeout=30s;
}
Note that if there is only a single server in a group, the fail_timeout and max_fails parameters are ignored and the server is never marked unavailable.
Server Slow Start
A recently recovered server can be easily overwhelmed by connections, which may cause the server to be marked as unavailable again. Slow start allows an upstream server to gradually recover its weight from zero to its nominal value after it has been recovered or became available. This can be done with the slow_start parameter to the upstream server directive:
upstream backend {
server backend1.example.com slow_start=30s;
server backend2.example.com;
server 192.0.0.1 backup;
}
The time value sets the time for the server to recover its weight.
Note that if there is only a single server in a group, the slow_start parameter is ignored and the server is never marked unavailable.
Active Health Checks
NGINX Plus can periodically check the health of upstream servers by sending special health‑check requests to each server and verifying the correct response.
To enable active health checks:
In the
locationthat passes requests (proxy_pass) to an upstream group, include thehealth_checkdirective:server {
location / {
proxy_pass http://backend;
health_check;
}
}
This snippet defines a virtual server that passes all requests (location /) to the upstream group called backend. It also enables advanced health monitoring with default parameters: every five seconds NGINX sends a request for / to each server in the backend group. If any communication error or timeout occurs (or the server responds with a status code outside the range from 200 through 399) the health check fails. The server is marked as unhealthy, and NGINX Plus does not send client requests to it until it once again passes a health check.
Define a shared memory zone for the upstream server group with the
zonedirective:http {
upstream backend {
zone backend 64k;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
server backend4.example.com;
}
}The
zonedirective defines a memory zone that is shared among worker processes and is used to store the configuration of the upstream group. This enables the worker processes to use the same set of counters to keep track of responses from the servers in the group. Thezonedirective also makes the group dynamically configurable.The defaults for active health checks can be overridden with parameters to the
health_checkdirective:location / {
proxy_pass http://backend;
health_check interval=10 fails=3 passes=2;
}Here, the
intervalparameter increases the delay between health checks to 10 seconds (from the default 5 seconds). Thefailsrequires the server to fail three health checks to be marked as unhealthy (up from the default one). Finally, thepassesparameter means the server must pass two consecutive checks to be marked as healthy again (instead of the default one).
Specifying the Requested URI
Use the uri parameter to set the URI to request in a health check:
location / {
proxy_pass http://backend;
health_check uri=/some/path;
}
The specified URI is appended to the server domain name or IP address set for the server in the upstream block. For the first server in the sample backend group declared above, a health check requests the URI http://backend1.example.com/some/path.
Defining Custom Conditions
Finally, it is possible to set custom conditions that the response must satisfy for the server to pass the health check. The conditions are defined in a match block, which is referenced in the match parameter to the health_check directive.
http {
...
match server_ok {
status 200-399;
body !~ "maintenance mode";
}
server {
...
location / {
proxy_pass http://backend;
health_check match=server_ok;
}
}
}
Here the health check is passed if the status code of the response is in the range 200–399, and its body does not contain the string maintenance mode.
The match directive enables NGINX Plus to check the status code, header fields, and the body of a response. Using this directive it is possible to verify whether the status is in a specified range, whether a response includes a header, or whether the header or body matches a regular expression. The match directive can contain one status condition, one body condition, and multiple header conditions. A response must satisfy all conditions defined in match block for the server to pass the health check.
For example, the following match directive matches responses that have status code 200, the exact value text/html in the Content-Type header, and the text Welcome to nginx! in the body:
match welcome {
status 200;
header Content-Type = text/html;
body ~ "Welcome to nginx!";
}
The following example uses the exclamation point (!) to define characteristics the response must not have to pass the health check. In this case, the health check passes when the status code is something other than 301, 302, 303, or 307, and there is no Refresh header.
match not_redirect {
status ! 301-303 307;
header ! Refresh;
}
Health checks can also be enabled for non-HTTP protocols, such as FastCGI, memcached, SCGI, and uwsgi, and also for TCP and UDP.
HTTP Health Checks的更多相关文章
- Kong(V1.0.2) Health Checks and Circuit Breakers Reference
介绍 您可以让Kong代理的API使用ring-balancer,通过添加包含一个或多个目标实体的 upstream 实体进行配置,每个 target指向不同的IP地址(或主机名)和端口.ring-b ...
- TCP Health Checks
This chapter describes how to configure health checks for TCP. Introduction NGINX and NGINX Plus can ...
- UDP Health Checks
This chapter describes how to configure different types of health checks for UDP servers in a load-b ...
- Zookeeper Health Checks
Short Description: The article talks about the basic health checks to be performed when working on i ...
- Service Discovery And Health Checks In ASP.NET Core With Consul
在这篇文章中,我们将快速了解一下服务发现是什么,使用Consul在ASP.NET Core MVC框架中,并结合DnsClient.NET实现基于Dns的客户端服务发现 这篇文章的所有源代码都可以在G ...
- Using HAProxy as an API Gateway, Part 3 [Health Checks]
转自:https://www.haproxy.com/blog/using-haproxy-as-an-api-gateway-part-3-health-checks/ Achieving high ...
- 11g新特性:Health Monitor Checks
一.什么是Health Monitor ChecksHealth Monitor Checks能够发现文件损坏,物理.逻辑块损坏,undo.redo损坏,数据字典损坏等等.Health Monitor ...
- About Health Monitor Checks
About Health Monitor Checks Health Monitor checks (also known as checkers, health checks, or checks) ...
- oralce health monitor
1. Health Monitor简介 Health Monitor是11g里新增加的特性,用于数据库的各层和各个组建的诊断检查.例如可以检查:文件损坏.物理逻辑块损坏.redo和undo故障. ...
随机推荐
- 【转】探讨:ASP.NET技术的学习顺序问题
摘要:很多人对于ASP.NET的入门和学习顺序比较迷茫,今天让我们一起来跟随作者的思路学习探讨ASP.NET的学习顺序问题,希望有所帮助. 如果你已经有较多的面向对象开发经验,跳过以下这两步: 第一步 ...
- [转]Mysql FROM_UNIXTIME as UTC
本文转自:https://stackoverflow.com/questions/18276768/mysql-from-unixtime-as-utc You would be better off ...
- Spring Boot搭建Web项目常用功能
搭建WEB项目过程中,哪些点需要注意: 1.技术选型: 前端:freemarker.vue 后端:spring boot.spring mvc 2.如何包装返回统一结构结果数据? 首先要弄清楚为什么要 ...
- C#基础知识总结(五)
摘要 其他的数据类型:常量.枚举(enum).结构(struct).数组一.常量 语法:const 类型 变量名 = 变量值 常量称之为值不可变的变量! 在定义的地方赋值,其他的地方不能赋值. 常量变 ...
- npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\package.json'
在使用 npm 命令安装常用的 Node.js web框架模块 express时出现: 解决方法是 在命令行切换到安装nodejs文件下的nodejs\node_modules\npm 后执行npm ...
- 【Linux命令】ps命令
ps命令介绍 用于报告当前系统的进程状态.可以搭配kill指令随时中断.删除不必要的程序. ps命令是最基本同时也是非常强大的进程查看命令,使用该命令可以确定有哪些进程正在运行和运行的状态.进程是否结 ...
- Fundebug累计处理1000万条错误事件!
摘要上线半年时间,Fundebug累计处理1000万条错误事件.10000000啊! 自从去年双11[正式上线](https://blog.fundebug.com/2016/11/11/fundeb ...
- JavaScript解析机制与闭包原理实例详解
js代码解析机制: js代码解析之前会创建一个如下的词法环境对象(仓库):LexicalEnvironment{ } 在扫描js代码时会把: 1.用声明的方式创建的函数的名字; 2.用var定义的变量 ...
- JavaScript 中的相等操作符 ( 详解 [] == []、[] == ![]、{} == !{} )
ECMAScript 中的相等操作符由两个等于号 ( == ) 表示,如果两个操作数相等,则返回 true. 相等操作符会先转换操作数(通常称为强制转型),然后比较它们的相等性. 在转换不同的数据类型 ...
- js 做账单处理
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...