nginx的Debugging

使用自定义日志格式

您可以在 Nginx 配置中作为变量访问的任何内容都可以记录,包括非标准的 HTTP 标头等。因此,这是一种针对特定情况创建自己的日志格式的简单方法。

这对于调试特定的 location 指令非常有帮助。

示例:

## Default main log format from the Nginx repository:
log_format main
                '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for"';
## Extended main log format:
log_format main-level-0
                '$remote_addr - $remote_user [$time_local] '
                '"$request_method $scheme://$host$request_uri '
                '$server_protocol" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent" '
                '$request_time';
## Debug log formats:
log_format debug-level-0
                '$remote_addr - $remote_user [$time_local] '
                '"$request_method $scheme://$host$request_uri '
                '$server_protocol" $status $body_bytes_sent '
                '$request_id $pid $msec $request_time '
                '$upstream_connect_time $upstream_header_time '
                '$upstream_response_time "$request_filename" '
                '$request_completion';

使用调试模式来跟踪意外行为

通常,error_log 指令是在 main 中指定的,但是也可以在 server 或 location 块中指定,全局设置将被覆盖,并且这个 error_log 指令将设置其自己的日志文件路径和日志记录级别。

如果要记录 ngx_http_rewrite_module (at the notice level) ,应该在 httpserver 或 location 块中开启 rewrite_log on;

注意:

  • 永远不要将调试日志记录留在生产环境中的文件上
  • 不要忘记在流量非常高的站点上恢复 error_log 的调试级别
  • 必须使用日志回滚政策

示例:

  • 将 debug 信息写入文件
## Turn on in a specific context, e.g.:
##   - global    - for global logging
##   - http      - for http and all locations logging
##   - location  - for specific location
error_log /var/log/nginx/error-debug.log debug;
  • 将 debug 信息写入内存
error_log memory:32m debug;

IP 地址/范围的调试日志:

events {
  debug_connection    192.168.252.15/32;
  debug_connection    10.10.10.0/24;
}

为不同服务器设置不同 Debug 配置

error_log /var/log/nginx/debug.log debug;
...
http {
  server {
    ## To enable debugging:
    error_log /var/log/nginx/domain.com/domain.com-debug.log debug;
    ## To disable debugging:
    error_log /var/log/nginx/domain.com/domain.com-debug.log;
    ...
  }
}

核心转储

核心转储基本上是程序崩溃时内存的快照。

Nginx 是一个非常稳定的守护程序,但是有时可能会发生正在运行的 Nginx 进程独特终止的情况。

如果要保存内存转储,它可以确保应启用两个重要的指令,但是,为了正确处理内存转储,需要做一些事情。 有关它的完整信息,请参见转储进程的内存(来自本手册)。

当您的 Nginx 实例收到意外错误或崩溃时,应始终启用核心转储。

示例:

worker_rlimit_core    500m;
worker_rlimit_nofile  65535;
working_directory     /var/dump/nginx;

发表评论

后才能评论