diff --git a/README.md b/README.md index ebda11c..a110ce1 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,15 @@ check_http_expect_alive These status codes indicate the upstream server's http response is ok, the backend is alive. +check_http_expect_body +------------------------ +* **syntax**: `check_http_body "text match"` +* **default**: `none` +* **context**: `upstream` + +If you set the check type is http, then check the body of +the returned page and search for this string. + check_keepalive_requests ------------------------ * **syntax**: `check_keepalive_requests num` diff --git a/ngx_http_upstream_check_module.c b/ngx_http_upstream_check_module.c index e83b327..be94bc5 100644 --- a/ngx_http_upstream_check_module.c +++ b/ngx_http_upstream_check_module.c @@ -237,6 +237,8 @@ struct ngx_http_upstream_check_srv_conf_s { ngx_uint_t status_alive; } code; + ngx_str_t expect_body; + ngx_array_t *fastcgi_params; ngx_uint_t default_down; @@ -458,6 +460,8 @@ static char *ngx_http_upstream_check_http_send(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_http_upstream_check_http_expect_alive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static char *ngx_http_upstream_check_http_expect_body(ngx_conf_t *cf, + ngx_command_t *cmd, void *conf); static char *ngx_http_upstream_check_fastcgi_params(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); @@ -552,6 +556,15 @@ static ngx_command_t ngx_http_upstream_check_commands[] = { NULL }, + { + ngx_string("check_http_expect_body"), + NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1, + ngx_http_upstream_check_http_expect_body, + 0, + 0, + NULL + }, + { ngx_string("check_fastcgi_param"), NGX_HTTP_UPS_CONF|NGX_CONF_TAKE2, @@ -1746,11 +1759,13 @@ static ngx_int_t ngx_http_upstream_check_http_parse(ngx_http_upstream_check_peer_t *peer) { ngx_int_t rc; ngx_uint_t code, code_n; + ngx_str_t expect_body; ngx_http_upstream_check_ctx_t *ctx; ngx_http_upstream_check_srv_conf_t *ucscf; ucscf = peer->conf; ctx = peer->check_data; + expect_body = ucscf->expect_body; if ((ctx->recv.last - ctx->recv.pos) > 0) { @@ -1789,7 +1804,11 @@ ngx_http_upstream_check_http_parse(ngx_http_upstream_check_peer_t *peer) { "http_parse: code_n: %ui, conf: %ui", code_n, ucscf->code.status_alive); - if (code_n & ucscf->code.status_alive) { + if (!(code_n & ucscf->code.status_alive)) { + return NGX_ERROR; + } + + if ((expect_body.len == 0) || (ngx_strstrn(ctx->recv.pos, expect_body.data, expect_body.len) != NULL)) { return NGX_OK; } else { return NGX_ERROR; @@ -3686,6 +3705,21 @@ ngx_http_upstream_check_http_send(ngx_conf_t *cf, ngx_command_t *cmd, return NGX_CONF_OK; } +static char * +ngx_http_upstream_check_http_expect_body(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf) { + ngx_str_t *value; + ngx_http_upstream_check_srv_conf_t *ucscf; + + value = cf->args->elts; + + ucscf = ngx_http_conf_get_module_srv_conf(cf, + ngx_http_upstream_check_module); + + ucscf->expect_body = value[1]; + + return NGX_CONF_OK; +} static char * ngx_http_upstream_check_fastcgi_params(ngx_conf_t *cf, ngx_command_t *cmd,