Keep-Alive 是一个通用消息头,允许消息发送者暗示连接的状态,还可以用来设置超时时长和最大请求数。

需要将 The Connection 首部的值设置为  "keep-alive" 这个首部才有意义。同时需要注意的是,在HTTP/2 协议中, ConnectionKeep-Alive  是被忽略的;在其中采用其他机制来进行连接管理。

Header type General header
Forbidden header name no

语法

Keep-Alive: parameters

指令

parameters
一系列用逗号隔开的参数,每一个参数由一个标识符和一个值构成,并使用等号 ('=') 隔开。下述标识符是可用的:
  • timeout:指定了一个空闲连接需要保持打开状态的最小时长(以秒为单位)。需要注意的是,如果没有在传输层设置 keep-alive TCP message 的话,大于 TCP 层面的超时设置会被忽略。
  • max:在连接关闭之前,在此连接可以发送的请求的最大值。在非管道连接中,除了 0 以外,这个值是被忽略的,因为需要在紧跟着的响应中发送新一次的请求。HTTP 管道连接则可以用它来限制管道的使用。

示例

含有 Keep-Alive 首部的响应示例:

HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Date: Thu, 11 Aug 2016 15:23:13 GMT
Keep-Alive: timeout=5, max=1000
Last-Modified: Mon, 25 Jul 2016 04:32:39 GMT
Server: Apache

(body)

规范

Specification Title
HyperText Transport Protocol Keep-Alive Header The Keep-Alive Header (Experimental specification)
RFC 7230, appendix A.1.2: Keep-Alive Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing
RFC 2068, section 19.7.1.1: The Keep-Alive Header Hypertext Transfer Protocol -- HTTP/1.1

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic support
ExperimentalDeprecatedNon-standard
Chrome Full support YesEdge Full support YesFirefox Full support YesIE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.

相关内容

文档标签和贡献者

标签: 
此页面的贡献者: zhangchen, WayneCui
最后编辑者: zhangchen,