HTTP 保活和 TCP 保活

发布于 2025-01-07 10:21:50 字数 73 浏览 1 评论 0原文

HTTP Keep Alive 是如何实现的?它内部是否使用 TCP Keep Alive?如果不是,服务器如何检测客户端是死是活?

How is HTTP Keep Alive implemented? Does it internally use TCP Keep Alive? If not, how does the server detect if the client is dead or alive?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拍不死你 2025-01-14 10:21:50

我知道这是一个老问题,但仍然:

  • HTTP Keep-Alive 是一项允许 HTTP 客户端(通常是浏览器)和服务器(Web 服务器)通过同一 TCP 连接发送多个请求/响应对的功能。这会减少第二个、第三个……HTTP 请求的延迟,减少网络流量等。

  • TCP keepalive 是一个完全不同的野兽。它通过发送小数据包来保持 TCP 连接打开。此外,当数据包发送时,这充当检查的作用,因此一旦连接断开,发送者就会收到通知(请注意,否则情况并非如此 - 直到我们尝试通过 TCP 连接进行通信,我们不知道它是否正常).

回答有关 HTTP Keep-Alive 的问题:

How is HTTP Keep Alive implemented? 

简单地说,HTTP 服务器不会在每次响应后关闭 TCP 连接,而是等待一段时间(如果其他 HTTP 请求也通过它)。经过一段时间后,它还是会关闭它。

Does it internally use TCP Keep Alive? 

不,至少我认为这没有任何意义。

If not, how does the server detect if the client is dead or alive?

它不——它不需要。如果客户端发送请求,它将得到响应。如果客户端没有通过 TCP 连接发送任何内容(可能是因为连接已死),则超时将关闭连接;客户端当然会注意到这一点,并在需要时通过另一个 TCP 连接发送请求。

I know this is an old question, but still:

  • HTTP Keep-Alive is a feature that allows HTTP client (usually browser) and server (webserver) to send multiple request/response pairs over the same TCP connection. This decreases latency for 2nd, 3rd,... HTTP request, decreases network traffic and similar.

  • TCP keepalive is a totally different beast. It keeps TCP connection opened by sending small packets. Additionally, when the packet is sent this serves as a check so the sender is notified as soon as connection drops (note that this is NOT the case otherwise - until we try to communicate through TCP connection we have no idea if it is ok or not).

To answer your questions about HTTP Keep-Alive:

How is HTTP Keep Alive implemented? 

To put it simply, the HTTP server doesn't close the TCP connection after each response but waits some time if some other HTTP request will come over it too. After some timeout it closes it anyway.

Does it internally use TCP Keep Alive? 

No, at least I see no point in it.

If not, how does the server detect if the client is dead or alive?

It doesn't - it doesn't need to. If a client sends a request, it will get the response. If the client doesn't send anything over TCP connection (maybe because the connection is dead) then a timeout will close the connection; client will of course notice this and will send request through another TCP connection if needed.

恏ㄋ傷疤忘ㄋ疼 2025-01-14 10:21:50

HTTP Keep-Alive 是 HTTP 协议的一项功能。实现保持活动功能的 Web 服务器必须定期检查连接/套接字(对于传入的 HTTP 请求)自发送最后一个 HTTP 响应(如果存在相应的 HTTP 请求)以来的时间跨度。如果在配置的保持活动时间(秒)内未收到 HTTP 请求,Web 服务器将关闭连接。 Web 服务器完成“关闭”后,将无法再进行 HTTP 请求。另一方面,TCP Keep-Alive是由TCP层的OS管理的。 HTTP Keep-Alive 和 TCP Keep-Alive 是完全不相关的东西。

HTTP Keep-Alive is a feature of HTTP protocol. The web-server, implementing Keep-Alive Feature, has to check the connection/socket periodically (for incoming HTTP request) for the time span since it sent the last HTTP response (in case there was corresponding HTTP Request). If no HTTP request is received by the time of the configured keep-alive time (seconds) the web server closes the connection. No further HTTP request will be possible after the 'close' done by Web Server. On the other hand, TCP Keep-Alive is managed by OS in the TCP layer. HTTP Keep-Alive and TCP Keep-Alive is totally unrelated things.

锦上情书 2025-01-14 10:21:50

HTTP keep-alive,又名 HTTP 持久连接,是一种允许单个 TCP 连接对多个 HTTP 请求/响应保持打开状态的指令。

默认情况下,HTTP 连接在每次请求后关闭。当有人访问您的网站时,他们的浏览器需要创建新的连接来请求构成网页的每个文件(例如图像、Javascript 和 CSS 样式表),这一过程可能会导致页面加载时间较长。

输入图片此处的描述

启用 keep-alive 标头允许您通过单个连接提供所有网页资源。 Keep-alive 还可以减少服务器上的 CPU 和内存使用量。

来源:https://www.imperva.com/learn/performance/http -保持活动/

HTTP keep-alive, a.k.a., HTTP persistent connection, is an instruction that allows a single TCP connection to remain open for multiple HTTP requests/responses.

By default, HTTP connections close after each request. When someone visits your site, their browser needs to create new connections to request each of the files that make up your web pages (e.g. images, Javascript, and CSS stylesheets), a process that can lead to high page load times.

enter image description here

Enabling the keep-alive header allows you to serve all web page resources over a single connection. Keep-alive also reduces both CPU and memory usage on your server.

Source: https://www.imperva.com/learn/performance/http-keep-alive/

埋情葬爱 2025-01-14 10:21:50

http keep-alive 只是让 tcp 存活的时间更长,以便传输多个 http 请求。 keep-alive 超时后,tcp 连接将被关闭。
tcp keep-alive只是一种保持tcp连接的机制,或者检查tcp连接是否关闭

http keep-alive is just making tcp living longer in order to transfer multi http request.After keep-alive timeout, the tcp connection will be closed.
tcp keep-alive is just a mechanism keeping the tcp connection,or check the tcp connection is not closed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文