nginx Access日志格式

默认,access日志路径是./logs/access.log, 默认的日志格式为combined格式;
使用log_format指令可以自定义日志格式;

语法

log_format name [escape=default|json|none] string ...;

escape参数(1.11.8)设置变量的字符转义,json或default风格;默认使用default风格;none关闭转义;

示例,自定义compression日志格式,并使用

http {
    log_format compression '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $body_bytes_sent '
                           '"$http_referer" "$http_user_agent" "$gzip_ratio"';

    server {
        gzip on;
        access_log /spool/logs/nginx-access.log compression;
        ...
    }
}

默认的combined格式为

log_format combined '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent '
                    '"$http_referer" "$http_user_agent"';

默认格式示例

192.168.1.186 - - [06/Aug/2018:09:57:51 +0800] "GET /Public/Css/plugins/morris/morris-0.4.3.min.css HTTP/1.1" 200 442 "http://www.example.com/index.php/Login/index.html" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0"

常用变量

变量名说明
$bytes_sent发送给客户端的字节数
$connection连接序列号
$connection_requests当前通过连接发出的请求数
$msec秒.毫秒;日志写入时间
$pipe如果请求是管道线,则为"p",否则为"."
$request_length请求长度(包括请求行,请求头和请求体)
$request_time秒.毫秒;请求处理时长;从客户端读取第1个字节开始(请求),到最后1个字节发送给客户端为止(响应)
$status响应状态码
$time_iso8601ISO 8601标准格式的本地时间,如"2018-08-06T09:57:51+08:00"
$time_local通用日志格式的本地时间,如"06/Aug/2018:09:57:51 +0800"

发送给客户端的变量,会有前辍"sent_http_",如"$sent_http_content_range"

每个模块有内置变量可用于记录日志

http_core模块

变量名说明
$arg_namename为请求行里的参数名
$args请求行里的参数
$binary_remote_addr
$body_bytes_sent发送给客户端的字节数,不包括响应头的大小
$bytes_sent发送给客户端的字节数
$connection连接序列号
$connection_requests当前通过连接发出的请求数
$content_length请求头字段"Content-Length"
$content_type请求头字段"Content-Type"
$cookie_namename为某个cookie的名字
$document_root
$document_uri同$uri
$host请求行里的host name或请求头字段"Host"或响应请求的服务器名
$hostname主机名
$http_namename为请求头字段名小写;如user_agent,cookie等
$https
$is_args
$limit_rate
$msec秒.毫秒;日志写入时间
$nginx_versionnginx的版本
$pid工作进程的pid
$pipe如果请求是管道线,则为"p",否则为"."
$proxy_protocol_addr
$proxy_protocol_port
$query_string同$args
$realpath_root
$remote_addr客户端请求IP
$remote_port客户端请求端口
$remote_user客户端用户名称,要启用用户认证才会有值
$request完整的原始请求行,如 “GET / HTTP/1.1”
$request_body请求体
$request_body_file
$request_completion
$request_filename
$request_id
$request_length请求长度(包括请求行,请求头和请求体)
$request_method请求方法,如"GET",“POST”
$request_time秒.毫秒;请求处理时长;从客户端读取第1个字节开始(请求),到最后1个字节发送给客户端为止(响应)
$request_uri完整的请求地址,如 “https://example.com/”
$scheme请求模式,如"http"或"https"
$sent_http_namename为响应头字段名小写;
$sent_trailer_name
$server_addr接受请求的服务器地址
$server_name接受请求的服务器名
$server_port接受请求的服务器端口
$server_protocol请求协议,如"HTTP/1.0"
$status响应状态码
$tcpinfo_rtt, $tcpinfo_rttvar, $tcpinfo_snd_cwnd, $tcpinfo_rcv_space
$time_iso8601ISO 8601标准格式的本地时间,如"2018-08-06T09:57:51+08:00"
$time_local通用日志格式的本地时间,如"06/Aug/2018:09:57:51 +0800"
$uri当前请求的URI;当内部转跳时,值会变

$http_name一般为

变量名说明
$http_user_agent请求头字段"User-Agent";发出请求的设备
$http_referer请求头字段"Referer";发出请求时所在的链接

$sent_http_name一般为

变量名说明
$sent_http_content_length响应头的"Content-Length";响应内容的长度

其他模块,参考官方文档;

参考:
https://nginx.org/en/docs/http/ngx_http_core_module.html#variables

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值