aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/http/headers/keep-alive/index.html
blob: afd7bc4111ca6ffa5187e9799dd590a0bc70b1f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---
title: Keep-Alive
slug: Web/HTTP/Headers/Keep-Alive
tags:
  - HTTP
  - HTTP Headers
translation_of: Web/HTTP/Headers/Keep-Alive
---
<div>{{HTTPSidebar}}</div>

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

<div class="note">
<p>需要将 The {{HTTPHeader("Connection")}} 首部的值设置为  "keep-alive" 这个首部才有意义。同时需要注意的是,在HTTP/2 协议中, {{HTTPHeader("Connection")}}{{HTTPHeader("Keep-Alive")}}  是被忽略的;在其中采用其他机制来进行连接管理。</p>
</div>

<table class="properties">
 <tbody>
  <tr>
   <th scope="row">Header type</th>
   <td>{{Glossary("General header")}}</td>
  </tr>
  <tr>
   <th scope="row">{{Glossary("Forbidden header name")}}</th>
   <td>no</td>
  </tr>
 </tbody>
</table>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">Keep-Alive: <em>parameters</em></pre>

<h2 id="指令">指令</h2>

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

<h2 id="示例">示例</h2>

<p><code><font face="Open Sans, arial, sans-serif">含有 </font>Keep-Alive</code> 首部的响应示例:</p>

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

(body)</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Title</th>
  </tr>
  <tr>
   <td><a href="https://tools.ietf.org/id/draft-thomson-hybi-http-timeout-01.html#rfc.section.2">HyperText Transport Protocol Keep-Alive Header</a></td>
   <td>The Keep-Alive Header (Experimental specification)</td>
  </tr>
  <tr>
   <td><a href="https://tools.ietf.org/html/rfc7230#appendix-A.1.2">RFC 7230, appendix A.1.2: Keep-Alive</a></td>
   <td>Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing</td>
  </tr>
  <tr>
   <td>{{RFC("2068", "The Keep-Alive Header", "19.7.1.1")}}</td>
   <td>Hypertext Transfer Protocol -- HTTP/1.1</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>

<p>{{Compat("http.headers.Keep-Alive")}}</p>

<h2 id="相关内容">相关内容</h2>

<ul>
 <li>{{HTTPHeader("Connection")}}</li>
 <li><a href="/en-US/docs/Web/HTTP/Connection_management_in_HTTP_1.x">Connection management in HTTP/1.x</a></li>
</ul>