Nginx and Cronolog (zz)

Nginx 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 :)

  1. Configure nginx.conf to log to logs/access.log and logs/error.log like normal.
  2. Remove those files from the logs directory.
  3. Recreate them as fifo's using "mkfifo access.log" and "mkfifo error.log".
  4. 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 )的方案!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值