A debugging log调试日志

Debugging log for selected clients所选客户端的调试日志
Logging to a cyclic memory buffer记录到循环内存缓冲区

To enable a debugging log, nginx needs to be configured to support debugging during the build:要启用调试日志,需要将nginx配置为在生成期间支持调试:

./configure --with-debug ...

Then the debug level should be set with the error_log directive:然后,应使用error_log指令设置debug级别:

error_log /path/to/log debug;

To verify that nginx is configured to support debugging, run the nginx -V command:要验证nginx是否配置为支持调试,请运行nginx -V命令:

configure arguments: --with-debug ...

Pre-built Linux packages provide out-of-the-box support for debugging log with the nginx-debug binary (1.9.8) which can be run using commands

service nginx stop service nginx-debug start

and then set the debug level. 然后设置debug级别。The nginx binary version for Windows is always built with the debugging log support, so only setting the debug level will suffice.用于Windows的nginx二进制版本始终使用调试日志支持构建,因此仅设置debug级别就足够了。

Note that redefining the log without also specifying the debug level will disable the debugging log. 请注意,重新定义日志而不指定debug级别将禁用调试日志。In the example below, redefining the log on the server level disables the debugging log for this server:在下面的示例中,在server级别重新定义日志将禁用此服务器的调试日志:

error_log /path/to/log debug;

http {
    server {
        error_log /path/to/log;
        ...

To avoid this, either the line redefining the log should be commented out, or the debug level specification should also be added:为了避免这种情况,应该注释掉重新定义日志的行,或者还应该添加debug级别规范:

error_log /path/to/log debug;

http {
    server {
        error_log /path/to/log debug;
        ...

Debugging log for selected clients所选客户端的调试日志

It is also possible to enable the debugging log for selected client addresses only:还可以仅为选定的客户端地址启用调试日志:

error_log /path/to/log;

events {
    debug_connection 192.168.1.1;
    debug_connection 192.168.10.0/24;
}

Logging to a cyclic memory buffer记录到循环内存缓冲区

The debugging log can be written to a cyclic memory buffer:调试日志可以写入循环内存缓冲区:

error_log memory:32m debug;

Logging to the memory buffer on the debug level does not have significant impact on performance even under high load. 即使在高负载下,在debug级别记录到内存缓冲区也不会对性能产生显著影响。In this case, the log can be extracted using a gdb script like the following one:在这种情况下,可以使用如下gdb脚本提取日志:

set $log = ngx_cycle->log 
while $log->writer != ngx_log_memory_writer     set $log = $log->next end 
set $buf = (ngx_log_memory_buf_t *) $log->wdata dump binary memory debug_log.txt $buf->start $buf->end