Curl Keep-Alive Example

"Keep-alive" names two unrelated mechanisms, and curl has switches for both. One is about reusing an HTTP connection for several requests; the other is about TCP probes on an idle socket.

Last updated . How this page is checked.

The command

curl -v -H "Connection: close" https://httpbin.org/get

What the flags do

FlagWhat it does
-H "Connection: close"Ask the server to close the connection after this response (HTTP/1.1).
-H "Connection: keep-alive"Redundant on HTTP/1.1, where persistence is already the default.
--no-keepaliveTurn off TCP keepalive probes. Does not affect HTTP connection reuse.
--keepalive-time NSeconds of idleness before the first TCP probe.

HTTP persistent connections

In HTTP/1.1 a connection stays open after a response unless one side says Connection: close. Curl reuses it, but only within a single invocation: give one command several URLs on the same host and the second request skips the TCP and TLS handshakes. Run curl twice and each process starts from nothing. -v shows it happening with a "Re-using existing connection" line.

# one process, two requests, one connection
curl -v -o /dev/null -o /dev/null \
  https://httpbin.org/get https://httpbin.org/headers 2>&1 | grep -i -E "re-using|connected to"

Forcing a close

Sending Connection: close tells the server you will not reuse the connection, and the server echoes the header and closes after responding. It is useful when testing how a load balancer distributes fresh connections, or when a broken server leaves a kept-alive connection hanging.

HTTP/2 and HTTP/3 have no such header. Connection-level headers are forbidden there and curl does not send them; the connection is managed by the protocol itself. Add --http1.1 if you need this behaviour against a server that would otherwise negotiate HTTP/2.

curl -sI --http1.1 -H "Connection: close" https://httpbin.org/get | grep -i "^connection"

TCP keepalive is something else

--no-keepalive, --keepalive-time and friends configure operating-system probes sent on an idle TCP socket so that firewalls and NAT devices do not silently drop it. They matter for long transfers with quiet periods, such as a slow download or a streaming response, and have nothing to do with whether the HTTP connection is reused.

curl --keepalive-time 30 -o big.iso https://example.com/big.iso

Common mistakes

  • A shell loop that runs curl repeatedly never reuses connections. Pass all the URLs to one command, or use --parallel, or a config file with -K.
  • The Keep-Alive: timeout=5 response header is advisory. Servers close idle connections whenever they like.
  • Connection: keep-alive does not make a server keep a connection open if it is configured to close it.

Related curl examples

More under Headers

References

These links leave api-tester.com, which is independent of the projects they point to.

FAQ

Does curl use keep-alive by default?

Yes. With HTTP/1.1 curl keeps the connection open and reuses it for further requests to the same host within the same command.

How do I send Connection: close with curl?

Add -H "Connection: close". On servers that negotiate HTTP/2, also add --http1.1, because HTTP/2 does not use the Connection header.

What does --no-keepalive do in curl?

It disables TCP keepalive probes on the socket. It does not disable HTTP persistent connections.