1
设置日志格式及路径:
全局格式设置 :nginx.conf
log_format wwwlogs '$remote_addr - $remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for';
access_log /www/web_logs/access.log wwwlogs;
error_log /www/web_logs/error.log notice;
2
新建文件 /cron/nginx_spilit.sh
#!/bin/bash
log_files_path="/www/web_logs/"
log_files_dir=${log_files_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
log_files_name=(access access error error)
nginx_sbin="$/usr.local/nginx/sbin/nginx "
save_days=60
mkdir -p $log_files_dir
log_files_num=${#log_files_name[@]}
for((i=0;i<$log_files_num;i++));do
mv ${log_files_path}${log_files_name[i]}.log ${log_files_dir}/${log_files_name[i]}_$(date -d "yesterday" +"%Y%m%d").log
done
find $log_files_path -mtime +$save_days -exec rm -rf {} \;
$nginx_sbin -s reload
3
crontab -e
00 00 * * * /bin/bash /cron/nginx_spilit.sh