Nginx 訪問日誌輪詢切割

Nginx 訪問日誌輪詢切割

自己動手寫Nginx 訪問日誌輪詢切割,先說說思路,通過每天把訪問日誌文件重命名,並nginx 服務reload一下:如下圖

wKioL1ioR73R0DpmAABXOnbwh84327.png

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





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章