Nginx源码编译时make报错多因环境、依赖或配置不匹配所致,需先确认configure成功生成完整Makefile,再据错误信息定位:缺PCRE等依赖则安装pcre-devel等包;--with-pcre参数须指向源码目录而非安装路径;-Werror导致警告变错误则删objs/Makefile中-Werror;多OpenSSL版本冲突需显式指定includes/libraries路径并清理pkg-config缓存。

编译 Nginx 源码时 make 报错,多数不是代码本身问题,而是环境、依赖或配置不匹配导致的。关键在快速定位错误类型,再针对性处理。
检查 configure 是否真正成功
很多 make 报错根源其实在上一步:./configure 看似运行完,但若检测失败(比如缺 PCRE、zlib、OpenSSL 开发包),它可能仍生成一个不完整的 Makefile,后续 make 就会出各种“找不到规则”“No rule to make target 'build'”等错误。
- 执行
./configure后,务必确认最后几行出现 “Configuration summary” 并列出启用的模块(如using PCRE library: /usr) - 如果看到 “checking for PCRE library ... not found” 或类似提示,说明依赖缺失,必须先装好再重跑
configure - 常见必需依赖(CentOS/RHEL):
yum install -y gcc pcre-devel zlib-devel openssl-devel
识别典型 make 错误并对应解决
报错信息里带路径和关键字,是判断类型的直接依据:
-
“/bin/sh: line 2: ./configure: No such file or directory”
→ 这是
--with-pcre=/path/to/dir指错了:它要的是 PCRE 源码目录(含configure脚本),不是安装后的/usr/lib或/usr/local。换成类似--with-pcre=/root/pcre-8.45/(解压后的源码路径) -
“error: this statement may fall through [-Werror=implicit-fallthrough=]”
→ GCC 版本较新,默认把警告当错误。打开
objs/Makefile,搜索-Werror,删掉这一项,保存后重make -
“struct crypt_data has no member named ‘current_salt’”
→ 与系统 libc 或 crypt 实现不兼容,常见于新版 glibc。最稳妥是换用较新的 Nginx 版本(如 1.21+),或临时注释掉
src/os/unix/ngx_user.c中相关行(仅调试用) -
“make: 警告: 检测到时钟错误”
→ 系统时间被调过,导致部分文件时间戳异常。进源码根目录执行:
find . -type f -exec touch {} \;,再make
多 OpenSSL 版本冲突的处理
如果报错含 SSL_CTX_set_ciphersuites、undefined reference to SSL_*,大概率是 configure 混用了不同版本的头文件和库:
- 先查清楚你真正想用的 OpenSSL 安装在哪,比如
/opt/openssl-1.1.1w/install - configure 时显式指定全部路径:
--with-openssl=/opt/openssl-1.1.1w/install \<br> --with-openssl-includes=/opt/openssl-1.1.1w/install/include \<br> --with-openssl-libraries=/opt/openssl-1.1.1w/install/lib \<br> --with-cc-opt="-I/opt/openssl-1.1.1w/install/include" \<br> --with-ld-opt="-L/opt/openssl-1.1.1w/install/lib -Wl,-rpath,/opt/openssl-1.1.1w/install/lib"
- 同时清空旧的 pkg-config 缓存:
rm -f /usr/lib64/pkgconfig/openssl.pc或临时改名
验证 Makefile 和基础工具链
确保底层工具就绪,避免低级但致命的问题:
- 确认已安装
make:which make,没有就yum install -y make - 确认
Makefile存在且非空:ls -l Makefile,如果不存在或大小为 0,说明configure根本没走通 - 检查是否误删了源码中关键子目录(如
objs/、auto/),可重新解压源码再试


















