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:

debug等级的设置可由error_log指令设置

error_log /path/to/log debug;

To verify that nginx is configured to support debugging, run the nginx -V command:

要验证nginx配置是否支持debugging,请运行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

预编译的Linux软件包通过nginx-debug二进制文件(nginx版本 1.9.8)为调试日志提供了开箱即用的支持,可以使用该命令开始debugging

service nginx stop
service nginx-debug start

and then set the debug level. The nginx binary version for Windows is always built with the debugging log support, so only setting the debug level will suffice.

然后设置debug级别。Windows版的nginx二进制版本始终支持调试日志,因此只需设置debug级别即可。

Note that redefining the log without also specifying the debug level will disable the debugging log. In the example below, redefining the log on the server level disables the debugging log for this server:

请注意,重新设置日志而不同时指定debug级别将禁用调试日志。在下面的示例中,在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. 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

Or using an lldb script as follows:

或者使用如下的lldb 脚本

expr ngx_log_t *$log = ngx_cycle->log
expr while ($log->writer != ngx_log_memory_writer) { $log = $log->next; }
expr ngx_log_memory_buf_t *$buf = (ngx_log_memory_buf_t *) $log->wdata
memory read --force --outfile debug_log.txt --binary $buf->start $buf->end