日志模块(ngx_http_log_module)
在全局块中,我们介绍过error_log指令,其用于nginx进程运行时的日志存放和级别。此处所指的日志与常规日志不同,它是记录nginx服务器提供服务过程应答前端请求的日志,我们将其称为服务日志。
示例:
1 2 |
log_format main '"$time_iso8601" "$remote_addr" "$remote_user" "$request" "$uri" $request_time ' '$status $body_bytes_sent "$http_referer" "$http_user_agent" $upstream_addr $upstream_status $upstream_response_time'; |
1)access_log
1 2 3 4 5 6 |
Syntax: access_log path [format [buffer=size [flush=time]] [if=condition]]; access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition]; access_log syslog:server=address[,parameter=value] [format [if=condition]]; access_log off; Default:access_log logs/access.log combined; Context:http, server, location, if in location, limit_except |
path – 配置服务日志文件存放的路径和名称;
format – 可选项,自定义服务日志的格式字符串,也可以通过”格式串的名称“使用log_format指令定义好的格式。格式串名称在log_format指令中定义;
buffer – 设置内存缓存区大小;
flush – 保存在缓存区中的最长时间;
gzip – 表示压缩等级;
syslog:server – 表示使用系统的syslog把nginx日志传输到远程服务器;
access_log off – 不记录日志;
Nginx的默认日志在logs/access.log文件中,且使用一个默认的 combined 日志格式。
2)log_format
1 2 3 |
Syntax: log_format name string ...; Default:log_format combined "..."; Context:http |
定义nginx日志格式,其中name表示格式名称,注意日志格式的名字不能够重复;string表示等义的格式。Nginx提供了默认的combined日志格
式,日志格式如上http{}配置段的内容。其中带$符号的都是引用的变量,http核心模块自带的,下面对日志格式常用的变量做个说明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
$remote_addr #记录客户端IP地址; $remote_user #记录客户端用户名称; $time_local #通用日志格式下的本地时间; $time_iso8601 #ISO8601标准格式下的本地时间; $request #记录请求的URL和HTTP协议; $status #记录请求状态; $body_bytes_sent #发送给客户端的字节数,不包括响应头的大小; $http_referer #记录从哪个页面链接访问过来的; $bytes_sent #发送给客户端的总字节数; $http_user_agent #记录客户端浏览器相关信息; $connection #连接的序列号; $connection_requests #当前通过一个连接获得的请求数量; $msec #日志写入时间,单位为秒,精度是毫秒; $pipe #如果请求是通过HTTP流水线(pipelined)发送,pipe值为p,否则为.; $request_length #请求的长度(包括请求行,请求头和请求正文); $request_time #请求处理时间,单位为秒,精度毫秒;从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止; $upstream_response_time #在nginx启用upstream的情况下,记录upstream响应的时间; $upstream_addr #在nginx启用upstream的情况下,记录upstream服务器地址; $upstream_status #在nginx启用upstream的情况下,记录upstream响应的状态; $http_x_forwarded_for #如果nginx服务器位于反向代理之后,$remote_addr只能获取反向代理服务器的IP地址,无法直接获取到客户端真实的IP地址; #而反向代理服务器可以在转发请求的http头信息中增加http_x_forwarded_for变量,用来记录客户端IP地址和客户端请求的服务器 #地址,然后一并发给nginx服务器; |
Nginx默认的日志格式信息如下:
1 |
172.16.17.96 - - [20/Apr/2017:12:03:24 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36" |
这里我在/etc/nginx/nginx.conf配置文件中自定义一个带说明信息的日志格式(这种日志格式利于python以字典的方式处理),格式名称为”json”,然后看看日志信息会是什么样子。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
log_format json '{"timestamp":"$time_iso8601",' '"server_addr":"$server_addr",' '"remote_addr":"$remote_addr",' '"body_bytes_sent":$body_bytes_sent,' '"status":"$status",' '"request":"$request",' '"url":"$uri",' '"http_referer":"$http_referer",' '"request_time":$request_time,' '"upstream_response_time":"$upstream_response_time",' '"upstream_addr":"$upstream_addr",' '"upstream_status":"$upstream_status",' '"http_user_agent":"$http_user_agent" }'; |
然后在/etc/nginx/conf.d/server.conf虚拟主机文件中,引用”json”这个日志格式,看看日志信息:
1 |
access_log /var/log/nginx/access.log json; # 这是我编译时默认使用的日志文件路径; |
重新加载nginx,然后访问日志文件看看日志信息效果如下所示:
1 2 3 4 5 6 7 8 9 10 |
{"timestamp":"2017-12-22T11:31:54+08:00", "server_addr":"112.74.99.130", "remote_addr":"180.171.121.167", "body_bytes_sent":17305,"status":"200", "request_time":0.000, "upstream_response_time":"-", "upstream_addr":"-", "url":"/wp-content/themes/Git/js/share.js", "http_referer":"http://www.ywnds.com/?p=8184", "http_user_agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36" } |
3)open_log_file_cache
1 2 3 4 |
Syntax: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; open_log_file_cache off; Default:open_log_file_cache off; Context:http, server, location |
对于每一条日志记录,都将是先打开文件,再写入日志,然后关闭。可以使用open_log_file_cache来设置日志文件缓存(默认是off),格式如下:
1 |
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2; |
max – 设置缓存中的最大文件描述符数量,如果缓存被占满,采用LRU算法将描述符关闭。
inactive – 设置存活时间,默认是10s
min_uses – 设置在inactive时间段内,日志文件最少使用多少次后,该日志文件描述符记入缓存中,默认是1次
valid – 设置检查频率,默认60s
off – 禁用缓存