必须用支持ALPN的OpenSSL(≥1.0.2e,推荐1.1.1u/3.x)编译Nginx并启用http_v2_module,配置listen 443 ssl http2,再通过curl --http2和浏览器Protocol列验证h2生效。

要在 Nginx 中真正启用 HTTP/2,不能只看 Nginx 版本是否 ≥1.9.5,关键在于它是否用满足 ALPN 协议支持的 OpenSSL 编译链接。ALPN 是 TLS 层协商 HTTP/2 的必要机制,而旧版 OpenSSL(如 1.0.1e 或系统默认的 1.0.2k 虽然版本号够但可能未启用 ALPN)无法完成该协商,导致 http_v2_module 形同虚设。
确认当前 OpenSSL 是否真正支持 HTTP/2
运行以下命令逐项验证:
- openssl version:输出必须是 1.0.2e 及以上,更推荐 1.1.1u 或 3.x 系列(如 3.0.2);若显示 1.0.1e、1.0.2a 等,需升级
- ldd $(which nginx) | grep ssl:检查 Nginx 实际链接的动态库路径,应指向你新装的 libssl.so.1.1 或 libssl.so.3,而非 /usr/lib64/libssl.so.1.0.2k
-
nginx -V 2>&1 | grep -i openssl:configure 参数中应明确含
--with-openssl=/usr/local/ssl或类似路径,而非仅依赖系统默认
升级 OpenSSL 并建立正确链接
不建议直接覆盖系统 OpenSSL(尤其生产环境),推荐安装到独立路径(如 /usr/local/openssl),再通过软链和动态库配置生效:
- 下载源码(例如
openssl-1.1.1u.tar.gz或openssl-3.0.2.tar.gz),解压后执行:./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl shared zlib make && make install- 创建软链接:
ln -sf /usr/local/openssl/bin/openssl /usr/bin/opensslln -sf /usr/local/openssl/include/openssl /usr/include/openssl - 更新动态库路径:
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf && ldconfig -v
重新编译 Nginx 并显式启用 http_v2_module
确保 configure 命令中同时包含 --with-http_ssl_module 和 --with-http_v2_module,且指定新版 OpenSSL 路径:
- 若 OpenSSL 安装在
/usr/local/openssl:./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-openssl=/usr/local/openssl --with-http_stub_status_module - 务必省略
--with-openssl=后跟源码目录(如../openssl-1.1.1u)——那是编译时临时引用,容易出错;应使用已安装好的完整路径 - 执行
make && make install,替换旧二进制文件前建议备份原/usr/local/nginx/sbin/nginx
验证 HTTP/2 是否真正生效
重启 Nginx 后,用终端命令验证端到端可用性:
-
curl -I --http2 https://your-domain.com:响应头中应含
HTTP/2 200,而非HTTP/1.1 -
nginx -V 2>&1 | grep -o with-http_v2_module:输出必须为
with-http_v2_module(非空) - Nginx 配置中监听 HTTPS 必须加
http2关键字:listen 443 ssl http2;(仅ssl不够)











