Nginx 访问日志轮询切割

时间:2024-01-15 23:17:02

Nginx 访问日志轮询切割脚本

 #!/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

配置www.conf

 [root@lnmp02 scripts]# 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;
}

配置bbs.conf

 [root@lnmp02 scripts]# 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; ##添加访问日志
}
~

配置blog.conf

 [root@lnmp02 scripts]# 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;
}

测试

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

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

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