nginx的logrotateconfiguration

什么是最好的方法来旋转nginx日志文件? 在我看来,我应该在/etc/logrotate.d/中创build一个“nginx”文件,并用下面的代码填充它,然后执行/etc/init.d/syslog restart。

这将是我的configuration(我还没有testing过):

/usr/local/nginx/logs/*.log { #rotate the logfile(s) daily daily # adds extension like YYYYMMDD instead of simply adding a number dateext # If log file is missing, go on to next one without issuing an error msg missingok # Save logfiles for the last 49 days rotate 49 # Old versions of log files are compressed with gzip compress # Postpone compression of the previous log file to the next rotation cycle delaycompress # Do not rotate the log if it is empty notifempty # create mode owner group create 644 nginx nginx #after logfile is rotated and nginx.pid exists, send the USR1 signal postrotate [ ! -f /usr/local/nginx/logs/nginx.pid ] || kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` endscript } 

我在/ usr / local / nginx / logs /中都有access.log和error.log文件,并且想要每天都进行轮换。 任何人都可以告诉我,如果“dateext”是正确的? 我想日志文件名是像“access.log-2010-12-04”。 还有一件事:我可以每天在特定的时间(例如,下午11点)做日志轮换吗? 如果是这样,怎么样? 谢谢。

man logrotate

  dateformat format_string Specify the extension for dateext using the notation similar to strftime(3) function. Only %Y %m %d and %s specifiers are allowed. The default value is -%Y%m%d. Note that also the character sep‐ arating log name from the extension is part of the dateformat string. The system clock must be set past Sep 9th 2001 for %s to work correctly. Note that the datestamps generated by this format must be lexically sortable (ie, first the year, then the month then the day. eg, 2001/12/01 is ok, but 01/12/2001 is not, since 01/11/2002 would sort lower while it is later). This is because when using the rotate option, logrotate sorts all rotated filenames to find out which logfiles are older and should be removed. 

任何人都可以告诉我,如果“dateext”是正确的? 我想日志文件名是像“access.log-2010-12-04”。

在你的configuration文件中插入一个dateformat指令,如下所示:

  /usr/local/nginx/logs/*.log { daily dateext dateformat -%Y-%m-%d ... 

还有一件事:我可以每天在特定的时间(例如,下午11点)做日志轮换吗?

默认情况下,logrotate在凌晨4点通过cron运行:

/etc/cron.daily/logrotate

 #!/bin/sh /usr/sbin/logrotate /etc/logrotate.conf EXITVALUE=$? if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" fi exit 0 

你可以把这个文件移动到某个地方,然后重命名为logrotate.sh ,然后在/etc/cron.d/创build一个新文件,如下所示:

 0 23 * * * root /path/to/logrotate.sh 

你可以一次旋转所有的虚拟主机:

 /var/www/vhosts/*/logs/*.log { ... } 

dateext的值由dateformat指令给出,默认为%Y%m%d (月,月,日)。 您可以将其自定义为%Y-%m-%d

如果你已经安装并运行了logrotate ,它很可能每天都作为一个cron作业运行,你只需要find它来改变时间(记住其他的东西会影响到它,比如anacron的用法,每个系统)。

检查cronolog, http: //cronolog.org/这应该做你所需要的