Is it possible to configure Nginx to send TCP keepalive?

On TCP connections held by NGinx, can it be configured to send TCP Keepalive

(Not HTTP KeepAlive!)


Solution 1:

You have 2 options:

  • use stock so_keepalive nginx option
  • use nginx-tcp-keepalive module directives

listen ... so_keepalive=on

listen directive with so_keepalive option is server level. See http://nginx.org/en/docs/http/ngx_http_core_module.html#listen for full documentation. This option is available since 2011-12-12 in nginx 1.1.11 and later.

tcp_keepalive=on

The nginx-tcp-keepalive module of Nicolas Viennot provides family of tcp_keep* directives that are location level. This module should work properly on modern Linux and BSD systems. Mac OS X support is limited to tcp_keepalive only. The module was originally releases approximately in the moment when nginx gained the so_keepalive option. I hadn't tried if the module is still compatible with newer version of nginx.

Solution 2:

Made it: https://github.com/nviennot/nginx-tcp-keepalive

Solution 3:

At the moment, only mail proxy module supports "so_keepalive" directive.

Solution 4:

There is as LD_PRELOAD module that turns keepalives on, even if the app doesn't have an option to do so. (I don't know if it works with nginx or not.)