必须在编译时显式添加 --with-http_v2_module 和 --with-http_ssl_module,并确保 Nginx ≥ 1.9.5、OpenSSL ≥ 1.0.2e(推荐 1.1.1+);安装后用 nginx -V | grep http_v2_module 验证模块已启用。

要在 Nginx 安装阶段就确保支持 HTTP/2,关键不是“安装时勾选”,而是编译时显式启用必要模块,并链接满足版本要求的 OpenSSL。Nginx 默认不包含 http_v2_module,必须手动加入编译参数。
确认基础依赖版本达标
HTTP/2 支持需要两个硬性前提:
- Nginx ≥ 1.9.5(推荐 1.20+ 或最新稳定版)
- OpenSSL ≥ 1.0.2e(强烈建议 1.1.1 或 3.0+,以支持 TLSv1.3 和更优性能)
执行以下命令验证当前环境:
nginx -V 2>&1 | grep -o with-http_v2_module —— 若无输出,说明未启用该模块openssl version —— 检查是否 ≥ 1.0.2e,且最好含
1.1.1 或 3.0 字样
编译安装时必须添加 http_v2_module
下载源码后,在 ./configure 步骤中明确加入:
- --with-http_v2_module(必需,启用 HTTP/2 协议栈)
- --with-http_ssl_module(必需,HTTPS 是 HTTP/2 的运行前提)
- --with-openssl=/path/to/openssl-source(若系统 OpenSSL 过旧,需指定新版源码路径)
示例完整命令:
./configure --prefix=/usr/local/nginx \--with-http_v2_module \
--with-http_ssl_module \
--with-openssl=/tmp/openssl-1.1.1w \
--with-http_realip_module \
--with-http_stub_status_module
避免常见编译陷阱
即使加了 --with-http_v2_module,仍可能因依赖缺失导致静默失效:
- 未安装
zlib-devel、openssl-devel(CentOS/RHEL)或libssl-dev、zlib1g-dev(Ubuntu/Debian)会导致模块编译跳过 - OpenSSL 路径错误或版本太低,会使
http_ssl_module编译失败,连带影响 HTTP/2 - 使用 OpenResty 时,需确认其内置 Nginx 版本 ≥ 1.9.5 且 configure 日志中出现
checking for http_v2 module ... found
安装后立即验证模块是否生效
完成 make && make install 后,不要直接配置,先做三步检查:
-
/usr/local/nginx/sbin/nginx -V —— 确认输出中含
with-http_v2_module - /usr/local/nginx/sbin/nginx -t —— 配置语法校验(此时可暂不写 http2 相关配置)
- 启动后用 curl -I --http2 https://your-domain.com 测试(需已配好 HTTPS)


















