Skip to content

Adding check_http_expect_body #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
36 changes: 35 additions & 1 deletion ngx_http_upstream_check_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down