对 QUIC 和 HTTP/3 的支持

从源码构建
配置
配置示例
故障排除

自 1.25.0 版本起,支持 QUICHTTP/3 协议。此外,自 1.25.0 版本起,QUIC 和 HTTP/3 支持在 Linux 二进制包中可用。

QUIC 和 HTTP/3 支持是实验性的,请谨慎使用。

从源码构建

使用 configure 命令配置构建。详细信息请参阅从源码构建 nginx

配置 nginx 时,可以使用 --with-http_v3_module 配置参数启用 QUIC 和 HTTP/3。

建议使用提供 QUIC 支持的 SSL 库来构建 nginx,例如 BoringSSLLibreSSLQuicTLS。否则,将使用不支持 early dataOpenSSL 兼容层。

使用以下命令通过 BoringSSL 配置 nginx

./configure
    --with-debug
    --with-http_v3_module
    --with-cc-opt="-I../boringssl/include"
    --with-ld-opt="-L../boringssl/build -lstdc++"

或者,可以使用 QuicTLS 配置 nginx

./configure
    --with-debug
    --with-http_v3_module
    --with-cc-opt="-I../quictls/build/include"
    --with-ld-opt="-L../quictls/build/lib"

或者,可以使用现代版本的 LibreSSL 配置 nginx

./configure
    --with-debug
    --with-http_v3_module
    --with-cc-opt="-I../libressl/build/include"
    --with-ld-opt="-L../libressl/build/lib"

配置完成后,使用 make 编译并安装 nginx。

配置

ngx_http_core_module 模块中的 listen 指令新增了一个参数 quic,该参数可在指定端口上启用基于 QUIC 的 HTTP/3。

quic 参数外,还可以指定 reuseport 参数,以便在多个 worker 进程下正常工作。

有关指令列表,请参阅 ngx_http_v3_module

启用地址验证

quic_retry on;

启用 0-RTT

ssl_early_data on;

启用 GSO (Generic Segmentation Offloading)

quic_gso on;

设置各种 token 的主机密钥

quic_host_key <filename>;

QUIC 需要 TLSv1.3 协议版本,该版本在 ssl_protocols 指令中默认启用。

默认情况下,GSO (Linux 特定的优化) 已禁用。如果相应的网络接口配置为支持 GSO,请启用它。

配置示例

http {
    log_format quic '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent" "$http3"';

    access_log logs/access.log quic;

    server {
        # for better compatibility it's recommended
        # to use the same port for quic and https
        listen 8443 quic reuseport;
        listen 8443 ssl;

        ssl_certificate     certs/example.com.crt;
        ssl_certificate_key certs/example.com.key;

        location / {
            # required for browsers to direct them to quic port
            add_header Alt-Svc 'h3=":8443"; ma=86400';
        }
    }
}

故障排除

有助于识别问题的提示