当前位置:首页 > Nginx
使用 Linux 系统自带的日志切割工具对 Nginx 进行日志切割
来源:靑龍一笑的博客  作者:靑龍一笑  发布时间:2015-11-14 12:52:01  点击量:761  评论:0

    logrotate 是 linux 系统自带的日志文件管理工具,我们需要做的就是在 /etc/logrotate.d/ 目录下建立 Nginx 日志切割的配置文件。
    首先,确认一下 Nginx 日志文件所在的路径:

[root@RicenOS logs]# pwd
/opt/nginx/logs

    接着,在 /etc/logrotate.d 目录下建立配置文件:

[root@RicenOS logs]# vi /etc/logrotate.d/nginx

    内容如下:

/opt/nginx/logs/*.log {
    daily
    rotate 180
    missingok
    notifempty
    dateext
    compress
    olddir /opt/nginx/logs/history
    sharedscripts
    postrotate
      if [ -f /opt/nginx/logs/nginx.pid ];then
        kill -USR1 `cat /opt/nginx/logs/nginx.pid`
      fi
    endscript
}

    测试:

[root@RicenOS logs]# logrotate -f /etc/logrotate.d/nginx

    接下来要创建一个计划任务,让它每天凌晨 0 点执行。
    创建一个 crontab:

[root@RicenOS logs]# crontab -e

    内容如下:

0 0 * * * /sbin/logrotate -f /etc/logrotate.d/nginx
版权所有 © 2005-2023 靑龍一笑的博客  Powered by C.S.Ricen
Copyright © 2005-2023 by www.ricensoftwares.com.cn  All Rights Reserved.

欢迎光临本站,这里是靑龍一笑的博客。

因资金匮乏,本站已迁到国外的免费空间,可能导致本站的访问速度较慢,由此给您带来的不便,敬请谅解。

您可以通过下方的“支持本站建设”链接,给本站提供资金支持。

Free Web Hosting