解释http keep-alive机制 [英] Explain http keep-alive mechanism

查看:43
本文介绍了解释http keep-alive机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Keep-alives 被添加到 HTTP 中以基本上减少显着的为每个快速创建和关闭套接字连接的开销新请求.以下是它在 HTTP 中的工作原理的总结1.0 和 1.1:

Keep-alives were added to HTTP to basically reduce the significant overhead of rapidly creating and closing socket connections for each new request. The following is a summary of how it works within HTTP 1.0 and 1.1:

HTTP 1.0 HTTP 1.0 规范并没有真正深入研究如何Keep-Alive 应该可以工作.基本上,支持 Keep-Alive 的浏览器向请求附加了一个额外的标头,[为清晰起见进行了编辑],解释如下:

HTTP 1.0 The HTTP 1.0 specification does not really delve into how Keep-Alive should work. Basically, browsers that support Keep-Alive appended an additional header to the request as [edited for clarity] explained below:

当服务器处理请求并生成一个响应,它还向响应添加一个标头:

When the server processes the request and generates a response, it also adds a header to the response:

连接:Keep-Alive

完成后,套接字连接没有像以前那样关闭,但在发送响应后保持打开状态.什么时候客户端发送另一个请求,它重用相同的连接.这连接将继续被重用,直到客户端或服务器决定对话结束,其中一个断开连接.

When this is done, the socket connection is not closed as before, but kept open after sending the response. When the client sends another request, it reuses the same connection. The connection will continue to be reused until either the client or the server decides that the conversation is over, and one of them drops the connection.

以上解释来自这里.但我不明白一件事

The above explanation comes from here. But I don't understand one thing

这样做后,socket连接并没有像以前那样关闭,而是发送响应后保持打开状态.

When this is done, the socket connection is not closed as before, but kept open after sending the response.

据我所知,我们只是发送 tcp 数据包来发出请求和响应,这个 socket connection 如何帮助以及它是如何工作的?我们仍然需要发送数据包,但它如何以某种方式建立持久连接?感觉好不真实.

As I understand we just send tcp packets to make requests and responses, how this socket connection helps and how does it work? We still have to send packets, but how can it somehow establish the persistent connection? It seems so unreal.

推荐答案

建立新的 TCP 连接(DNS 查找、TCP 握手、SSL/TLS 握手等)存在开销.如果没有保持连接,每个 HTTP 请求都必须建立一个新的 TCP 连接,然后在发送/接收响应后关闭连接.保持活动允许将现有 TCP 连接重新用于多个请求/响应,从而避免所有这些开销.这就是使连接持久"的原因.

There is overhead in establishing a new TCP connection (DNS lookups, TCP handshake, SSL/TLS handshake, etc). Without a keep-alive, every HTTP request has to establish a new TCP connection, and then close the connection once the response has been sent/received. A keep-alive allows an existing TCP connection to be re-used for multiple requests/responses, thus avoiding all of that overhead. That is what makes the connection "persistent".

在 HTTP 0.9 和 1.0 中,默认情况下,服务器在向客户端发送响应后关闭其 TCP 连接的末端.客户端必须在收到响应后关闭其 TCP 连接端.在 HTTP 1.0(但不是 0.9)中,客户端可以通过在请求中包含 Connection: keep-alive 标头来明确要求服务器不要关闭其连接端.如果服务器同意,它会在响应中包含一个 Connection: keep-alive 标头,并且不会关闭其连接端.然后客户端可以重新使用相同的 TCP 连接来发送它的下一个请求.

In HTTP 0.9 and 1.0, by default the server closes its end of a TCP connection after sending a response to a client. The client must close its end of the TCP connection after receiving the response. In HTTP 1.0 (but not in 0.9), a client can explicitly ask the server not to close its end of the connection by including a Connection: keep-alive header in the request. If the server agrees, it includes a Connection: keep-alive header in the response, and does not close its end of the connection. The client may then re-use the same TCP connection to send its next request.

在 HTTP 1.1 中,keep-alive 是默认行为,除非客户端通过在其请求中包含 Connection: close 标头明确要求服务器关闭连接,或者服务器决定在其响应中包含一个 Connection: close 标头.

In HTTP 1.1, keep-alive is the default behavior, unless the client explicitly asks the server to close the connection by including a Connection: close header in its request, or the server decides to includes a Connection: close header in its response.

这篇关于解释http keep-alive机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆