使用logrotate定期切割nginx日志
2021/6/23 7:27:06
本文主要是介绍使用logrotate定期切割nginx日志,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
nginx环境
- nginx安装位置:/usr/local/nginx
按天切割nginx日志的配置
vim /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log { daily size 10m minsize 10m rotate 30 missingok notifempty compress nodelaycompress copytruncate dateext dateformat -%Y-%m-%d dateyesterday postrotate if [ -f /usr/local/nginx/logs/nginx.pid ];then kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` fi endscript }
配置说明:
- 按天切割
- 文件大小为10M的时候才切割
- 保留最近30天的日志文件
- 切割中遇到日志错误忽略
- 日志如果为空将不进行切割和压缩
- 以gzip压缩
- 不要将刚切割后的日志文件放到下个循环中进行压缩
- 切割后的日志文件添加扩展名
- 扩展名为年月日
- 扩展名的年月日为昨天的日期
- 在切割后执行 postrotate/endscript之间的命令,此处为热重启nginx
使用crontab定时执行logrotate
因为系统默认的logrotate规则比较奇葩,所以直接删了默认规则,自行定义。
rm -rf /etc/cron.daily/logrotate
- 使用
crontab -e
新建crontab规则:00 00 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx
,这样每天凌晨0点0分就会执行nginx日志切割。注意确认logrotate命令的位置。
这篇关于使用logrotate定期切割nginx日志的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-29fail2ban nginx
- 2024-03-18install nginx proxy manager
- 2024-01-2444从零开始用Rust编写nginx,命令行参数的设计与解析及说明
- 2024-01-22比Nginx更好用的Gateway!
- 2024-01-17nginx daemon off
- 2023-12-30Nginx Vhost Traffic Status 是什么意思-icode9专业技术文章分享
- 2023-12-26使用nginx搭建creates.io镜像(稀疏索引)
- 2023-11-18centos7编译安装Nginx教程。
- 2023-11-02nginx启动命令和停止命令
- 2023-10-25Centos7系统Nginx负载均衡如何安装和配置?