background
nginx log file does not rotate function. Over time, the log will increasingly bloated, a accesslog soon break through 2.5G, it is necessary to realize a script by day cut logs.
1, the backup log files under / usr / local / nginx / logs / historyLog directory, file name of the backup is access_yyyyMMdd.log and error_yyyyMMdd.log.
1. Create nginx_logs.sh files, and grant execute permissions.
chmod +x nginx_logs.sh
2. backup script
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/nginx/logs/historyLog/"
logPath="/usr/local/nginx/logs/"
#创建备份目录 如2020/03
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
#将日志移动到上面的目录中
mv ${logPath}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
mv ${logPath}error.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/error_$(date -d "yesterday" +"%Y%m%d").log
#主要用到nginx的-usr1信号,usr1 : reopen the log files 向nginx主进程发送信号以重新打开日志
kill -USR1 `cat ${logs_path}nginx.pid`
3. Create a directory under the log backups, switch to / usr / local / nginx / logs
mkdir historyLog
2, by setting the crontab command, time-moon and second circumferential
1, set the timer task "crontab -e"
#将于每天凌晨0点0分将nginx日志重命名为昨天的日期格式,并重新生成今天的新日志
0 0 * * * bash /usr/local/nginx/logs/nginx_logs.sh
2. First performed manually nginx_logs.sh