gznz12345 发表于 2018-11-14 08:02:58

利用logrotate切割nginx日志

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


[*]# see "man logrotate" for details
[*]# rotate log files weekly
[*]daily
[*]
[*]# keep 4 weeks worth of backlogs
[*]rotate 4
[*]
[*]# create new (empty) log files after rotating old ones
[*]create
[*]
[*]# use date as a suffix of the rotated file
[*]dateext
[*]
[*]# uncomment this if you want your log files compressed
[*]#compress
[*]
[*]# RPM packages drop log rotation information into this directory
[*]include /etc/logrotate.d
[*]
[*]# no packages own wtmp and btmp -- we'll rotate them here
[*]/var/log/wtmp {
[*]    monthly
[*]    create 0664 root utmp
[*]    minsize 1M
[*]    rotate 1
[*]}
[*]
[*]/var/log/btmp {
[*]    missingok
[*]    monthly
[*]    create 0600 root utmp
[*]    rotate 1
[*]}
[*]# system-specific logs may be also be configured here.
  

  创建一个nginx文件
  


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

  增加以下内容
  


[*]/usr/local/nginx/logs/*.log {
[*]    daily
[*]    rotate 31
[*]    missingok
[*]    notifempty
[*]    nocompress
[*]    postrotate
[*]      /bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null || :
[*]    endscript
[*]}
  

  强制生成一次日志信息
  


[*]logrotate -f /etc/logrotate.conf
  

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



页: [1]
查看完整版本: 利用logrotate切割nginx日志