linux运维之logrotate日志轮替――对nginx日志进行轮替


本文摘自PHP中文网,作者齐天大圣,侵删。

linux系统上有一个非常好用的轮替服务――logrotate。通过这个服务,可以对日志文件进行轮替管理。当日志文件过大时,可以对其进行切割成多个小的日志文件,还可以对其进行压缩处理。nginx默认的日志文件access.log以及error.log日志文件,是不会自行进行轮替的。所以,经常会用logrotate对nginx的日志进行轮替管理。

logrotate

对于logrotate这个程序的运行,是挂在系统的定时任务中每日执行的。

1

2

3

4

5

# cat /etc/cron.daily/logrotate

#!/bin/sh

 

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status

……

下面,我们来看看logrotate的配置文件/etc/logrotate.conf。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

# 通过man logrotate查看更多信息

 

# 下面是默认配置

weekly <=== 默认一周进行一次轮替操作

rotate 4 <=== 默认保存4份日志文件

create <==== 当轮替完成后,创建一个新的日志文件

dateext <=== 轮替的文件名前加上日期以便管理

#compress  <=== 默认对轮替的文件不进行压缩,如果想压缩,就把#去掉

 

#将/etc/logrotate.d目录下的文件加载进来

include /etc/logrotate.d

 

 

# 下面是两个日志文件轮替的规则

/var/log/wtmp {  <=== 指定文件名

    monthly <=== 一个月进行一次轮替

    create 0664 root utmp <=== 新的日志文件权限及所有人,所属组设置

 minsize 1M <=== 日志文件至少有1M时候才进行轮替

    rotate 1  <=== 只保留一份日志文件

}

 

/var/log/btmp {

    missingok <=== 如果日志文件不存在,则不报错,直接忽略

    notifempty <=== 如果日文文件是空的则不进行轮替

    monthly

    create 0600 root utmp

    rotate 1

}

在了解完logrotate的配置文件后,我们就可以自己弄一个日志进行轮替练习了。首先,我们准备一个日志文件

1

2

# ll logrotate_learn.log

-rwx------ 1 www www 223288003 Nov 12 09:05 logrotate_learn.log

这里,我们最好给该日志文件加上-a属性,只能向其新增内容。

1

# chattr +a logrotate_learn.log

接下来,我们要思考该日志文件的轮替规则了。这里,我们打算每周进行一次轮替并对轮替的文件进行压缩处理,并且当文件大小超过10M时候才进行轮替,另外,保留7份日志文件。这里,还有一点需要注意的是。因为该文件有a属性,所以轮替前需要将a属性删除,然后在轮替完成后再将a属性加上去。那么,现在我们需要写配置规则了

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

vim /etc/logrotate.d/logrotate_learn

 

# 轮替练习

/root/logrotate_learn.log

{

    missingok

    notifempty

    weekly

    rotate 7

    size=10M

    create 0700 www www

    compress

    sharedscripts

    prerotate

       /usr/bin/chattr -a /root/logrotate_learn.log

    endscript

    sharedscripts

    postrotate

        /usr/bin/chattr +a /root/logrotate_learn.log

    endscript

}

下面,我们来进行轮替。

1

2

3

4

5

6

7

8

9

10

11

12

13

# logrotate [-vf] logfile

选项与参数:

-v :启动显示模式,会显示logrotate 运作的过程喔!

-f :不论是否符合,强制每个日志文件都进行rotate 的动作!

 

# logrotate -v /etc/logrotate.d/logrotate_learn

reading config file /etc/logrotate.d/logrotate_learn

Allocating hash table for state file, size 15360 B

……

 

# ll logrotate_learn.log*

-rwx------ 1 www www        0 Nov 12 10:15 logrotate_learn.log

-rwx------ 1 www www 20974585 Nov 12 10:15 logrotate_learn.log.1.gz


对nginx日志文件进行轮替管理

学完后如果对日志文件进行轮替管理后,我们现在进入实战练习。

1

2

3

4

5

6

# ll /www/wwwlogs/

total 1852452

-rwx------ 1 www www    350288 Nov 12 02:50 access.log

-rwx------ 1 www www  14016937 Nov 11 22:44 isunshinenet.com.error.log

-rwx------ 1 www www 300416647 Nov 11 22:44 isunshinenet.com.log

-rwx------ 1 www www         0 Sep 30 09:57 nginx_error.log

我们需要对这四个日志文件进行轮替管理,首先,创建好配置文件。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

# 对nginx日志文件进行呢轮替

/www/wwwlogs/access.log

/www/wwwlogs/nginx_error.log

/www/wwwlogs/isunshinenet.com.error.log

/www/wwwlogs/isunshinenet.com.log

{

    missingok

    notifempty

    daily

    rotate 7

    size=1M

    create 0700 www www

    sharedscripts

    postrotate

 if [ -f /www/server/nginx/logs/nginx.pid ];then

    /www/server/nginx/sbin/nginx -s reload 

 fi

    endscript

}

至此,我们就完成了对nginx日志的轮替管理了,每天系统都会自行检测,需要进行轮替时,就会进行轮替管理。

以上就是linux运维之logrotate日志轮替――对nginx日志进行轮替的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

Linux如何查看防火墙是否开启

Linux中出现网络故障该如何排除

Linux如何修改文件名?

ecs Linux服务器重启后数据丢失的解决方案

Linux下find和grep区别和常用命令介绍

怎么在Linux写c语言

几个常用的Linux系统之间传输文件的命令

什么系统是基于Linux系统的

Linux如何删除指定文件

Linux下怎么查看哪些端口被占用

更多相关阅读请进入《Linux》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...