支持 QUIC 和 HTTP/3

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

从 1.25.0 开始提供对 QUICHTTP/3 协议的支持。 此外,从 1.25.0 开始,QUIC 和 HTTP/3 支持在 Linux 二进制包

QUIC 和 HTTP/3 支持是实验性的,需要注意的是 emptor。

从源构建

构建使用configure命令。 有关详细信息,请参阅从源构建 nginx

配置 nginx 时,可以使用--with-http_v3_moduleconfiguration 参数。

建议使用提供 QUIC 支持的 SSL 库来构建 nginx,例如 BoringSSL、LibreSSLQuicTLS。 否则,将使用不支持早期数据的 OpenSSL 兼容层。

使用以下命令使用 BoringSSL 配置 nginx:

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

或者,nginx 可以使用 QuicTLS 进行配置:

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

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

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

配置后, nginx 是使用make.

配置

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

quic参数 还可以指定 reuseport 参数以使其与多个 worker 一起正常工作。

有关指令的列表,请参阅 ngx_http_v3_module

启用地址验证:

quic_retry on;

启用 0-RTT:

ssl_early_data on;

启用 GSO(通用分段卸载):

quic_gso on;

各种令牌设置 host key:

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';
        }
    }
}

故障 排除

可能有助于识别问题的提示:

  • 确保 nginx 是使用适当的 SSL 库构建的。
  • 确保 nginx 在运行时使用正确的 SSL 库 (nginx -V显示当前使用的内容)。
  • 确保客户端确实通过 QUIC 发送请求。 建议从简单的控制台客户端(如 ngtcp2)开始,以确保服务器配置正确后再尝试 对于可能对证书相当挑剔的真实浏览器。
  • 使用 debug support 构建 nginx 并检查调试日志。 它应包含有关连接及其失败原因的所有详细信息。 所有相关消息都包含”quic“ 前缀 并且很容易被过滤掉。
  • 为了进行更深入的调查,可以启用其他调试 使用以下宏:NGX_QUIC_DEBUG_PACKETS,NGX_QUIC_DEBUG_FRAMES,NGX_QUIC_DEBUG_ALLOC,NGX_QUIC_DEBUG_CRYPTO.

    ./configure
        --with-http_v3_module
        --with-debug
        --with-cc-opt="-DNGX_QUIC_DEBUG_PACKETS -DNGX_QUIC_DEBUG_CRYPTO"