- Nginx error_log配置
nginx的error_log类型如下(从左到右:debug最详细 crit最少):
[ debug | info | notice | warn | error | crit ]
注意:当访问量较大时,不要使用debug,消耗磁盘IO
例如:error_log logs/nginx_error.log crit;
解释:日志文件存储在nginx安装目录下的 logs/nginx_error.log ,错误类型为 crit ,也就是记录最少错误信息;
默认值: error_log logs/error.log error;
配置段: main, http, server, location
参考资料:http://nginx.org/en/docs/ngx_core_module.html#error_log
2.Nginx访问日志
语法: 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 gzip buffer=32k flush=5
默认值: 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;
- 日志格式允许包含的变量注释如下:
- $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)发送,pipe值为“p”,否则为“.”。
- $http_referer 记录从哪个页面链接访问过来的
- $http_user_agent 记录客户端浏览器相关信息
- $request_length 请求的长度(包括请求行,请求头和请求正文)。
- $request_time 请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个字节开始,直到把最后一个字符发送给客户端后进行日志写入为止。
- $time_iso8601 ISO8601标准格式下的本地时间。
- $time_local 通用日志格式下的本地时间。
官方文档:http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log
3.nginx自动切割访问日志
自动切割访问日志脚本
#!/bin/bash
Dataformat=`date +%F -d -1day`
Basedir=”/application/nginx”
Nginxlogdir=”$Basedir/logs”
Logname=”access”
[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dataformat}_${Logname}.log
$Basedir/sbin/nginx -s reload
- 添加定时任务实现每天定时切割
00 00 * * * /bin/sh /server/scripts/cut_log.sh &>/dev/null
也可以在脚本中添加多个mv命令对每个虚拟主机进行切割
运维人员必须熟悉的运维工具汇总: http://oldboy.blog.51cto.com/2561410/775056