nginx 日志格式规范

  • k8s-nginx基础镜像-格式

  log_format main '$remote_addr|$remote_user|[$time_local]|"$request"' '|$status|$request_time|$body_bytes_sent|"$http_referer"' '|"$http_user_agent"|"$http_x_forwarded_for"|$upstream_response_time|$upstream_status';

  • k8s-nginx基础镜像-输出样例

  10.206.252.64|-|[20/Mar/2020:13:39:41 +0800]|"GET /stream/10 HTTP/1.1"|200|0.001|3320|"-"|"-"|"-"|0.001|200

格式

  • 参考约束
    UTF-8
    换行,|需转义

  • 字段含义

nginx日志变量
变量 含义

$remote_addr

客户端IP 
 $remote_user  客户端用户名称,针对启用了用户认证的请求
 $time_local  通用日志格式下的本地时间,如”24/May/2017:18:31:27 +0800”
 $request  完整的原始请求行,如 “GET / HTTP/1.1”
 $status  响应状态码
 $request_time  请求处理时长,单位为秒,精度为毫秒,从读入客户端的第一个字节开始,直到把最后一个字符发送张客户端进行日志写入为止
 $body_bytes_sent  发送给客户端的字节数,不包括响应头的大小
 $http_referer  请求的referer地址。
 $http_user_agent  客户端浏览器信息。
 $http_x_forwarded_for  当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置。
 $upstream_response_time  upstream时长
 $upstream_status  upstream状态

 

 

分隔符、日期格式,暂不和应用日志做统一

  • 字段含义-扩展

如下字段,根据需要,可作为扩展字段,加到默认字段的后面,日志采集时需提供扩展字段信息。

nginx日志变量扩展字段
变量 含义
$bytes_sent 发送给客户端的总字节数
$connection 连接序列号
$connection_requests 当前通过连接发出的请求数量
$msec 日志写入时间,单位为秒,精度是毫秒
$pipe 如果请求是通过http流水线发送,则其值为”p”,否则为“.”
$request_length 请求长度(包括请求行,请求头和请求体)
$time_iso8601 标准格式的本地时间,形如“2017-05-24T18:31:27+08:00”
$request_uri 完整的请求地址,如 “https://daojia.com/“

 

 

 

配置

  • k8s-nginx基础镜像里面的配置

/app/openresty/nginx/conf/nginx.conf

error_log /app/openresty/nginx/logs/error.log error;

pid /app/openresty/nginx/logs/nginx.pid;

http {

  include mime.types;

  include custom_upstream.conf;

  default_type application/octet-stream;

  log_format main '$remote_addr|$remote_user|[$time_local]|"$request"'

           '|$status|$request_time|$body_bytes_sent|"$http_referer"'

           '|"$http_user_agent"|"$http_x_forwarded_for"|$upstream_response_time|$upstream_status';

 

  access_log /app/openresty/nginx/logs/access.log main;

}

 

posted @ 2023-01-16 16:46  谢端阳  阅读(492)  评论(0编辑  收藏  举报