<!-- review: finished -->

<a id="http-v2"></a>

# HTTP/2

提供对 [HTTP/2](https://datatracker.ietf.org/doc/html/rfc9113) 的支持。

当 [从源代码构建](https://cn.angie.software//angie/docs/installation/sourcebuild.md#sourcebuild) 时,
默认不构建此模块;
应使用
`‑‑with‑http_v2_module`
[构建选项](https://cn.angie.software//angie/docs/installation/sourcebuild.md#configure) 启用它。

在来自 [我们仓库](https://cn.angie.software//angie/docs/installation/index.md#install-packages) 的软件包和镜像中,
该模块已包含在构建中。

<a id="configuration-example-49"></a>

## 配置示例

```nginx
server {
    listen 443 ssl;

    http2 on;

    ssl_certificate server.crt;
    ssl_certificate_key server.key;
}
```

#### NOTE
请注意,通过 TLS 接受 HTTP/2 连接需要"应用层协议协商"(ALPN)TLS 扩展支持,该功能从 [OpenSSL](http://www.openssl.org/) 1.0.2 版本开始提供。

如果 [ssl_prefer_server_ciphers](https://cn.angie.software//angie/docs/configuration/modules/http/http_ssl.md#ssl-prefer-server-ciphers) 指令设置为值"on",则应配置 [密码套件](https://cn.angie.software//angie/docs/configuration/modules/http/http_ssl.md#ssl-ciphers) 以符合 [RFC 9113 附录 A](https://datatracker.ietf.org/doc/html/rfc9113#appendix-A) 黑名单,并且客户端需要支持。

<a id="directives-52"></a>

## 指令

<a id="index-0"></a>

<a id="http2"></a>

### http2

| [语法](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `http2` `on` | `off`;   |
|--------------------------------------------------------------------------------------|-------------------------|
| 默认值                                                                                  | `http2 off;`            |
| [上下文](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server            |

启用 [HTTP/2](https://datatracker.ietf.org/doc/html/rfc9113) 协议。

<a id="index-1"></a>

<a id="http2-body-preread-size"></a>

### http2_body_preread_size

| [语法](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `http2_body_preread_size` size;   |
|--------------------------------------------------------------------------------------|-----------------------------------|
| 默认值                                                                                  | —                                 |
| [上下文](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server                      |

设置每个请求的缓冲区大小,在开始处理请求之前,请求体可以保存在该缓冲区中。

<a id="index-2"></a>

<a id="http2-chunk-size"></a>

### http2_chunk_size

| [语法](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `http2_chunk_size` size;   |
|--------------------------------------------------------------------------------------|----------------------------|
| 默认值                                                                                  | `http2_chunk_size 8k;`     |
| [上下文](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location     |

设置响应体被切分成的块的最大大小。值过低会导致更高的开销。值过高会由于 [队头阻塞](http://en.wikipedia.org/wiki/Head-of-line_blocking) 而影响优先级处理。

<a id="index-3"></a>

<a id="http2-max-concurrent-pushes"></a>

### http2_max_concurrent_pushes

#### Deprecated
自 1.2.0 版本弃用.

| [语法](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `http2_max_concurrent_pushes` number;   |
|--------------------------------------------------------------------------------------|-----------------------------------------|
| 默认值                                                                                  | `http2_max_concurrent_pushes 10;`       |
| [上下文](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server                            |

限制连接中并发 [推送](#http2-push) 请求的最大数量。

<a id="index-4"></a>

<a id="http2-max-concurrent-streams"></a>

### http2_max_concurrent_streams

| [语法](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `http2_max_concurrent_streams` number;   |
|--------------------------------------------------------------------------------------|------------------------------------------|
| 默认值                                                                                  | `http2_max_concurrent_streams 128;`      |
| [上下文](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server                             |

设置连接中并发 HTTP/2 流的最大数量。

<a id="index-5"></a>

<a id="http2-push"></a>

### http2_push

#### Deprecated
自 1.2.0 版本弃用.

| [语法](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `http2_push` uri | `off`;   |
|--------------------------------------------------------------------------------------|-----------------------------|
| 默认值                                                                                  | `http2_push off;`           |
| [上下文](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location      |

抢先发送([推送](https://datatracker.ietf.org/doc/html/rfc9113#name-server-push))对指定 uri 的请求,连同对原始请求的响应一起发送。只会处理带有绝对路径的相对 URI,例如:

```nginx
http2_push /static/css/main.css;
```

`uri` 值可以包含变量。

可以在同一配置级别指定多个 http2_push 指令。`off` 参数取消从上一级配置继承的 http2_push 指令的效果。

<a id="index-6"></a>

<a id="http2-push-preload"></a>

### http2_push_preload

#### Deprecated
自 1.2.0 版本弃用.

| [语法](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `http2_push_preload` `on` | `off`;   |
|--------------------------------------------------------------------------------------|--------------------------------------|
| 默认值                                                                                  | `http2_push_preload off;`            |
| [上下文](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location               |

启用将"Link"响应头字段中指定的 [预加载链接](https://www.w3.org/TR/preload/#server-push-http-2) 自动转换为 [推送](https://datatracker.ietf.org/doc/html/rfc9113#name-server-push) 请求。

<a id="index-7"></a>

<a id="http2-recv-buffer-size"></a>

### http2_recv_buffer_size

| [语法](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)   | `http2_recv_buffer_size` size;   |
|--------------------------------------------------------------------------------------|----------------------------------|
| 默认值                                                                                  | `http2_recv_buffer_size 256k;`   |
| [上下文](https://cn.angie.software//angie/docs/configuration/configfile.md#configfile)  | http                             |

设置每个 [工作进程](https://cn.angie.software//angie/docs/configuration/modules/core.md#worker-processes) 的输入缓冲区大小。

<a id="built-in-variables-17"></a>

## 内置变量

http_v2 模块支持以下内置变量:

<a id="v-http2"></a>

### `$http2`

协商的协议标识符:

| `h2`   | 表示基于 TLS 的 HTTP/2   |
|--------|---------------------|
| `h2c`  | 表示基于明文 TCP 的 HTTP/2 |
| `""`   | 否则为空字符串             |
