nginx日志切割
http://www.ttlsa.com/nginx/nginx-log-cutting/
nginx日志默认情况下统统写入到一个文件中,文件会变的越来越大,非常不方便查看分析。以日期来作为日志的切割是比较好的,通常我们是以每日来做统计的。下面来说说nginx日志切割。
关于nginx相关日志配置参见:《nginx日志配置》一文。logrotate用法参见《logrotate日志管理工具》。
1. 定义日志轮滚策略
# vim nginx-log-rotate
/data/weblogs/*.log { |
nocompress |
daily |
copytruncate |
create |
notifempty |
rotate 7 |
olddir /data/weblogs/old_log |
missingok |
dateext |
postrotate |
/bin/ kill -HUP ` cat /var/run/nginx.pid 2> /dev/null` 2> /dev/null || true |
endscript |
} |
2. 设置计划任务
# vim /etc/crontab
59 23 * * * root ( /usr/sbin/logrotate -f /PATH/TO/nginx-log-rotate) |
这样每天23点59分钟执行日志切割。
如需转载请注明出处: http://www.ttlsa.com/html/3166.html
在linux环境下,我希望能够每天到0点切换nginx日志,前一天的日志使用日期后缀,并且压缩。
nginx and Logrotate
February 3, 2012
I wanted to start cycling and compressing my previous nginx log files, as they were getting a little large. NGINX doesn't have built in support for log rotation, there is a system level linux command called logrotate that handles this on behalf of many applications. I'm doing this on a Debian squeeze.
/etc/logrotate.d
In /etc/logrotate.d
you'll find a file for each application that is using logrotate. There should be one in here called nginx
that contains the following:
/var/log/nginx/*.log { daily missingok rotate 52 compress delaycompress notifempty create 640 root adm sharedscripts postrotate [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid` endscript }
This is the default rotate process for nginx files. I store my logs in /home/drumcoder/logs
, so we will specify another block in this same file, using many of the same options.
/home/drumcoder/log/*.access.log { daily missingok rotate 52 mail drumcoder@here.com compress delaycompress notifempty sharedscripts postrotate [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid` endscript }
The following options are used:
daily
- rotates the log files dailymissingok
- it's fine if there is no log to rotaterotate 52
- rotate logs 52 times, and then delete the oldestmail drumcoder@here.com
- before you delete an old log, send it to this mail addresscompress
- use gzip on old logsdelaycompress
- leave one rotated log that isn't compressed so the process can still write to it if needednotifempty
- don't rotate empty filesharedscripts
- when telling nginx that logs have been rotated, only do it once rather than once for each file group
The bottom postrotate
block sends a signal to nginx to tell it that logs have been rotated and it should pick up the file handles for the new ones.