编译 Nginx 时必须用 --with-openssl=/path/to/openssl-src 指向 OpenSSL 源码根目录,不可用预编译库或安装包;需解压官方源码、不执行 make install,并确保路径包含 include/ 和 Configure 等文件。

编译 Nginx 时若需使用自定义版本的 OpenSSL(比如新版以支持 TLS 1.3 或修复安全漏洞),不能仅靠系统已安装的 openssl-devel 包,必须显式指定 OpenSSL 源码路径——因为 Nginx 的 ./configure 脚本只认源码目录,不接受预编译的库路径。
确保 OpenSSL 已解压且未安装
OpenSSL 官方源码(如 openssl-3.0.13.tar.gz)需先解压到某目录(例如 /path/to/openssl-3.0.13),但不要执行 make install。Nginx 编译过程会直接调用该目录下的头文件和静态/动态对象,依赖的是源码树结构(尤其是 include/ 和 .libs/ 或 libcrypto.a 等生成物)。若已安装过,建议清理系统 OpenSSL 开发包(如 yum remove openssl-devel 或 apt remove libssl-dev),避免 configure 自动探测到旧版本。
在 Nginx configure 中用 --with-openssl 指向源码根目录
进入 Nginx 源码目录后,运行 configure 命令时加入:
-
--with-openssl=/path/to/openssl-3.0.13:这是唯一有效方式,路径必须是 OpenSSL 解压后的顶层目录(含Configure、include/、crypto/等子目录) - 可选搭配:
--with-openssl-opt="enable-tls1_3 no-asm",用于向 OpenSSL 的 Configure 传递额外参数(如禁用汇编优化以提升兼容性) - 无需再写
--with-http_ssl_module(它默认启用,且依赖 OpenSSL)
验证是否生效
configure 执行完成后,检查输出末尾是否出现类似:
checking for OpenSSL library ... found+ using OpenSSL from /path/to/openssl-3.0.13
编译(make)完成后,用以下命令确认实际链接的 OpenSSL 版本:
-
ldd objs/nginx | grep ssl—— 查看动态链接的 libssl.so 路径(若静态链接则无此输出) -
objs/nginx -V 2>&1 | grep -i openssl—— 输出中应显示 OpenSSL 版本号,例如built with OpenSSL 3.0.13 - 启动后访问
https://your-server/并用openssl s_client -connect your-server:443 -tls1_3测试协议支持
常见失败原因与绕过技巧
如果 configure 报错 “OpenSSL version too old” 或 “not found”,可能是:
- OpenSSL 源码未完整解压(缺少
.git或补丁未打全),建议从官网下载完整 tarball,勿用 git clone(除非明确 checkout release tag 并运行./Configure生成必要文件) - 路径写错(如指向了
openssl-3.0.13/include而非根目录) - 权限问题:确保 configure 进程对 OpenSSL 目录有读取权限
- 交叉编译场景下,需额外加
--with-openssl-opt="cross_compile_prefix=arm-linux-gnueabihf-"










