第3章:日志管理

1、错误日志配置

错误日志属于核心功能模块的参数

worker_processes  1;
error_log  /data/logs/nginx/error.log  error;    #一般配置这一行即可
events {
    worker_connections  1024;
}

语法规则:error_log   file   level

错误的日志级别有[debug|info|notice|warn|error|crit|alert|emerg],级别越高,记录的信息越少,生产场景一般是warn|error|crit这三个级别之一

可以放置的标签段为:main,http,server,location

2、访问日志配置

①定义日志格式(放置的http标签内)

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

在没有特殊要求的情况下,采用默认配置即可

②在每个虚拟主机配置应用

[root@c1 ~]# cat /application/nginx/conf/vhost/www.heboan.com.conf 
server {
     listen       80;
     server_name  www.heboan.com;
     
     location / {
        root   /data/www/heboan;
        index  index.html index.htm;
     }
    
    access_log  /data/logs/nginx/www.heboan.com.log; #配置这一行即可
}


#没有什么需求,建议在生产环境关闭访问日志
access_log  off

3、访问日志的轮询切割

①创建轮询切割脚本

#vim /data/shell/nginx_cut_log.sh

#!/bin/bash
#
LOGDIR=/data/logs/nginx
LOGBKDIR=$LOGDIR/`date +%Y-%m`
NGINX_SBIN=/application/nginx/sbin/nginx

logrotate () {
    local I
    for I in `ls $LOGDIR`;do
	    if [ -f $LOGDIR/$I ];then
		    log_bkname=$LOGBKDIR/${I}_`date +%d`.gz
            log_file=$LOGDIR/$I
            cat $log_file|gzip >$log_bkname
            rm -f $I
	    fi
    done
    ${NGINX_SBIN} -s reload
}

delempdir () {
    local I
    for I in $*;do
        IFEMPTY=`ls $I`
        [ "$IFEMPTY" == "" ] && rmdir $I
	done
}

#do the log rotating
[ ! -d $LOGBKDIR ] && mkdir -p $LOGBKDIR
logrotate

#delete the outdated bakcup log files
find $LOGDIR -name "*log*" -mtime +7  -exec rm -rf {} \;

#delete the empty directory under $LOGDIR
ALLBAKLOCATION=`find $LOGDIR -type d`
delempdir $ALLBAKLOCATION

②通过定时任务每天00.01点准时执行/data/shell/nginx_cut_log.sh

[root@c1 ~]# crontab -l
#cut nginx access log by heboan
01 00 * * * /usr/bin/bash /data/shell/nginx_cut_log.sh

  

 

posted @ 2016-04-10 22:02  sellsa  阅读(191)  评论(0)    收藏  举报