优选主流主机商
任何主机均需规范使用

Linux环境下Nginx日志自动切割与管理的最佳实践

Linux下nginx生成日志自动切割的实现方法

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

?

1 #vi /data/nginx/cut_nginx_log.sh

输入代码:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #!/bin/bash               # This script run at 00:00               function cutAccess() {    dir =$1    newdir= "${dir}/$(date -d " yesterday " +" %Y ")/$(date -d " yesterday " +" %m ")"    suffix=$( date -d "yesterday" + "%Y%m%d" )    mkdir -p $newdir    mv ${ dir } /access .log ${newdir} /access .$suffix.log }               cutAccess "/home/wwwlogs/www.yourdomain.com/" cutAccess "/home/wwwlogs/www.yourdomain-1.com/" cutAccess "/home/wwwlogs/www.yourdomain-2.com/"             # 重启 nginx kill -HUP ` cat /usr/local/nginx/logs/nginx .pid`

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

?

1 2 # crontab -e 0 0 * * * /bin/bash /data/nginx/cut_nginx_log .sh

3、nginx 日志格式

?

1 2 3 4 log_format access '$remote_addr - $remote_user [$time_local] "$request" '        '$status $body_bytes_sent "$http_referer" '        '"$http_user_agent" $http_x_forwarded_for' ; access_log /home/wwwlogs/www .yourdomain.com /access .log access;

 

未经允许不得转载:搬瓦工中文网 » Linux环境下Nginx日志自动切割与管理的最佳实践