2008-03-13

Nginx and Cronolog (zz)

关键字: nginx cronolog

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 )的方案!

评论
发表评论

您还没有登录,请登录后发表评论

masterkey
搜索本博客
最近加入圈子
存档
最新评论