ookkl 发表于 2017-2-20 09:58:26

Nginx 访问日志轮询切割

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

1、选看看虚拟主机的server标签内容
# vim /application/nginx/conf/extra/www.conf
server {
      listen       80;
      server_namewww.judong.org judong.org;
      location / {
            root   html/www;
            indexindex.html index.htm;
      }
       access_log logs/access_www.log main;
}

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

# vim /application/nginx/conf/extra/blog.conf   
server {
      listen       80;
      server_nameblog.judong.org;
      location / {
            root   html/blog;
            indexindex.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点进行切割:
# crontab -l
######cut nginx access.log########
00 00 * * */bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1





页: [1]
查看完整版本: Nginx 访问日志轮询切割