In a Node 24 http.Server, what do headersTimeout, requestTimeout, keepAliveTimeout and server.timeout each limit, and which of them stop slow clients?
answer
- four clocks, four phases
- headers, whole request, idle, inactivity
- 408 and a closed connection
- a 30-second checking interval
- server.timeout defaults to 0
basics
~20 sheadersTimeout caps the time to receive request headers (60 s), requestTimeout the whole request (300 s), keepAliveTimeout the idle gap after a response (5 s), and server.timeout socket inactivity (off). The first two stop slow clients with a 408.
solid answer
~40 sEach option guards a different phase of a connection. `server.headersTimeout` (default the lower of `requestTimeout` and 60000 ms) limits how long the parser waits for complete headers; `server.requestTimeout` (default 300000 ms since Node 18) limits receiving the entire request. When either expires Node answers `408` if no response has started, then closes the connection, which is the defence against clients that trickle headers or a body. `server.keepAliveTimeout` (5000 ms) closes a connection that sits idle after its last response. `server.timeout` is a plain socket-inactivity timer, `0` (off) since Node 13. Two details matter in production: the header and request clocks are checked every `connectionsCheckingInterval` (30000 ms), so they fire late by up to that much, and a `requestTimeout` shorter than a legitimate slow upload rejects it.
code
javascript · 13 linesimport { createServer } from 'node:http';
const server = createServer({
headersTimeout: 10_000, // 408 if headers take longer than 10 s
requestTimeout: 120_000, // 408 if the whole request takes longer
connectionsCheckingInterval: 2_000, // sweep for overdue requests every 2 s
keepAliveTimeout: 5_000, // the default, shown for clarity
}, (req, res) => {
res.end('ok');
});
console.log(server.timeout); // 0: no inactivity timeout
server.listen(3000);go deeper
Recall that Node has separate timeouts for headers, the whole request, idle keep-alive time and socket inactivity, and that a slow client gets a 408.
Give each default for Node 24 and the phase it covers, and explain why only the header and request deadlines stop a client that keeps sending a trickle of bytes.
Account for the connectionsCheckingInterval lag, size requestTimeout against real uploads, and know which settings only apply to new connections.
Decide which limits the Node layer owns and which a proxy owns, and make that split explicit so no one disables a deadline assuming the other layer has it.
## Four clocks for four phases A connection to a Node HTTP server passes through phases: the client sends headers, then a body, the handler responds, and then the connection either closes or waits idle for the next request. `http.Server` has a separate timeout for each, settable as `http.createServer` options or as properties on the server. | Setting | What it limits | Node 24 default | When it fires | |---|---|---|---| | `headersTimeout` | time to receive the complete request headers | the lower of `requestTimeout` and `60000` ms | `408`, request listener not called, connection closed | | `requestTimeout` | time to receive the **entire** request, body included | `300000` ms (5 minutes) | `408` if no response has started, connection closed | | `keepAliveTimeout` | idle time after the last response finished | `5000` ms | idle socket destroyed | | `timeout` (`server.setTimeout`) | inactivity on the socket at any phase | `0`, disabled | `'timeout'` event, or the socket is destroyed if nobody listens | ## The two that stop slow clients A **slow-client attack** opens many connections and feeds each one bytes just fast enough never to look idle: headers one byte at a time, or a body at a crawl. Every such connection holds a socket and parser state. An inactivity timer does not catch it, because data keeps arriving; what catches it is a deadline on the whole phase. - `headersTimeout` is that deadline for headers. A client that has not finished its headers within 60 seconds gets `408 Request Timeout` and the connection is closed, and the request never reaches your handler. - `requestTimeout` is the deadline for the whole request. Your handler may already be running when it fires, because Node calls the handler as soon as the headers are complete, but if the body is still arriving after 5 minutes Node writes a `408` (when no response has been started) and closes the connection. The http docs say both must stay non-zero to protect against denial of service when the server is deployed without a reverse proxy in front. The constructor enforces their order too: passing a `headersTimeout` larger than a non-zero `requestTimeout` to `createServer` throws `ERR_OUT_OF_RANGE`. Two neighbouring limits work alongside them. Request headers larger than `maxHeaderSize` (16 KiB by default, also set with `--max-http-header-size`) are rejected with `431`, and other malformed requests get a `400` through the `'clientError'` default. ## How the deadlines are enforced Node does not arm a timer per request for these two. The server keeps a list of connections and, every `connectionsCheckingInterval` (30000 ms by default), sweeps it for requests whose headers or body are overdue. In practice: 1. A deadline fires late by up to one interval, so a 5-second `headersTimeout` with the default interval can let a stalled client live for 35 seconds. 2. Lowering the interval makes enforcement tighter at the cost of a more frequent sweep. 3. Both deadlines are about receiving the request; a handler that is slow to respond after the request has fully arrived is not cut by them. ## The two that manage idle connections - `keepAliveTimeout` applies only after a response has been fully written. It decides how long a kept-alive socket may wait for the next request before Node destroys it. Since v24.6.0 the actual socket timeout is `keepAliveTimeout` plus `keepAliveTimeoutBuffer` (1000 ms by default), while the `Keep-Alive: timeout=5` header still advertises the base value. - `server.timeout` is the general inactivity timer. It was 120 seconds before Node 13 and is now `0`. Turning it on also cuts a long-poll or a slow streaming response that goes quiet, so it is not a slow-client defence. Changing `keepAliveTimeout` or `timeout` on a running server affects only new connections, so set them in `createServer` options or before `listen()`. ## Choosing values - Keep `headersTimeout` short; honest clients send headers in milliseconds. - Size `requestTimeout` to the slowest legitimate upload, or move large uploads to a route that streams them elsewhere. - Set `keepAliveTimeout` against whatever reuses your connections, a load balancer in particular. - Leave `server.timeout` at `0` unless every route has a known activity pattern.
- An upload route on a Node 24 server starts failing with 408 for users on slow connections sending large files. Why, and what are the options?`requestTimeout` covers receiving the entire request, body included, and defaults to 300000 ms, so a body that takes longer than 5 minutes is cut with a 408. Options: raise `requestTimeout` for that server, run uploads on a separate server instance with a longer limit, or have clients upload in parts. Setting it to 0 removes the protection and should only be done behind a proxy that enforces its own limit.
- Why can a Node server with headersTimeout set to 5000 still hold a stalled connection for more than 30 seconds?The header and request deadlines are not per-request timers; the server checks its connection list every `connectionsCheckingInterval`, 30000 ms by default. A stalled request is only noticed on the next sweep after its deadline passes, so enforcement lags by up to one interval. Lowering `connectionsCheckingInterval` tightens it.
- Thinks server.timeout still defaults to 120 seconds
- Expects requestTimeout to limit how long the handler runs
- Believes keepAliveTimeout stops a client trickling headers
- Sets headersTimeout above requestTimeout and expects it to work
- Assumes each deadline fires at the exact millisecond
- Disables requestTimeout with no proxy in front
Assumes Node 24: requestTimeout defaults to 300000 ms since v18.0.0 (no limit before), headersTimeout to the lower of requestTimeout and 60000 since v18.14.0 and v19.4.0, and keepAliveTimeoutBuffer exists since v24.6.0.