对 QUIC 和 HTTP/3 的支持
从源码构建 配置 配置示例 故障排除 |
自 1.25.0 版本起,支持 QUIC 和 HTTP/3 协议。此外,自 1.25.0 版本起,QUIC 和 HTTP/3 支持在 Linux 二进制包中可用。
QUIC 和 HTTP/3 支持是实验性的,请谨慎使用。
从源码构建
使用 configure
命令配置构建。详细信息请参阅从源码构建 nginx。
配置 nginx 时,可以使用 --with-http_v3_module
配置参数启用 QUIC 和 HTTP/3。
建议使用提供 QUIC 支持的 SSL 库来构建 nginx,例如 BoringSSL、LibreSSL 或 QuicTLS。否则,将使用不支持 early data 的 OpenSSL 兼容层。
使用以下命令通过 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'; } } }
故障排除
有助于识别问题的提示
- 确保 nginx 使用正确的 SSL 库构建。
- 确保 nginx 在运行时使用正确的 SSL 库 (
nginx -V
命令会显示当前使用的库)。 - 确保客户端确实通过 QUIC 发送请求。建议先从简单的控制台客户端开始测试,例如 ngtcp2,以确保服务器配置正确,然后再使用可能对证书要求苛刻的真实浏览器进行测试。
- 使用调试支持构建 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"