Nginx and Cronolog (zz)
关键字: nginx cronologNginx and Cronolog
I recently setup Nginx for one of our webservers and needed to hook cronolog up to it for all the normal reasons you want cronolog.
But Nginx doesn't support piped logging yet :( But we can use fifo's though to accomplish the same thing :)
- Configure nginx.conf to log to logs/access.log and logs/error.log like normal.
- Remove those files from the logs directory.
- Recreate them as fifo's using "mkfifo access.log" and "mkfifo error.log".
- Tweak the nginx startup script to start cronolog just before nginx.
Something like this:
(cat /usr/local/nginx/logs/access.log |\ /usr/local/sbin/cronolog -l /var/log/nginx/access.log \ /var/log/nginx/%Y/%m/%d/%H/access.log) & (cat /usr/local/nginx/logs/error.log |\ /usr/local/sbin/cronolog -l /var/log/nginx/error.log \ /var/log/nginx/%Y/%m/%d/%H/error.log) &
That's it. It seems that you'd need to stop cronolog when shutting down nginx, but at least on CentOS this isn't required. I suspect that when the fifo is closed for writing it gets closed for reading and cat exists which exits cronolog as well. Would love it it someone could confirm that though.
UPDATE
Igor Sysoev made the comment that the above might hinder nginx's performance because of context switching and the blocking between the worker processes. So instead of the above you can simulate it with the following as an hourly cron task:
log_dir="/var/log/nginx"
date_dir=`date +%Y/%m/%d/%H`
/bin/mkdir -p ${log_dir}/${date_dir} > /dev/null 2>&1
/bin/mv ${log_dir}/access.log ${log_dir}/${date_dir}/access.log
/bin/mv ${log_dir}/error.log ${log_dir}/${date_dir}/error.log
kill -USR1 `cat /var/run/nginx.pid`
/bin/gzip ${log_dir}/${date_dir}/access.log &
/bin/gzip ${log_dir}/${date_dir}/error.log &
我还是很喜欢nginx作者(Igor Sysoev )的方案!
发表评论
- 浏览: 33088 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
链接
最新评论
-
一位台湾校长的震动所有中 ...
如果这样的校长能占到中国所有学校的千分之一,今日的中国就不会是现在的这种样子了。
-- by bnmcvzx -
关于memcachefs
是采用memcached作为存储介质的一种文件系统。memcached可以运行于 ...
-- by masterkey -
关于memcachefs
和普通基于内存的文件系统相比有什么特点?
-- by 都别装了 -
nginx 0.7.1 release
我发现discuz已经在用nginx7.0,那说明这个版本已经完全可以运用于商业 ...
-- by Arden -
nginx 0.7.1 release
这是开发版
-- by 剑 事






评论排行榜