Nginx怎么启用HTTP/3
发布日期:
作者: 西木
评论数:暂无评论
什么是 HTTP/3 与 QUIC
HTTP/3 是基于 UDP 的下一代 HTTP 协议,底层使用 QUIC 传输层协议,具备 连接建立更快、减少队头阻塞、对丢包更鲁棒、支持多路复用和零/一 RTT 启动 等优点。Nginx 从1.25版本开始支持 HTTP/3 协议。
1. 确认 Nginx 已支持 HTTP/3
Debian 13通过Debian apt仓库安装的 Nginx 已经包含 HTTP/3 模块。
执行以下命令:
sudo nginx -V
输出以下内容:
nginx version: nginx/1.26.3
built with OpenSSL 3.5.0 8 Apr 2025 (running with OpenSSL 3.5.1 1 Jul 2025)
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -Werror=implicit-function-declaration -ffile-prefix-map=/build/reproducible-path/nginx-1.26.3=. -fstack-protector-strong -fstack-clash-protection -Wformat -Werror=format-security -fcf-protection -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=stderr --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_v3_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_secure_link_module --with-http_sub_module --with-mail_ssl_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-stream_realip_module --with-http_geoip_module=dynamic --with-http_image_filter_module=dynamic --with-http_perl_module=dynamic --with-http_xslt_module=dynamic --with-mail=dynamic --with-stream=dynamic --with-stream_geoip_module=dynamic
可以看到包含:
--with-http_v3_module
表示支持 HTTP/3。
2. 确保使用 QUIC 所需的 TLS 版本
HTTP/3 基于 QUIC,只支持 TLS 1.3,因此:
- 必须启用 TLS 1.3;
- 必须使用 HTTPS (443)。
Nginx 配置中应包含:
ssl_protocols TLSv1.3;
3. 配置 Nginx 以启用 HTTP/3
在你的 HTTPS 站点配置(例如 /etc/nginx/sites-available/default
)中添加:
server {
listen 443 ssl http2; # 继续保留 HTTP/2
listen 443 quic reuseport; # 新增 HTTP/3 (QUIC)
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# 启用 QUIC 相关标头
add_header Alt-Svc 'h3=":443"'; # 通知浏览器支持 HTTP/3
# 其他 server 配置...
}
4. 打开防火墙 UDP 443
HTTP/3 使用 UDP 443,如果服务器有防火墙,例如ufw,需要允许443端口:
sudo ufw allow 443/udp
5. 检查 Nginx 配置并重启
sudo nginx -t
sudo systemctl reload nginx
6. 测试 HTTP/3 是否生效
使用 curl
测试,例如本站:
curl -I --http3 https://www.ximu.work
如果输出头部正常返回,如下内容,返回 HTTP/3 200 说明已启用 HTTP/3:
curl -I --http3 https://www.ximu.work
HTTP/3 200
server: nginx
date: Fri, 05 Sep 2025 09:34:22 GMT
content-type: text/html; charset=UTF-8
link: <https://www.ximu.work/wp-json/>; rel="https://api.w.org/"
alt-svc: h3=":443"
strict-transport-security: max-age=31536000; includeSubDomains; preload
也可以用浏览器访问,然后在 开发者工具 → 网络 (Network) 中查看是否使用 HTTP/3。