CentOS服务器里nginx生成日志自动切割

浏览次数:2639 关键词 ( 器里  日志  CentOS  nginx  )

1、编辑切割日志的 shell 程序,目录自定


[plain]view plaincopy在CODE上查看代码片派生到我的代码片
  1. #vi /data/nginx/cut_nginx_log.sh  

输入代码:


[python]view plaincopy在CODE上查看代码片派生到我的代码片
  1. #!/bin/bash

  2. # This script run at 00:00

  3. function cutAccess()  

  4. {  

  5.    dir=$1

  6.    newdir="${dir}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")"

  7.    suffix=$(date -d "yesterday" +"%Y%m%d")  

  8.    mkdir -p $newdir  

  9.    mv ${dir}/access.log ${newdir}/access.$suffix.log  

  10. }  

  11. cutAccess "/home/wwwlogs/www.yourdomain.com/"

  12. cutAccess "/home/wwwlogs/www.yourdomain-1.com/"

  13. cutAccess "/home/wwwlogs/www.yourdomain-2.com/"

  14. # 重启 nginx

  15. kill -HUP `cat /usr/local/nginx/logs/nginx.pid`  



2、加入定时任务,每天0点自动切割


[plain]view plaincopy在CODE上查看代码片派生到我的代码片
  1. # crontab -e  

  2. 0 0 * * * /bin/bash /data/nginx/cut_nginx_log.sh  


3、nginx 日志格式



[plain]view plaincopy在CODE上查看代码片派生到我的代码片
  1. log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '  

  2.            '$status $body_bytes_sent "$http_referer" '  

  3.            '"$http_user_agent" $http_x_forwarded_for';  

  4. access_log  /home/wwwlogs/www.yourdomain.com/access.log  access;