Connection Header

The Connection HTTP header controls whether the current network connection remains open after the transaction.

The persistent network connection allows the client and server to send/receive multiple HTTP requests/responses without opening a new connection for every single request/response pair.

Persistent connections can improve overall system performance by eliminating the need to send additional TCP/IP packets for establishing and closing the network connection before/after each request.

The connection is persistent by default for HTTP/1.1 clients. For HTTP/1.0 connections, you need to explicitly indicate that you want a persistent connection by adding the "Connection: keep-alive" header.

Connection Examples


Keep-Alive Connection Example Live Request
GET / HTTP/1.1
Connection: keep-alive
Keep-Alive: timeout=5, max=100
Host: google.com

The "Connection keep-alive" header means the client wants to keep it open. The Keep-Alive header: timeout = 5, max = 100 means that the client specifies a timeout to keep the connection open for 5 seconds and a maximum of 100 requests.

Close Connection Example Live Request
GET / HTTP/1.1
Connection: close
Host: google.com

A client can inform the server that it doesn't need a persistent connection by sending the "Connection: close" header, thereby freeing up server resources. For example, if the client does not plan to send more than one request to this server.

If the server does not support persistent connections or cannot provide a persistent connection at this time, it can indicate this by sending the "Connection: close" header in the response.

Server Response with Connection: close
HTTP/1.1 200 OK
Connection: close
...