您的位置:首页 > 运维架构 > Nginx

nginx日志

2013-09-14 14:53 621 查看
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 cachingUsage 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;
}
}


注: 使用以上配置,各站点日志的目录不需要有worker进程用户的创建文件的权限,但是如果使用logs/$server_name.access.log这种配置,则需要创建文件权限。但是使用这种配置不能使用buffer,降低服务器性能。
logs/$server_name.access.log使用该配置时,如果server_name配置了多个参数,取第一个。

==================================================================

日志切割脚本:
详见:/article/4287454.html

本文出自 “notepad” 博客,请务必保留此出处http://sndapk.blog.51cto.com/5385144/1297051
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: