nginx 的日志切割
nginx的日志切割脚本
说明:在nginx的配置文件中nginx.conf并没有定义access_log的位置,
在/usr/local/nginx/conf/vhost/下的配置文件中定义了access_log的位置。
该位置在/home/wwwlogs/的目录下,
本例中是在/usr/local/nginx/conf/vhost/下有90005.com.conf和www-xam.com两个配置文件,
同样的access_log定义了
access_log /home/wwwlogs/90005.com_access.log;
access_log /home/wwwlogs/www-xam.com_access.log;
vi nginx.cut.sh
#!/bin/bash
#function:cut nginx log files for nginx
#set the path to nginx log files
log_files_path="/home/wwwlogs/"
#log_files_dir=${log_files_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
log_files_dir=$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")
#set nginx log files you want to cut
log_files_name=( 90005.com_access www-xam.com_access)
#set the path to nginx.
nginx_sbin="/usr/local/nginx/sbin/nginx"
#Set how long you want to save
save_days=10
cd $log_files_path
mkdir -p $log_files_dir
log_files_num=${#log_files_name[@]}
#cut nginx log files
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
tar -zcvf ${log_files_dir}/${log_files_name[i]}_$(date -d "yesterday" +"%Y%m%d").tar.gz ${log_files_name[i]}.log
#delete source
rm -rf ${log_files_name[i]}.log
done
#delete 10 days ago nginx log files
find $log_files_path -mtime +$save_days -exec rm -rf {} \;
$nginx_sbin -s reload
在crontab中同步脚本文件
* 0 * * * sh /home/shell/cut_nginx_logs.sh >> /dev/null 2>&1