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
Tags标签: 日志,切割 Tags相关文章如下:

上一篇:Redis客户端输出缓冲区限制调整

下一篇:2018全球互联网架构大会上海站略影