Nginx and Cronolog

March 15, 2007

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 :( 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 &