首页 > 代码库 > Nginx 访问日志轮询切割

Nginx 访问日志轮询切割

默认情况下 Nginx 会把所有的访问日志生成到一个指定的访问日志文件 access.log 里,但这样一来,时间长了就会导致日志个头很大,不利于日志的分析和处理,因此,有必要对 Nginx 日志,按天或按小时进行切割,使其分成不同的文件保存。

[root@localhost conf]# cat cut_nginx_log.sh #!/bin/bashDateformat=`date +%Y%m%d`Basedir="/usr/local/nginx"Nginxlogdir="$Basedir/logs"Logname="access"[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1[ -f ${Logname}.log ] || exit 1/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log$Basedir/sbin/nginx -s reload
[root@localhost nginx]# crontab -l0 0 * * * /bin/bash /usr/local/nginx/conf/cut_nginx_log.sh > /dev/null 2>&1

 

效果:

[root@localhost logs]# ll-rw-r--r--. 1 root root    0 May 25 18:57 20170521_access.log-rw-r--r--. 1 root root    0 May 25 18:57 20170522_access.log-rw-r--r--. 1 root root    0 May 25 18:57 20170523_access.log-rw-r--r--. 1 root root    0 May 25 18:57 20170524_access.log-rw-r--r--. 1 root root    0 May 25 18:57 20170525_access.log-rw-r--r--. 1 root root    0 May 25 18:57 access.log-rw-r--r--. 1 root root 6083 May 25 18:57 error.log-rw-r--r--. 1 root root    5 May 25 13:12 nginx.pid

 

 

 

 

    

 

Nginx 访问日志轮询切割