nginx 日志切割

908 阅读1分钟

$ vim nginx-log-rotate

/data0/log-data/shihuo.log {
  su root root
  nocompress
  daily
  copytruncate
  create
  notifempty
  rotate 7
  olddir /data0/log-data
  missingok
  dateext
  postrotate
    /bin/kill -HUP `cat /var/run/nginx.pid 2>/dev/null` 2>/dev/null || true
  endscript
}

/data0/log-data/*.log 使用通配符时,/data0/log-data/目录下的所有匹配到的日志文件都将切割。如果要切割特定日志文件,就指定到该文件。

设置计划任务

$ vim /etc/crontab

59 23 * * * root ( /usr/sbin/logrotate -f /PATH/TO/nginx-log-rotate)

这样每天 23 点 59 分钟执行日志切割。

分割日志时报错:

error: skipping "/var/log/dnx/.access.log" because parent directory has insecure permissions (It’s world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.

解决方法: 添加"su 用户组",如su root root