log_format
context: http,server
syntax: log_format name 'format string ...';
注:format string可以使用一般变量,也可以使用写入日志那些刻的变量(过程变量)
$body_bytes_sent $bytes_sent $connection $msec $pipe $request_length
$request_time $status $time_iso8601 $time_local
其它一些变量
$sent_http_content_range upstream_http_...
access_log
context: http,server,location
syntax: acces_log path [format [buffer=size [flush=time]]] | off
注:
如果不指定format默认使用combined格式;
buffer
默认关闭,如果path中包含变量,则不能使用buffer参数;
使用buffer,必须指定日志format;
使用flush必须使用buffer,作用:在缓存没有填满的情况下,超过flush指定的时间将强制写入到日志文件。
如果设置为off,将关闭继承的access_log参数;
path
0.7.4版本以上,path可以包含变量,但是这样的日志有一定的限制,如下:
worker用户必须有新建文件的权限;
不能使用缓存
open_log_file_cache
功能:缓存频繁使用的日志文件描述符
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
max
- sets the maximum number of descriptors in a cache; if the cache becomes full the least recently used (LRU) descriptors are closed
inactive
- sets the time after which the cached descriptor is closed if there were no access during this time; by default, 10 seconds
min_uses
- sets the minimum number of file uses during the time defined by the
inactive
parameter to let the descriptor stay open in a cache; by default, 1
valid
- sets the time after which it should be checked that the file still exists with the same name; by default, 60 seconds
off
- disables caching
Usage example:
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
总结:
经过测试:访问约5到6次会产生1k的日志文件,如果buffer设置32k,也就是说要访问32*6=192/次才能填满缓存,这个根据实际需要调整配合flush参数使用。
实际生产中,如果需要记录日志,可以使用以下格式:
http { open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2; server { server_name test.cn; access_log /var/log/nginx/test.cn/access.log combined buffer=32k flush=1m; } }
logs/$server_name.access.log使用该配置时,如果server_name配置了多个参数,取第一个。
日志切割脚本:
详见:http://sndapk.blog.51cto.com/5385144/1297120转载于:https://blog.51cto.com/sndapk/1297051