必须在 ./configure 阶段添加 --with-debug 选项才能启用调试日志,否则配置文件中设置 error_log debug 会被静默忽略;编译后需在 nginx.conf 中正确配置 error_log 路径和 debug 级别,并通过 nginx -V 和 nginx -t 验证。
要在源码安装过程中启用调试日志来排查底层问题,关键是在 ./configure 阶段显式开启 --with-debug 编译选项,并在后续配置中将 error_log 级别设为 debug。仅靠配置文件设 debug 级别是无效的——如果编译时没加 --with-debug,Nginx 会静默忽略该级别,日志仍只输出到 error 或更高级别。
运行 ./configure 时,需明确添加 --with-debug 参数:
debug 相关代码会被预处理器剔除pcre-devel 和 openssl-devel)./configure
--prefix=/opt/nginx
--with-debug
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--conf-path=/etc/nginx/nginx.conf
安装完成后,在 nginx.conf 的顶层(如 main 或 http 块)设置:
error_log /var/log/nginx/error.log debug; —— 注意路径必须与 --error-log-path 一致或可写,且级别明确写 debug
debug_core 或 debug_http 等子级别,除非你已确认源码中对应模块启用了细分调试开关debug_connection 指令:
events {debug_connection 192.168.1.100;
}
启用后务必执行以下步骤再启动:
nginx -V 2>&1 | grep -o with-debug,确认输出含 with-debug 字样nginx -t 测试配置,确保无语法错误;若提示 unknown log level "debug",说明编译遗漏 --with-debug
mkdir -p /var/log/nginx && chown nginx:nginx /var/log/nginx
-e stderr 直接看终端输出:/opt/nginx/sbin/nginx -c /etc/nginx/nginx.conf -e stderr
debug 日志信息密集,需有针对性地观察:
[debug] 标识segmentation fault 及附近上下文rewrite_log on; 和 log_not_found off; 可缩小范围warn 或 error