logrotate切割nginx日志

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 链接配置测试添加定时任务1 链接个人博客: alex-my.xyzCSDN: blog.csdn.net/alex_my2 配置使用系统自带的logrorate来切个nginx日志,位于/usr/sbin/logrotate假设服务器上有两个网站的nginx配置分别如下:去除其它配置信息,只保留了日志相关A网站


1 链接

个人博客: alex-my.xyz

CSDN: blog.csdn.net/alex_my

2 配置

  • 使用系统自带的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
    }
    • 需要注意的是你们的nginx.pid位置,不一定是在/usr/local/nginx/nginx.pid
  • 配置说明

    配置 说明
    daily 指定转储周期为每天
    weekly 指定转储周期为每周
    monthly 指定转储周期为每月
    rotate 转储次数,超过将会删除最老的那一个
    missingok 忽略错误,如“日志文件无法找到”的错误提示
    dateext 切换后的日志文件会附加上一个短横线和YYYYMMDD格式的日期
    compress 通过gzip 压缩转储旧的日志
    delaycompress 当前转储的日志文件到下一次转储时才压缩
    notifempty 如果日志文件为空,不执行切割
    sharedscripts 只为整个日志组运行一次的脚本
    prerotate/endscript 在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
    postrotate/endscript 在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行

3 测试

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

4 添加定时任务

  • 每日0点执行脚本

    • 在终端运行 crontab -e
    • 插入以下语句
    0 0 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
24天前
|
应用服务中间件 nginx
nginx error日志 client intended to send too large body: 1434541 bytes 如何处理?
【8月更文挑战第27天】nginx error日志 client intended to send too large body: 1434541 bytes 如何处理?
29 6
|
29天前
|
应用服务中间件 Linux nginx
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
|
1月前
|
存储 监控 应用服务中间件
查看nginx日志文件
器性能和提高网站可用性。掌握日志文件的路径、查看方法和基本分析技能对于任何服务器管理员来说都是必备技能。
48 1
|
1月前
|
存储 Ubuntu 应用服务中间件
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
如何在 Ubuntu VPS 上配置 Nginx 的日志记录和日志轮转
17 4
|
1月前
|
监控 关系型数据库 Linux
Linux日志管理工具:Logrotate(二)
Linux日志管理工具:Logrotate(二)
51 2
|
1月前
|
应用服务中间件 Linux nginx
Nginx log 日志文件较大,按日期生成 实现日志的切割
Nginx log 日志文件较大,按日期生成 实现日志的切割
206 0
|
1月前
|
存储 监控 安全
Linux日志管理工具:Logrotate(一)
Linux日志管理工具:Logrotate(一)
96 0
|
1月前
|
应用服务中间件 nginx
[nginx]日志中记录自定义请求头
[nginx]日志中记录自定义请求头
|
1月前
|
应用服务中间件 Shell nginx
shell分析nginx日志的一些指令
shell分析nginx日志的一些指令
|
Linux 应用服务中间件 nginx