[PATCH] Send Connection: close for draining.

Pablo Fischer pablo at pablo.com.mx
Tue Mar 20 19:25:35 UTC 2018


Howdy,

First off sorry if the C code is a bit ugly, been a while since I did
some C with nginx.

So I've some very long running keep-alive connections that although
keepalive_timeout helps it is just "not enough" or "fast enough" when
I need it to stop draining connections. I looked at the headers-more
plugin and then noted that nginx was still adding the Connection
header via ngx_http_header_filter.

So I came up with a patch, feel free to use it.

The patch does two things:

- Adds a new variable ($disable_keep_alive_now).
- Disables if we (nginx) are told to disable keep-alive (aka start
sending Connection: close).

The flow is a little bit like this:

1. You set $disable_keep_alive_now in your nginx location or wherever you want.
2. If the value is "yes" (set $disable_keep_alive_now "yes") then
nginx will start sending the Connection: close header rather than the
Connection: keep-alive.

For example:

location / {
  if (!-f "healthcheck/path") {
    set $disable_keep_alive_now "yes";
  }
}

That way if you take your host out of service via checking a specific
file then you just set the header and nginx will start sending close
and thus connections will start draining faster.

I opted for a variable rather than a setting so that I still let nginx
manage the keepalive timeouts but at the same time having a "lever"
that I can turn on dynamically for changing the Connection header.

When I googled for it I noted that there were a few people asking how
to do something similar so that's why I opted in publishing the patch.

Ah, the url is:
https://github.com/pfischermx/nginx-keepalive-disable-patch

Thanks
-- 
Pablo


More information about the nginx mailing list