Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท HTTP, HTTPS, net, TLS, dgram and dns

Node.js HTTP server timeouts

Understand headersTimeout, requestTimeout, keepAliveTimeout and server.timeout on a Node.js http.Server: defaults, what each protects against, and how to tune them.

An http.Server has several separate timers. Each protects against a different kind of slow or idle client, and each is easy to confuse with the others. A slow-loris attacker, for example, opens many connections and sends a few bytes at a time; a well-behaved browser leaves a keep-alive connection idle between page loads. The timers tell the server when to give up in each case.

All of them are properties on the server object (or options to http.createServer()), measured in milliseconds, and 0 disables the check where noted. They apply to https.Server too.

Property Default What it limits
server.headersTimeout 60000 Time to receive the complete request headers.
server.requestTimeout 300000 Time to receive the entire request (headers and body).
server.keepAliveTimeout 5000 How long an idle keep-alive socket waits for the next request.
server.timeout 0 Socket inactivity before the 'timeout' event (0 means never).
server.connectionsCheckingInterval 30000 How often the server scans for expired headers/request timers.
NODE
const server = http.createServer({ headersTimeout: 10_000, requestTimeout: 30_000 }, handler);
server.keepAliveTimeout = 65_000;

Try it

Node.js Defaults and the 408 response

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js keepAliveTimeout and server.timeout

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

How each timeout works

headersTimeout starts when the first byte of a request arrives. If the blank line that ends the headers does not arrive in time, the server answers 408 Request Timeout and closes the socket. This is the main defense against a client that dribbles headers. The value must be lower than or equal to requestTimeout unless requestTimeout is 0.

requestTimeout covers the whole request, including a slow upload. It protects handlers that wait for a body. The default of five minutes can be too short for big uploads over slow links; raise it for upload routes, or set it to 0 and enforce deadlines in your own code.

keepAliveTimeout governs idle time between requests on one connection. After a response, the server waits this long for another request and then closes the socket. It also sends the value in the Keep-Alive: timeout=N response header (in whole seconds). If a load balancer sits in front of Node, set keepAliveTimeout higher than the balancer's idle timeout, otherwise the balancer may reuse a connection that Node has just closed and the user sees an occasional 502.

server.timeout is plain socket inactivity. When it is non-zero and a socket sees no traffic for that long, the server emits 'timeout'. If there is no listener, the socket is destroyed; if you add one with server.setTimeout(ms, cb) or server.on('timeout', ...), you decide what happens, and you must close the socket yourself.

connectionsCheckingInterval controls the sweep that enforces headersTimeout and requestTimeout. Because the check runs only at this interval, the effective deadline can be later than the configured value by up to one interval.

Usage notes

  • Set them on the server before or right after listen(). Changing them later affects new requests.
  • Response time is not limited by any of these. A handler that never answers keeps the connection open until the client gives up. Add your own deadline (for example with AbortSignal.timeout()), or use res.setTimeout() per response.
  • Long polling and server-sent events need requestTimeout and server.timeout to leave the stream alone; both only look at incoming data and idle sockets, so test with your proxy as well.
  • Behind a reverse proxy, the proxy usually enforces its own header and body timeouts; keep Node's slightly looser so the proxy reports the problem first.
  • server.close() does not cut idle keep-alive sockets immediately; use server.closeIdleConnections() or closeAllConnections() for a fast shutdown.
  • maxRequestsPerSocket (default 0, unlimited) is a related limit: after that many requests the server sends Connection: close.