nginx日志轮循小记

目录

1. 概述

Nginx 是一个非常轻量的 Web 服务器,体积小、性能高、速度快等诸多优点。但不足的是也存在缺点,比如其产生的访问日志文件一直就是一个,如果是yum方式安装的nginx,系统默认会自动通过logrotate这个日志管理软件,按天进行分割。而如果是源码编译方式安装nginx其日志不会自动地进行切割,如果访问量很大的话,将导致日志文件容量非常大,不便于管理。当然了,我们也不希望看到这么庞大的一个访问日志文件,那需要手动对这个文件进行切割。

2. 切割方式介绍

  • 使用logrotate工具+系统自带的crontab命令
    logrotate是什麽呢?它是一个linux系统日志的管理工具。它可以切割、压缩等其他软件的日志文件软件。
  • 编写Shell 脚本+系统自带的crontab 命令

3. 使用logrotate工具+系统自带的crontab命令

3.1 安装logrotate工具
[yuki@myhost ~]$ sudo yum install -y logrotate

#查看是否安装成功
[yuki@myhost ~]$ sudo rpm -qa logrotate
logrotate-3.8.6-17.el7.x86_64
3.2 查看logrotate的配置文件
[yuki@myhost ~]$ sudo rpm -ql logrotate
/etc/cron.daily/logrotate
/etc/logrotate.conf                       		#####
/etc/logrotate.d							 	#####
/etc/rwtab.d/logrotate
/usr/sbin/logrotate
/usr/share/doc/logrotate-3.8.6
/usr/share/doc/logrotate-3.8.6/CHANGES
/usr/share/doc/logrotate-3.8.6/COPYING
/usr/share/man/man5/logrotate.conf.5.gz
/usr/share/man/man8/logrotate.8.gz
/var/lib/logrotate
/var/lib/logrotate/logrotate.status

#logrotate的配置文件是/etc/logrotate.conf
#而/etc/logrotate.d/是用于存储其他配置文件的目录。该目录里的所有文件都会被主动的读入 /etc/logrotate.conf中执行。

3.3 logrotate配置文件详解

logrotate配置选项相对来说比较少,为了切合本篇文章,在此我们以切割nginx的配置文件为例,如下:

[yuki@myhost ~]$ cat /etc/logrotate.d/nginx
/home/nginx/logs/*.log{
	daily
	rotate 30
	dateext
	nocompress
	copytruncate  
	notifempty
	create 640 nginx adm
	missingok
	postrotate
    [ -f /home/nginx/logs/nginx.pid ] && /bin/kill -HUP `cat /home/nginx/logs/nginx.pid 2> /dev/null` 2> /dev/null || true
	endscript
}

在该配置文件中,每个参数作用如下:

  • /home/nginx/logs/ 为nginx日志的存储目录,可以根据实际情况进行修改。
  • daily:日志文件将按天轮循;
  • weekly:日志文件将按周轮循;
  • monthly:日志文件将按月轮循。
  • missingok:在日志轮循期间,任何错误将被忽略,例如“文件无法找到”之类的错误。
  • rotate 30:一次存储30个日志文件。对于第31个日志文件,时间最久的那个日志文件将被删除。
  • dateext:定义日志文件后缀是日期格式,也就是切割后文件是:xxx.log-20160402.gz这样的格式。如果该参数被注释掉,切割出来是按数字递增,即前面说的 xxx.log-1这种格式。
  • compress:在轮循任务完成后,已轮循的归档将使用gzip进行压缩。
  • delaycompress:总是与compress选项一起用,delaycompress选项指示logrotate不要将最近的归档压缩,压缩将在下一次轮循周期进行。这在你或任何软件仍然需要读取最新归档时很有用。
  • notifempty:如果是空文件的话,不进行转储。
  • create 640 nginx adm:以指定的权限和用户属性,创建全新的日志文件,同时logrotate也会重命名原始日志文件。
  • postrotate/endscript:在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。在这种情况下,rsyslogd进程将立即再次读取其配置并继续运行。注意:这两个关键字必须单独成行。
3.4 查看logrotate默认执行时间
[yuki@myhost ~]$ sudo cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22    ###这个参数就是配置logrotate切割的时间点(默认凌晨3点22分)###

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

如果你想在指定时间点,让logrotate切割日志的话,可以修改此配置文件的START_HOURS_RANGE参数。但是经过多次实验,我发现logrotate没有按照我指定的时间进行切割日志。所以,最后我将切割时间直接写入/etc/crontab里面!!!

3.5 编写定时任务
[yuki@myhost ~]$ sudo cat /etc/crontab
59 23 * * * root /usr/sbin/logrotate  -f  /etc/logrotate.d/nginx

4. 编写Shell 脚本+系统自带的crontab 命令

4.1 编写Shell 脚本
[yuki@myhost script ]$ cat /home/script/cut_del_nginx_logs.sh 
#!/bin/sh
source /etc/profile

#define variables

logs_path="/home/nginx/logs"
yesterday_date="`date -d 'yesterday' +%Y-%m-%d`"
today_date="`date +%Y-%m-%d`"
special_day_date="`date -d '60 day ago' +%Y-%m-%d`"

#logrotate by day

mv ${logs_path}/access.log   ${logs_path}/access.${yesterday_date}.log

#向nginx主进程发送USR1信号,重新打开日志文件,否则会继续往mv后的文件写数据的。
#原因在于:linux系统中,内核是根据文件描述符来找文件的。如果不这样操作导致日志切割失败。

kill -USR1 `ps axu | grep "nginx: master process" | grep -v grep | awk '{print $2}'`

#删除60天前的日志

if [ -d ${logs_path}/access.${special_day_date}.log ];
        then
                rm -rf ${logs_path}/access.${special_day_date}.log
        else
                echo "$special_day_date这天没有日志"

fi

该shell脚本有两个功能,第一个是切割nginx日志,第二个是删除60天前的nginx日志。
在切割nginx日志的功能中,我们要注意该shell脚本命名切割的日志是以前一天的时间命名该日志文件的。
所以我们在把该shell脚本放在crontab中执行时,建议在每天的0点0分执行。如下:

4.2 编写定时任务
[yuki@myhost script ]$ sudo cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

00 00 * * * root /home/script/cut_del_nginx_logs.sh 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值