Skip to content

Connection

Description

The Connection response header controls whether the network connection stays open after the current transaction finishes. It's used to signal the desired connection behavior to the client, primarily determining if the connection should be kept alive for subsequent requests or closed after the current request-response cycle.

In HTTP/1.1, connections are persistent by default (kept alive after the response), while in HTTP/1.0, connections are non-persistent by default (closed after the response). The Connection header allows servers to override these defaults and explicitly specify the connection behavior.

This header is particularly important for performance optimization, as keeping connections open can reduce the overhead of establishing new TCP connections for each request, leading to faster page loads and reduced server load.

Syntax

The syntax of the Connection header follows this structure:

Connection: <token>[, <token>]*
  • <token>: A connection token, most commonly keep-alive or close.
  • Multiple tokens can be specified, separated by commas.

Common Tokens

  • keep-alive: Indicates that the connection should remain open for subsequent requests.
  • close: Indicates that the connection should be closed after the current request-response cycle.
  • Other tokens may be used to specify hop-by-hop headers that should not be forwarded by proxies.

Example Syntax

Connection: keep-alive

This example tells the client to maintain the connection for future requests.

Connection: close

This example tells the client to close the connection after the current response.

Examples

Keep-Alive Example

A response indicating that the connection should remain open:

HTTP/1.1 200 OK
Date: Mon, 02 Jun 2025 15:00:00 GMT
Content-Type: text/html
Content-Length: 5678
Connection: keep-alive
Keep-Alive: timeout=5, max=1000

<!DOCTYPE html>
<html>
<head><title>Example Page</title></head>
<body>Page content...</body>
</html>

In this example, the server indicates that the connection should be kept alive, with additional parameters specifying a timeout of 5 seconds and a maximum of 1000 requests before closing.

Close Connection Example

A response indicating that the connection should be closed:

HTTP/1.1 200 OK
Date: Mon, 02 Jun 2025 15:10:30 GMT
Content-Type: application/json
Content-Length: 42
Connection: close

{"status": "success", "message": "Goodbye"}

This response tells the client that the server will close the connection after this response is sent.

HTTP/1.0 Connection

A response to an HTTP/1.0 request that explicitly keeps the connection alive:

HTTP/1.0 200 OK
Date: Mon, 02 Jun 2025 15:20:45 GMT
Content-Type: text/plain
Content-Length: 12
Connection: keep-alive

Hello World!

Since HTTP/1.0 defaults to closing connections, this header explicitly overrides the default behavior.

Summary

The Connection response header is a fundamental component of HTTP connection management, allowing servers to control whether connections remain open for subsequent requests or are closed after the current transaction. By using this header effectively, servers can optimize network performance, reduce latency, and manage server resources more efficiently. In modern web applications, persistent connections are generally preferred for performance reasons, but there are scenarios where closing connections is appropriate, such as when a server is under heavy load or when a specific transaction represents the end of a session.