内容搜索
热门内容
推荐内容
最新内容
提出意见和建议

Nginx and Cronolog (zz)


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


 
点这里复制本页地址发送给您QQ/MSN上的好友
相关文章

Nginx Memcached, a 400% boost
new-nginx-conf-with-optimizations
nagix-php.conf
Media Wiki User Permissions

相关评论


本文章所属分类:首页 文档 系统配置管理