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
  • 插入以下语句
  * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx

最新文章

  1. iOS-----程序异常处理----- 断言NSAssert()和NSParameterAssert区别和用处
  2. iOS-UI-UI控件概述
  3. Vs2013中通过Nuget添加不同版本jQuery
  4. FTPS链接服务器
  5. (gridcontrol等)通用导出excel z
  6. 获取byte数组的实际使用长度
  7. 25 个超棒的 HTML5 & JavaScript 游戏引擎开发库
  8. 浅谈JavaScript中的apply,call和bind
  9. thinkphp实现自动登录
  10. Unable to chmod /system/build.prop.: Read-only file system
  11. Go语言打造以太坊智能合约测试框架(level3)
  12. C# IQueryable和IEnumerable的区别
  13. Django框架详细介绍---请求流程
  14. nDPI-dev分析
  15. Confluence 6 PostgreSQL 测试你的数据库连接
  16. java.lang.UnsatisfiedLinkError:no dll in java.library.path
  17. lnmp+zabbix 3.2 的编译安装
  18. webdriver+expected_conditions二次封装
  19. BZOJ 1022 [SHOI2008]小约翰的游戏John AntiNim游戏
  20. 本地blast的安装

热门文章

  1. OpenGL(三) RGBA颜色设置
  2. poj 2763 Housewife Wind(树链拆分)
  3. eclipse 配置maven tomcat 环境
  4. WPF特效-拼图游戏
  5. 线程安全ConcurrentBag
  6. Bootstrap 添加到项目
  7. Telnet协议详解(远程登陆协议)
  8. js错误界面
  9. glibc_error reporting
  10. Win8Metro(C#)数字图像处理--2.6图像对比度调整