
如何重新编译 nginx 开启 --with-debug 并配置 debug 级别日志【免费下载链接】nginxThe official NGINX Open Source repository.项目地址: https://gitcode.com/GitHub_Trending/ng/nginxNGINX 的 debug 日志不是运行时可以随意打开的开关。官方 man 页面 docs/man/nginx.8 的 DEBUGGING LOG 一节明确写道要启用调试日志必须重新配置编译reconfigure即./configure --with-debug ...之后再给error_log指令设置debug级别。本文依据 nginx 仓库中的 man 页面、构建脚本和默认配置整理出一条完整路径准备编译依赖 → 带--with-debug执行 configure → 编译安装 → 修改error_log配置 → 用nginx -V、nginx -t和实际请求验证 debug 日志是否生效。适用环境使用本仓库源码构建构建步骤来自 README.md其依赖安装命令基于apt包管理器适用于 Ubuntu/Debian 系发行版。准备条件源码与编译依赖按 README.md 的 Building from source 章节构建前需要准备获取源码仓库git clone https://github.com/nginx/nginx.git安装编译器与构建工具需要 root/sudo 权限会向系统安装 gcc 和 make 包sudo apt update sudo apt install gcc make安装最低依赖库README 说明这是构建带有 rewriting 和 gzip 能力的 NGINX 所需的最小依赖集sudo apt install libpcre3-dev zlib1g-dev如果你后续还要启用 SSL 相关模块README 提示需要额外安装libssl-dev并提醒要留意configure阶段的输出它会指出缺少的模块依赖sudo apt install libssl-dev配置编译带 --with-debug 运行 configureman 页面给出的写法是./configure --with-debug ...其中...表示你还需要附加的其他构建选项。在本仓库中configure 脚本实际位于auto/目录下README.md 的构建步骤使用的命令是auto/configure二者指同一脚本在源码根目录执行即可。--with-debug是合法的构建选项auto/options 中对应的处理逻辑为--with-debug) NGX_DEBUGYES帮助文本描述为--with-debug enable debug logging最简单的 debug 构建配置auto/configure --with-debugconfigure 成功执行后会在源码根目录生成MakefileREADME 原话Theconfigurescript will generate aMakefilein the NGINX source root directory upon successful execution。可选分支如果你的构建还需要其他模块在同一行追加对应选项即可例如同时启用 SSL 模块auto/configure --with-debug --with-http_ssl_module完整的官方构建参数示例可参考 misc/GNUmakefile 中的win32目标其中./auto/configure一行同时带了--with-debug、--prefix、--conf-pathconf/nginx.conf、--error-log-pathlogs/error.log等大量选项可作为组合参数时的参照。编译与安装在源码根目录依次执行均来自 README.mdmake编译成功后二进制文件生成在NGINX_SRC_ROOT_DIR/objs/nginx。然后安装sudo make install注意make install会向/usr/local/nginx/目录写入可执行文件和默认配置文件README 明确说明 The binary will be installed into the/usr/local/nginx/directory。配置 debug 级别日志编译选项只负责让二进制具备 debug 能力真正输出 debug 日志还要改配置。man 页面的 DEBUGGING LOG 一节的完整要求是两步./configure --with-debug ... error_log /path/to/log debug;即给error_log指令追加debug级别参数/path/to/log替换为实际日志文件路径。仓库默认配置 conf/nginx.conf 中error_log相关行原本是被注释的#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;因此安装后在nginx.conf默认位于前缀目录下的conf/nginx.conf中取消其中一行注释并加上debug级别得到一个具体写法error_log logs/error.log debug;logs/error.log是仓库默认配置与构建脚本共同使用的相对路径相对于安装前缀也可按 man 页面示例改为绝对路径。如果只想对特定客户端 IP 输出 debug 日志man 页面还给出了第二个可选配置写在events块中events { debug_connection 127.0.0.1; }man 页面的原文说明是 It is also possible to enable the debugging for a particular IP address。这个指令对 debug 构建有依赖关系后面的限制一节会说明。验证结果按文档给出的检查方式逐项确认1. 确认二进制确实带--with-debug构建。man 页面说明-V选项会 Print the version, compiler version, and configure script parameters即构建时的 configure 参数会原样打印出来sudo /usr/local/nginx/sbin/nginx -V在输出的 configure 参数列表中应能看到--with-debug。如果看不到说明当前运行的二进制不是带 debug 重新编译的版本debug 级别配置不会生效。2. 测试新配置语法。man 页面说明-t选项 Do not run, just test the configuration file会检查语法并尝试打开配置引用的文件sudo /usr/local/nginx/sbin/nginx -t3. 启动 nginx 并发起请求。按 README.md 的方式运行并测试sudo /usr/local/nginx/sbin/nginxcurl localhostREADME 给出该请求的预期结果输出以如下内容开头文档示例!DOCTYPE html html head titleWelcome to nginx!/title4. 查看 debug 日志。请求完成后查看配置中指定的日志文件上例为logs/error.log其中应出现error_log配置的debug级别记录。man 页面还记录了-e选项可用于指定替代的 error log 文件stderr有特殊含义表示写入标准错误输出例如调试期间可以把 debug 日志写到单独文件不影响主错误日志sudo /usr/local/nginx/sbin/nginx -e /tmp/nginx-debug.log限制与注意事项--with-debug与debug级别是配套关系。man 页面的措辞是 To enable a debugging log, reconfigure nginx to build with debugging——只改error_log ... debug而不重新编译debug 日志不会开启。debug_connection对编译选项有硬性依赖。源码 src/event/ngx_event.c 中对未以 debug 构建的二进制给出的告警原文是debug_connection is ignored, you need to rebuild nginx using --with-debug option to enable it也就是说用普通构建的 nginx 写了debug_connection只会收到这条忽略提示不会有任何 debug 输出必须重新编译。依赖缺失会在 configure 阶段暴露。README 提醒最小依赖集只保证基础功能添加其他模块时 Monitor the output of theconfigurecommand根据输出补装对应库不要等到make阶段才发现失败。构建参数在编译期固定nginx -V打印的就是当时的 configure 参数如果之后再次调整auto/configure的选项需要重新走 configure → make → make install 全流程。【免费下载链接】nginxThe official NGINX Open Source repository.项目地址: https://gitcode.com/GitHub_Trending/ng/nginx创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考