Alokai
Features

Keep-Alive Timeout

How long the middleware, Nuxt and Next.js servers keep idle keep-alive connections open, and the KEEP_ALIVE_TIMEOUT variable that controls it.

A proxy or load balancer in front of the application reuses connections to it. If Node closes an idle connection first and the proxy sends a request on it anyway, the proxy fails the request - typically with HTTP 503. Node closes idle connections after 5 seconds by default, which is shorter than the idle timeout of most proxies.

The middleware, Nuxt and Next.js servers keep idle keep-alive connections open for 65 seconds and set headersTimeout 5 seconds above that. Node only uses headersTimeout to bound how long it waits for the headers of an incoming request, but keeping it above the keep-alive timeout is a long-standing convention for servers behind a proxy. The application timeout must always be longer than the idle timeout of the proxy in front of it.

Prerequisites Requires the release that introduced this feature of @alokai/connect (middleware), @vue-storefront/nuxt, @vue-storefront/next and @alokai/cli (Next.js).

Configuration

All three servers read the same environment variable, in milliseconds:

VariableDefaultDescription
KEEP_ALIVE_TIMEOUT65000How long an idle keep-alive connection stays open.

headersTimeout is always KEEP_ALIVE_TIMEOUT + 5000. Invalid values (not a positive integer) fall back to the default.

When you host the application yourself, keep KEEP_ALIVE_TIMEOUT above the idle timeout of every proxy that pools connections to it.

How it is wired

  • Middleware: createServer from @alokai/connect sets both timeouts on the server it returns.
  • Nuxt: @vue-storefront/nuxt registers a Nitro server plugin that sets both timeouts when the server starts listening. It is skipped in development.
  • Next.js: the image built by alokai deploy sets KEEP_ALIVE_TIMEOUT=65000, which the standalone server.js applies to keepAliveTimeout. The graceful-shutdown preload from @vue-storefront/next sets headersTimeout. If you build your own image, set the variable and keep the --require ./graceful-shutdown.cjs flag in the entrypoint.

On this page