Nginx 访问日志轮询切割

Nginx 访问日志轮询切割脚本

 

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

 

 

 

配置www.conf

 

 1 [root@lnmp02 scripts]# vim /application/nginx/conf/extra/www.conf 
 2  server {
 3         listen       80;
 4         server_name  www.judong.org judong.org;
 5         location / {
 6             root   html/www;
 7             index  index.html index.htm;
 8         }
 9         access_log logs/access_www.log main;
10   }

 

配置bbs.conf

 

 1 [root@lnmp02 scripts]# vim /application/nginx/conf/extra/bbs.conf 
 2     server {
 3         listen       80;
 4         server_name  bbs.judong.org;
 5         location / {
 6             root   html/bbs;
 7             index  index.html index.htm;
 8         }
 9        access_log logs/access_bbs.log main;  ##添加访问日志
10   }
11 ~         

 

配置blog.conf

 1 [root@lnmp02 scripts]# vim /application/nginx/conf/extra/blog.conf   
 2 server {
 3         listen       80;
 4         server_name  blog.judong.org;
 5         location / {
 6             root   html/blog;
 7             index  index.html index.htm;
 8         }
 9  access_log logs/access_blog.log main;
10   }

测试

 

 1 [root@lnmp02 scripts]# ll /application/nginx/logs/
 2 total 40
 3 -rw-r--r--. 1 root root   756 Mar  3 22:07 20160315_access_www.log 
 4 -rw-r--r--. 1 root root     0 Mar 15 09:27 20160316_access_www.log
 5 -rw-r--r--. 1 root root     0 Mar 17  2016 20160317_access_bbs.log
 6 -rw-r--r--. 1 root root     0 Mar 17  2016 20160317_access_blog.log
 7 -rw-r--r--. 1 root root     0 Mar 17  2016 20160317_access_www.log
 8 -rw-r--r--. 1 root root     0 Mar 17  2016 access_bbs.log
 9 -rw-r--r--. 1 root root     0 Mar 17  2016 access_blog.log
10 -rw-r--r--. 1 root root 22177 Mar  3 22:06 access.log
11 -rw-r--r--. 1 root root     0 Mar 17  2016 access_www.log
12 -rw-r--r--. 1 root root  2088 Mar 17  2016 error.log
13 -rw-r--r--. 1 root root     5 Mar  3 19:31 nginx.pid

 

配置定时任务使得每天0点整执行脚本

 

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

 

posted @ 2016-03-15 09:57  Simon92  阅读(1381)  评论(1编辑  收藏  举报