大家好,又见面了,我是你们的朋友全栈君。
默认,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_iso8601 | ISO 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_name | name为请求行里的参数名 |
| $args | 请求行里的参数 |
| $binary_remote_addr | |
| $body_bytes_sent | 发送给客户端的字节数,不包括响应头的大小 |
| $bytes_sent | 发送给客户端的字节数 |
| $connection | 连接序列号 |
| $connection_requests | 当前通过连接发出的请求数 |
| $content_length | 请求头字段”Content-Length” |
| $content_type | 请求头字段”Content-Type” |
| $cookie_name | name为某个cookie的名字 |
| $document_root | |
| $document_uri | 同$uri |
| $host | 请求行里的host name或请求头字段”Host”或响应请求的服务器名 |
| $hostname | 主机名 |
| $http_name | name为请求头字段名小写;如user_agent,cookie等 |
| $https | |
| $is_args | |
| $limit_rate | |
| $msec | 秒.毫秒;日志写入时间 |
| $nginx_version | nginx的版本 |
| $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_name | name为响应头字段名小写; |
| $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_iso8601 | ISO 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
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/132719.html原文链接:https://javaforall.net
