Nginx日志访问量大的时候,日志文件增长很快,查询日志很不方便,以下为实现按天或者小时将access文件切割:
按天切割方法如下:
创建log.sh脚本:

vim /usr/local/nginx/logs/log.sh
#!/bin/bash
LOG_PATH=/usr/local/nginx/logs/
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
mv ${LOG_PATH}access.log ${LOG_PATH}access-${YESTERDAY}.log
mv ${LOG_PATH}error.log ${LOG_PATH}error-${YESTERDAY}.log
/usr/local/nginx/sbin/nginx -s reload

给权限

chmod +x /usr/local/nginx/logs/log.sh

crontal -e 加入定时执行,每天凌晨的时候,对日志进行切割,并reload

00 00 * * * /usr/local/nginx/logs/log.sh

按小时切割:
创建log.sh脚本:

vim /usr/local/nginx/logs/log.sh
#!/bin/bash
LOG_PATH=/usr/local/nginx/logs/
YESTERDAY=$(date -d "-1hours" +%Y-%m-%d_%H)
mv ${LOG_PATH}access.log ${LOG_PATH}access-${YESTERDAY}.log
mv ${LOG_PATH}error.log ${LOG_PATH}error-${YESTERDAY}.log
/usr/local/nginx/sbin/nginx -s reload

给权限

chmod +x /usr/local/nginx/logs/log.sh

crontal -e 加入定时执行,每个整点的时候,对日志进行切割,并reload

*/60  * * * * /usr/local/nginx/logs/log.sh

扫描下面二维码,给我点动力吧~

微信

微信

支付宝

支付宝

带符号 * 的表示必填项