利用logrotate切割nginx日志

为了防止nginx日志日积月累变得很大而不方便查看管理,我们需要用logrotate对nginx的日志按照日期进行归档,以备查看分析nginx的访问记录等信息。

编辑/etc/logrotate.conf文件


 
  1. # see "man logrotate" for details  
  2. # rotate log files weekly  
  3. daily  
  4.  
  5. # keep 4 weeks worth of backlogs  
  6. rotate 4  
  7.  
  8. # create new (empty) log files after rotating old ones  
  9. create  
  10.  
  11. # use date as a suffix of the rotated file  
  12. dateext  
  13.  
  14. # uncomment this if you want your log files compressed  
  15. #compress  
  16.  
  17. # RPM packages drop log rotation information into this directory  
  18. include /etc/logrotate.d  
  19.  
  20. # no packages own wtmp and btmp -- we'll rotate them here  
  21. /var/log/wtmp {  
  22.     monthly  
  23.     create 0664 root utmp  
  24.     minsize 1M  
  25.     rotate 1  
  26. }  
  27.  
  28. /var/log/btmp {  
  29.     missingok  
  30.     monthly  
  31.     create 0600 root utmp  
  32.     rotate 1  
  33. }  
  34. # system-specific logs may be also be configured here.  

创建一个nginx文件


 
  1. # ll /etc/logrotate.d/nginx  
  2. -rw-r--r-- 1 root root 246 4月   5 20:09 /etc/logrotate.d/nginx  

增加以下内容


 
  1. /usr/local/nginx/logs/*.log {  
  2.     daily  
  3.     rotate 31  
  4.     missingok  
  5.     notifempty  
  6.     nocompress  
  7.     postrotate  
  8.         /bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null || :  
  9.     endscript  

强制生成一次日志信息


 
  1. logrotate -f /etc/logrotate.conf 

查看生产的日志,发现日志已经切割了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值