nginx可以作为很多服务端的反向代理服务器,所以随着时间的增长,access.log和error.log会越来越大,那么可能造成的问题有:

  • 日志过大,写入日志的性能降低,nginx的响应时间变慢,
  • 日志过大,可能磁盘空间不够,造成网站打不开都是有可能的。

所以,我们不能让日志文件一直这么增加下去,需要对日志进行分割,保存最近多少次分割的日志信息,这里介绍日志分割工具 logrotate。

logrotate 介绍

logrotate是linux系统的一种日志切割工具,一般地,linux系统都自带logrotate日志切割工具而不需要去安装它,logrotate可以自动对日志进行切割、压缩以及删除旧的日志文件,防止日志文件无限增长,造成磁盘空间的不足。

可以使用命令

rpm -ql logrotate

    看系统是否安装,如下:

    nginx 日志分割配置Logrotate

    从截图可以看出,系统中已经安装了logrotate日志切割工具,如果没有安装请使用如下命令安装logrotate即可

    yum -y install logrotate

      logrotate是基于crontab运行的,计划每天运行的脚本在 /etc/cron.daily/logrotate

      该路径下的文件不需要我们自己配置,它每天会自己调用一次/etc/cron.daily下的脚本。

      从该脚本可以看到logrotate的配置文件位于/etc/logrotate.conf ,该配置文件的内容如下:

      # see "man logrotate" for details
      # rotate log files weekly
      weekly
      
      # 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. 
      • 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
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34

      从上面的配置文件可以看出/etc/logrotate.d是自定义的配置文件路径。

      接下来介绍nginx中使用logrotate进行日志切割。

      nginx结合 logrotate 日志切割

      我们切换到目录/etc/logrotate.d看到

      nginx 日志分割配置Logrotate

      它有一个默认的nginx使用logrotate 的配置文件,内容如下:

      /var/log/nginx/*.log {
              daily
              missingok
              rotate 52
              compress
              delaycompress
              notifempty
              create 640 nginx adm
              sharedscripts
              postrotate
                      if [ -f /var/run/nginx.pid ]; then
                              kill -USR1 `cat /var/run/nginx.pid`
                      fi
              endscript
      }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14

      它会对路径/var/log/nginx/*.log下的日志文件进行切割、压缩、删除。

      相关参数解释:

      • daily: 按天切割。触发切割时如果时间不到一天不会执行切割。除了daily,还可以选monthly,weekly,yearly
      • missingok: 切割中遇到日志错误忽略
      • rotate number: 切割后的压缩文件保留的份数,这里保留52份。
      • delaycompress:当前转储的日志文件到下一次转储时才压缩
      • notifempty:空文件不进行轮转。
      • postrotate/endscript: 执行的脚本
      • create 640 nginx adm: 以指定的权限创建全新的日志文件,同时logrotate也会重命名原始日志文件

      logrotate 虚拟主机日志切割配置

      在前面介绍的内容中,/etc/logrotate.d/nginx 是nginx默认的日志切割配置,意思是安装nginx后就都配置好了。

      但是虚拟主机的日志配置有时候需要自定义路径,比如:

      server {
      	error_log /data/log/yxjc123.com/error.log;
      	access_log /data/log/yxjc123.com/access.log;        
      }
      • 1
      • 2
      • 3
      此时, 路径/data/log/yxjc123.com 不在/var/log/nginx,那么此时有两种方式处理这个问题。

      1) logrotate配置多个路径,改写刚才的nginx的日志配置文件增加路径/data/log/yxjc123.com/,如下:

      /var/log/nginx/*.log {
              daily
              missingok
              rotate 52
              compress
              delaycompress
              notifempty
              create 640 nginx adm
              sharedscripts
              postrotate
                      if [ -f /var/run/nginx.pid ]; then
                              kill -USR1 `cat /var/run/nginx.pid`
                      fi
              endscript
      }
      
      /data/log/yxjc/*.log{
              daily
              missingok
              rotate 52
              compress
              delaycompress
              notifempty
              create 640 nginx adm
              sharedscripts
              postrotate
                      if [ -f /var/run/nginx.pid ]; then
                              kill -USR1 `cat /var/run/nginx.pid`
                      fi
              endscript
      } 
      • 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
      • 29
      • 30
      2) 再创建一个nginxYxjc123配置文件
      /data/log/yxjc123/*.log {
              daily
              missingok
              rotate 52
              compress
              delaycompress
              notifempty
              create 640 nginx adm
              sharedscripts
              postrotate
                      if [ -f /var/run/nginx.pid ]; then
                              kill -USR1 `cat /var/run/nginx.pid`
                      fi
              endscript
      }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      使用logrotate测试一下是否有效:
      logrotate -d -f /etc/logrotate.d/nginx