跳转至

3. nginx的日志切割

nginx的日志切割

思路

如果使用默认日志配置,经过一段时间运行后,access.log和error.log文件会变得非常大,使维护和排查问题变得不便,所以非常有必要做日志切割。

通常的思路是:使用nginx的-s reopen命令,结合linux系统的crontab定时任务命令,弄一个定时任务按时切割日志文件。

#!/bin/bash
#Rotate the nginx logs to prevent a single logfile from consuming too much disk space.
LOGS_PATH=/usr/local/nginx/logs/history
CUR_LOGS_PATH=/usr/local/nginx/logs
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
mv ${CUR_LOGS_PATH}/access.log ${LOGS_PATH}/access_${YESTERDAY}.log
mv ${CUR_LOGS_PATH}/error.log ${LOGS_PATH}/error_${YESTERDAY}.log
#向nginx master进程发送USR1信号,USR1信号是重新打开日志文件,相当于-s reopen命令
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)