# HTTP/1.1 协议详解

> 逐字段拆解 HTTP/1.1——报文语法、起始行、方法、状态码与每一个头字段的精确语义。

- 作者：David（道雾轩）
- 最后更新：2026-08-03
- 原文：https://daiw.org/manual/http11
- 转载与引用：请注明出处并附原文链接（https://daiw.org/about/copyright）

本专栏逐字段解析 HTTP/1.1：**报文怎么切分、每个字段的 ABNF 语法是什么、取值范围有哪些、收到非法值该怎么办**。不讲发展史，不讲「为什么要有网络」，只讲协议本身。

## 规范基准

以 **RFC 9110（HTTP 语义）** 与 **RFC 9112（HTTP/1.1 报文语法）** 为准，二者于 2022 年 6 月取代 RFC 7230–7235（更早的 RFC 2616 已废弃）。相关：RFC 9111（缓存）、RFC 6265（Cookie）、RFC 7617（Basic 认证）。

字段语义归 RFC 9110（跨版本通用），**报文成帧与连接管理归 RFC 9112（HTTP/1.1 专属）**——这是理解「哪些东西 HTTP/2 还有、哪些没了」的分界线。

## 记号约定

全书用 **ABNF**（RFC 5234）描述语法，并复用 RFC 9110 的核心规则：

```text
OWS  = *( SP / HTAB )          ; optional whitespace
RWS  = 1*( SP / HTAB )         ; required whitespace
BWS  = OWS                     ; "bad" whitespace，语法允许但语义上应为空
tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
        "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
token = 1*tchar
quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
```

`CRLF` 一律指 `\r\n`（0x0D 0x0A）。示例报文中的行尾均为 CRLF，正文里不再重复标注。

## 阅读顺序

**第 1 部分 · 报文与成帧**——[报文结构](https://daiw.org/manual/http11/message-format)、[请求行](https://daiw.org/manual/http11/request-line)、[状态行](https://daiw.org/manual/http11/status-line)、[字段行语法](https://daiw.org/manual/http11/field-syntax)、[消息体长度](https://daiw.org/manual/http11/message-body-length)、[连接管理](https://daiw.org/manual/http11/connection-management)。HTTP/1.1 的骨架，先读。

**第 2 部分 · 方法与状态码**——[方法](https://daiw.org/manual/http11/methods)的安全性/幂等性/可缓存性，以及 [1xx/2xx](https://daiw.org/manual/http11/status-codes)、[3xx](https://daiw.org/manual/http11/status-3xx)、[4xx](https://daiw.org/manual/http11/status-4xx)、[5xx](https://daiw.org/manual/http11/status-5xx) 每个码的精确触发条件。

**第 3 部分 · 字段详解**——[表示字段](https://daiw.org/manual/http11/representation-fields)、[内容协商](https://daiw.org/manual/http11/content-negotiation)、[日期](https://daiw.org/manual/http11/http-date)、[条件请求](https://daiw.org/manual/http11/conditional-requests)、[范围请求](https://daiw.org/manual/http11/range-requests)、[缓存](https://daiw.org/manual/http11/caching)、[认证](https://daiw.org/manual/http11/authentication)、[Cookie](https://daiw.org/manual/http11/cookies)、[上下文字段](https://daiw.org/manual/http11/context-fields)、[代理字段](https://daiw.org/manual/http11/proxy-fields)，末尾附 [字段速查表](https://daiw.org/manual/http11/field-index)。

急用查字段直接跳 [字段速查表](https://daiw.org/manual/http11/field-index)；系统读从 [报文结构](https://daiw.org/manual/http11/message-format) 开始。

## 本专栏篇目

- [HTTP/1.1 协议详解](https://daiw.org/manual/http11.md): 逐字段拆解 HTTP/1.1——报文语法、起始行、方法、状态码与每一个头字段的精确语义。

### 第 1 部分 · 报文与成帧

- [报文结构](https://daiw.org/manual/http11/message-format.md): start-line、字段行、空行、消息体——HTTP/1.1 报文的四段式语法与解析规则。
- [请求行](https://daiw.org/manual/http11/request-line.md): method、request-target 的四种形式、HTTP-version——请求起始行逐字段解析。
- [状态行](https://daiw.org/manual/http11/status-line.md): HTTP-version、status-code 三位数字、reason-phrase——响应起始行逐字段解析。
- [字段行语法](https://daiw.org/manual/http11/field-syntax.md): field-name、field-value、OWS 处理、重复字段合并与 list-based 字段的通用规则。
- [消息体长度](https://daiw.org/manual/http11/message-body-length.md): Transfer-Encoding、Content-Length 与连接关闭——七条成帧规则的判定优先级。
- [连接管理](https://daiw.org/manual/http11/connection-management.md): Connection、Keep-Alive、Upgrade、Expect——持久连接与逐跳字段的处理规则。

### 第 2 部分 · 方法与状态码

- [方法](https://daiw.org/manual/http11/methods.md): 安全性、幂等性、可缓存性——九个方法的精确语义与请求体约束。
- [状态码：1xx 与 2xx](https://daiw.org/manual/http11/status-codes.md): 状态码注册规则、可缓存码集合，以及信息性与成功类每个码的精确触发条件。
- [状态码：3xx 重定向](https://daiw.org/manual/http11/status-3xx.md): 301/302/303/307/308 的方法改写差异、Location 解析规则与 300/304 的特殊语义。
- [状态码：4xx 客户端错误](https://daiw.org/manual/http11/status-4xx.md): 400–451 每个码的精确触发条件，以及易混对（401/403、400/422、409/412）的判定标准。
- [状态码：5xx 服务端错误](https://daiw.org/manual/http11/status-5xx.md): 500–511 的触发条件，以及 502/503/504 在网关场景下的判定边界。

### 第 3 部分 · 字段详解

- [表示字段](https://daiw.org/manual/http11/representation-fields.md): Content-Type、Content-Encoding、Content-Language、Content-Location——描述消息体的四个字段。
- [内容协商](https://daiw.org/manual/http11/content-negotiation.md): Accept 系列字段、qvalue 语法、媒体范围优先级规则与 Vary 的缓存含义。
- [日期与时间](https://daiw.org/manual/http11/http-date.md): IMF-fixdate 与两种废弃格式、Date 字段的生成规则与时钟偏差处理。
- [条件请求](https://daiw.org/manual/http11/conditional-requests.md): ETag 强弱比较、Last-Modified 验证器与五个 If-* 字段的求值优先级。
- [范围请求](https://daiw.org/manual/http11/range-requests.md): Range 的三种范围形式、Content-Range 语法、206 与 multipart/byteranges 的构造。
- [缓存](https://daiw.org/manual/http11/caching.md): Cache-Control 请求与响应指令全集、Expires/Age 与新鲜度计算公式。
- [认证](https://daiw.org/manual/http11/authentication.md): challenge/credentials 语法、realm、token68、Basic 与 Bearer 方案及代理认证字段。
- [Cookie](https://daiw.org/manual/http11/cookies.md): Set-Cookie 的七个属性、Cookie 请求语法、SameSite 与 __Host- 前缀规则。
- [上下文与控制字段](https://daiw.org/manual/http11/context-fields.md): User-Agent、Server、Referer、Retry-After、Allow、Link 等字段的语法与约束。
- [代理与转发字段](https://daiw.org/manual/http11/proxy-fields.md): Via、Forwarded、X-Forwarded-*、Max-Forwards 与逐跳字段的剥离规则。
- [字段速查表](https://daiw.org/manual/http11/field-index.md): 全部字段按 A–Z 索引——方向、语法要点、是否逐跳，链到详解。
