Nginx 访问日志轮询切割

自己动手写Nginx 访问日志轮询切割,先说说思路,通过每天把访问日志文件重命名,并nginx 服务reload一下:如下图

Nginx 访问日志轮询切割_第1张图片

1、选看看虚拟主机的server标签内容

[root@web extra]# vim /application/nginx/conf/extra/www.conf
 server {
        listen       80;
        server_name  www.judong.org judong.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        access_log logs/access_www.log main;
  }


[root@web extra]# vim /application/nginx/conf/extra/bbs.conf
    server {
        listen       80;
        server_name  bbs.judong.org;
        location / {
            root   html/bbs;
            index  index.html index.htm;
        }
       access_log logs/access_bbs.log main;  ##添加访问日志
  }


[root@web extra]# vim /application/nginx/conf/extra/blog.conf  
server {
        listen       80;
        server_name  blog.judong.org;
        location / {
            root   html/blog;
            index  index.html index.htm;
        }
       access_log logs/access_blog.log main;
  }


轮询切割脚本cut_nginx_log.sh:

#!/bin/sh
Dateformat=`date +%Y%m%d`
Basedir="/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1
[ -f ${Logname}.log ]||exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
/bin/mv access_bbs.log ${Dateformat}_access_bbs.log
/bin/mv access_blog.log ${Dateformat}_access_blog.log
$Basedir/sbin/nginx -s reload



添加定时任务,每天0点进行切割:

[root@web scripts]# crontab -l
######cut nginx access.log########
00 00 * * *  /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1