Nginx 使用 logrotate 滚动日志
时间:2022-11-28 14:21:56.053 +0800 CST 浏览:386

安装logrotate

一般系统已经安装了logrotate, 如果没有安装使用一下命令安装

yum install logrotate

入口文件

/etc/cron.daily/logrotate 使用crontab每天执行一次

配置文件

主配置文件/etc/logrotate.conf, 会include/etc/logrotate.d/下的配置,建议一个日志单独在/etc/logrotate.d/创建一个配置。

配置nginx日志滚动

cat  /etc/logrotate.d/openresty
/var/log/openresty/*.log {     # 可以指定多个路径
    daily                      # 日志轮询周期,weekly,monthly,yearly
    rotate 30                  # 保存30天数据,超过的则删除
    size 100M                  # 超过100M时分割,单位K,M,G,优先级高于daily
    dateext
    dateformat -%Y-%m-%d       # 切割后文件格式:igame_qq_com_access.3.log-2019-03-13.gz
    compress                   # 切割后压缩,也可以为nocompress
    delaycompress              # 切割时对上次的日志文件进行压缩
    dateext                    # 日志文件切割时添加日期后缀
    missingok                  # 如果没有日志文件也不报错
    notifempty                 # 日志为空时不进行切换,默认为ifempty
    create 640 nginx nginx     # 使用该模式创建日志文件
    sharedscripts              # 所有的文件切割之后只执行一次下面脚本
    postrotate
        [! -f /usr/local/openresty/nginx/logs/nginx.pid ] || kill -USR1 `cat /usr/local/openresty/nginx/logs/nginx.pid`
    endscript
}


如果这篇文章对你有所帮助,可以通过下边的“打赏”功能进行小额的打赏。

本网站部分内容来源于互联网,如有侵犯版权请来信告知,我们将立即处理。


来说两句吧