55

When the client uses the Connection: close header in the request message, this means that it wants the server to close the connection after sending the response message.

I thought that this header is only used in the request messages, but I have noticed that it is also used in the response messages. For example:

enter image description here

What does this header means when used in the response message?

I think that it means that the server will close the connection after sending the response the message (even if the client has used the Connection: keep-alive header in its request message). Am I correct?

user365656
  • 553
  • 1
  • 4
  • 4

1 Answers1

49

Yes, this is correct. The server can just say "I don't support your keep-alive request and will just close the connection when I am finished".

From RFC 2616, Section 14.10:

HTTP/1.1 defines the "close" connection option for the sender to
signal that the connection will be closed after completion of the
response. For example,

   Connection: close

in either the request or the response header fields indicates that the connection SHOULD NOT be considered `persistent' (section 8.1)
after the current request/response is complete.

HTTP/1.1 applications that do not support persistent connections MUST include the "close" connection option in every message.

Sven
  • 98,649
  • 14
  • 180
  • 226
  • 4
    That's weird IMO, why to say that the connection will be closed, if server can simply close it? Shouldn't client then just see it that the connection was closed by the server. Any idea what was the point in this header? I don't see any value here. – Pavel P Nov 24 '18 at 05:43
  • 3
    @Pavel The web application includes the "Connection: close" http header in the response in cases where it want's to inform the front-end load balancer to close the keep alive connection incase the load balancer is using one. – Basil A Mar 12 '19 at 13:59
  • 1
    @BasilA so that load balancer closes connection to the server or to the client? IMO load balancer could see that the connection was closed by the server and would know that without reading any headers. Also reverse is true: server can close connection without the `Connection: close` header – Pavel P Mar 13 '19 at 23:05
  • 2
    @Pavel The loadbalancer usually receives multiple connections from multiple clients and utilizes the same connection with the backend by using multiplexing through a keep-alive connection. When some backends might not want to be part of this multiplexing, the backend http response should include a "Connection: close" to inform the load balancer to close it's connection and stop multiplexing. This is described in detail in AWS Load Balancing Guide (Scroll down to 'HTTP Connections') https://docs.aws.amazon.com/elasticloadbalancing/latest/userguide/how-elastic-load-balancing-works.html – Basil A Mar 14 '19 at 08:20
  • @BasilA But wouldn't that then mean that the upstream server only signals that it wants the connection closed (that the browser should not reuyse it), because if it would actively close it, then it would be closing the multiplexing-connection between the load balancer and itself, which is not desired? I'm asking because I was dealing with this https://stackoverflow.com/questions/57812501/python-twisted-is-it-possible-to-reload-certificates-on-the-fly and noticed that the browser will close the connection when the header is set, and simply keep reusing the connection if it is not set. – Daniel F Sep 05 '19 at 22:03
  • 5
    @BasilA Looks like you're right. Interesting. _The Connection general-header field allows the sender to specify options that are desired for that particular connection and MUST NOT be communicated by proxies over further connections._ – Daniel F Sep 05 '19 at 22:21
  • I have a question that if the client send a request with connection is close, does it mean the server will close the tcp connection rather than the client does? – JaskeyLam Jan 28 '21 at 12:52
  • In other words the connection will behave like HTTP 1.0 in that only one request is handled per connection? – davolfman Jul 21 '21 at 23:57