Nginx日志配置概述
日志功能在数据统计与故障排查方面具有显著优势。文章归纳了Nginx中与日志相关的关键配置项,涵盖access_log、log_format、open_log_file_cache、log_not_found、log_subrequest、rewrite_log以及error_log等指令。
Nginx具备高度灵活的日志记录机制,允许在不同配置层级独立设置访问日志。日志的具体格式需借助log_format命令进行自定义,其中ngx_http_log_module模块专门负责定义请求日志的格式规范。
1. access_log指令
语法结构为:access_log path [format [buffer=size [flush=time]]]; 复制代码 代码如下: access_log path format gzip[=level] [buffer=size] [flush=time]; access_log syslog:server=address[,parameter=value] [format]; access_log off;
默认配置为:access_log logs/access.log combined; 适用配置段包括:http, server, location, if in location, limit_except
gzip参数用于指定压缩等级。 buffer参数用于设定内存缓存区的大小。 flush参数用于确定数据在缓存区中的最长保存时间。 若需禁止记录日志,使用:access_log off; 若采用默认的combined格式记录日志,可写为:access_log logs/access.log 或 access_log logs/access.log combined;
2. log_format指令
语法结构为:log_format name string …; 默认值为:log_format combined “…”; 适用配置段为:http
name参数代表格式的名称,string参数定义与之对应的格式字符串。log_format内置一个无需额外设置的combined默认日志格式,其效果等同于Apache的combined日志格式,具体示例如下:
复制代码 代码如下: log_format combined '$remote_addr - $remote_user [$time_local] ' ' "$request" $status $body_bytes_sent ' ' "$http_referer" "$http_user_agent" ';
当Nginx部署在负载均衡器、Squid或反向代理之后时,Web服务器无法直接获取客户端的真实IP地址。此时$remote_addr获取的是反向代理服务器的IP。反向代理可在转发的HTTP头信息中添加X-Forwarded-For字段,以记录客户端IP及请求的目标服务器地址,示例如下:
复制代码 代码如下: log_format porxy '$http_x_forwarded_for - $remote_user [$time_local] ' ' "$request" $status $body_bytes_sent ' ' "$http_referer" "$http_user_agent" ';
日志格式支持包含以下变量,注释说明如下: 复制代码 代码如下: $remote_addr, $http_x_forwarded_for 用于记录客户端IP地址 $remote_user 用于记录客户端用户名称 $request 用于记录请求的URL和HTTP协议版本 $status 用于记录请求的状态码 $body_bytes_sent 记录发送给客户端的字节数,不含响应头大小;该变量兼容Apache模块mod_log_config中的“%B”参数。 $bytes_sent 记录发送给客户端的总字节数。 $connection 记录连接的序列号。 $connection_requests 记录当前通过单一连接获得的请求数量。 $msec 记录日志写入的时间,单位为秒,精度达毫秒。 $pipe 若请求通过HTTP流水线(pipelined)发送,值为“p”,否则为“.”。 $http_referer 记录从哪个页面链接跳转访问过来。 $http_user_agent 记录客户端浏览器的相关信息。 $request_length 记录请求的长度(含请求行、请求头和请求正文)。 $request_time 记录请求处理时间,单位为秒,精度毫秒;从读取客户端首字节开始,直至向客户端发送完最后一个字符后写入日志为止。 $time_iso8601 记录ISO8601标准格式下的本地时间。 $time_local 记录通用日志格式下的本地时间。
发送给客户端的响应头字段拥有“sent_http_”前缀,例如$sent_http_content_range。
具体实例展示如下:
复制代码 代码如下: http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '"$status" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '"$gzip_ratio" $request_time $bytes_sent $request_length';
log_format srcache_log '$remote_addr - $remote_user [$time_local] "$request" ' '"$status" $body_bytes_sent $request_time $bytes_sent $request_length ' '[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]';
open_log_file_cache max=1000 inactive=60s;
server { server_name ~^(www\.)?(.+)$; access_log logs/$2-access.log main; error_log logs/$2-error.log;
location /srcache { access_log logs/access-srcache.log srcache_log; } } }
3. open_log_file_cache指令
语法结构为:open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time]; 或者设置为:open_log_file_cache off; 默认值为:open_log_file_cache off; 适用配置段包括:http, server, location
每条日志记录的生成过程通常是先打开文件,写入内容后再关闭。可通过open_log_file_cache指令启用日志文件缓存功能(默认关闭),其配置格式如下:
各参数含义如下: max:设定缓存中保留的最大文件描述符数量。若缓存已满,将采用LRU算法关闭描述符。 inactive:设定存活时间,默认为10秒。 min_uses:设定在inactive时间段内,日志文件被最少访问多少次后才计入缓存,默认为1次。 valid:设定检查频率,默认为60秒。 off:禁用缓存功能。 实例配置如下: 复制代码 代码如下:open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
4. log_not_found指令
语法结构为:log_not_found on | off; 默认值为:log_not_found on; 适用配置段包括:http, server, location 该指令决定是否在error_log中记录文件不存在的错误,默认开启。
5. log_subrequest指令
语法结构为:log_subrequest on | off; 默认值为:log_subrequest off; 适用配置段包括:http, server, location 该指令决定是否在access_log中记录子请求的访问日志,默认不记录。
6. rewrite_log指令
此指令由ngx_http_rewrite_module模块提供,用于记录重写日志。建议在调试重写规则时开启。参考Nginx重写规则指南。 语法结构为:rewrite_log on | off; 默认值为:rewrite_log off; 适用配置段包括:http, server, location, if 启用后,将在error log中以notice级别记录重写日志。
7. error_log指令
语法结构为:error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg]; 默认值为:error_log logs/error.log error; 适用配置段包括:main, http, server, location 该指令用于配置错误日志的输出路径及级别。