设为首页收藏本站

数码鹭岛论坛

 找回密码
 注-册

QQ登录

只需一步,快速开始

搜索
查看: 3040|回复: 0
打印 上一主题 下一主题

使用Cronolog实现Nginx日志切换的3种方案

[复制链接]
跳转到指定楼层
1#
发表于 2011-6-15 09:13:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
http://masterkey.javaeye.com/blog/170918 Nginx and Cronolog (2008-03-13)
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(Nginx作者) 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 )的方案!
http://syre.blogbus.com/logs/32229878.html 让 nginx 用上 cronolog (神仙 @ 2008-12-07 10:45 )
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://syre.blogbus.com/logs/32229878.html
nginx 的日志是不支持 |/path/to/cronolog 这样的写法的,不能直接支持 cronolog 来轮转日志。但是日志总是要拆分的。
一般都是每天跑一个 crontab ,把原来的日志 mv 一下,然后 kill -USR1 让 nginx 重新打开日志文件。
以前想到过应该可以利用命名管道来做这事情,昨天去实践了一下。
1,先创建一个命名管道,
mkfifo /path/to/nginx/logs/access_log_pipe ,当然也可以直接用命名管道代替原来的日志文件。
2,然后让 cronolog 从这个文件读:
/path/to/cronolog /path/to/log/access_%Y%m%.log < /path/to/nginx/logs/access_log_pipe &
3,把 nginx 的日志路径指到那个命名管道然后重启一下就可以了。之后在 nginx 的启动脚本里要把启动 cronolog 放在启动nginx 之前。
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享
您需要登录后才可以回帖 登录 | 注-册

本版积分规则

小黑屋|手机版|Archiver|数码鹭岛 ( 闽ICP备20006246号 )  

counter

GMT+8, 2025-12-3 18:18 , Processed in 0.073518 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表