nginx 日志记录:收到的第一个字节和最后一个字节

sid*_*ngh 7 logging nginx

我需要记录 nginx 花费大量时间来响应用户的所有请求。上游时间为 200 毫秒,其中 nginx 响应时间 > 333 秒我希望 nginx 记录:

  1. 当它收到请求的第一个字节时
  2. 当它收到请求的最后一个字节时
  3. 当它向上游发送请求时
  4. 当收到上游响应时

Nij*_*jo 2

您可以通过编辑 nginx 配置来创建自定义日志记录模式/etc/nginx/nginx.conf

log_format apm '"$time_local" client=$remote_addr '
           'method=$request_method request="$request" '
           'request_length=$request_length '
           'status=$status bytes_sent=$bytes_sent '
           'body_bytes_sent=$body_bytes_sent '
           'referer=$http_referer '
           'user_agent="$http_user_agent" '
           'upstream_addr=$upstream_addr '
           'upstream_status=$upstream_status '
           'request_time=$request_time '
           'upstream_response_time=$upstream_response_time '
           'upstream_connect_time=$upstream_connect_time '
           'upstream_header_time=$upstream_header_time';
Run Code Online (Sandbox Code Playgroud)

这是一个名为 apm 的示例日志格式,其中包含这四个 NGINX 计时变量以及一些其他有用的信息,并更新访问日志以使用该格式

access_log /var/log/nginx/access.log apm;

要找到需要更长响应时间的请求,您必须将数据发送到 ELK 或类似服务并从那里过滤它: https://www.youtube.com/watch ?v=XaFH2PcYI64&ab_channel=TravelsCode

您可以在 kibana 中创建仪表板以全面了解结果

例如: https: //blog.ruanbekker.com/blog/2019/04/02/setup-kibana-dashboards-for-nginx-log-data-to-understand-the-behavior/