zoukankan      html  css  js  c++  java
  • logrotate切割nginx日志

    1 配置

    • 使用系统自带的logrorate来切个nginx日志,位于/usr/sbin/logrotate
    • 假设服务器上有两个网站的nginx配置分别如下:

      • 去除其它配置信息,只保留了日志相关
      • A网站
        ...
        access_log  /data/logs/a.com/access.log;
        error_log   /data/logs/a.com/error.log;
        ...
      • B网站
        ...
        access_log  /data/logs/b.com/access.log;
        error_log   /data/logs/b.com/error.log;
        ...
    • 在/etc/logrotate.d/下创建一个配置文件 nginx, 内容如下:
      # 这里可以添加你想切个的目录,也可以直接使用正则表达式
      
      /data/logs/a.com/*.log
      /data/logs/b.com/*.log
      {
          daily
          rotate 30
          missingok
          dateext
          compress
          delaycompress
          notifempty
          sharedscripts
          postrotate
              if [ -f /usr/local/nginx/nginx.pid ]; then
                  kill -USR1 `cat /usr/local/nginx/nginx.pid`
              fi
          endscript
      }
    • 配置说明:
    daily 指定转储周期为每天
    weekly 指定转储周期为每周
    monthly 指定转储周期为每月
    rotate 转储次数,超过将会删除最老的那一个
    missingok 忽略错误,如“日志文件无法找到”的错误提示
    dateext 切换后的日志文件会附加上一个短横线和YYYYMMDD格式的日期
    compress 通过gzip 压缩转储旧的日志
    delaycompress 当前转储的日志文件到下一次转储时才压缩
    notifempty 如果日志文件为空,不执行切割
    sharedscripts 只为整个日志组运行一次的脚本
    prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
    postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行

    2、 测试

    • 执行以下命令进行测试
      logrotate -vf /etc/logrotate.d/nginx 
      • 然后到相应的日志目录下查看 (/data/logs/a.com/, /data/logs/b.com/)
      • 应该会有类似以下的文件: 
        • access.log
        • access.log-20170626
        • error.log
        • error.log-20170626

    3、 添加定时任务

    每日0点执行脚本

    • 在终端运行 crontab -e
    • 插入以下语句
    0 0 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx
  • 相关阅读:
    小小杨的影视空间
    关于励志的事情
    关于2020年的总结
    关于心情不好的时候
    关于我的2020年
    单链表基本操作的实现
    原型模式
    android—安卓系统文件目录结构
    android——apk安装文件的组成结构
    android——项目的组成结构
  • 原文地址:https://www.cnblogs.com/root0/p/9390412.html
Copyright © 2011-2022 走看看