Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2549316
  • 博文数量: 271
  • 博客积分: 6659
  • 博客等级: 准将
  • 技术积分: 3141
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-17 10:24
文章分类

全部博文(271)

文章存档

2016年(2)

2015年(12)

2014年(7)

2013年(19)

2012年(22)

2011年(81)

2010年(128)

分类: 系统运维

2011-03-17 16:12:48

Apache条件日志(限制写入日志文件的格式、条件)
 
许多时候,根据与请求特征相关的环境变量来有选择地记录某些客户端请求会带来便利。
首先,需要使用SetEnvIf指令来设置特定的环境变量以标识符合某种特定条件的请求,
然后用CustomLog指令的 env= 子句,根据这些环境变量来决定记录或排除特定的请求。
例如:

# 不记录本机发出的请求
SetEnvIf Remote_Addr "127\.0\.0\.1" dontlog
# 不记录对robots.txt文件的请求
SetEnvIf Request_URI "^/robots\.txt$" dontlog
# 记录其他请求
CustomLog logs/access_log common env=!dontlog
 
 
再如,将使用英语的请求记录到一个日志,而记录非英语的请求到另一个日志:
SetEnvIf Accept-Language "en" english
CustomLog logs/english_log common env=english
CustomLog logs/non_english_log common env=!english
 
比如我不想.gif,,.jpg,.xbm结尾的都不写入日志文件,则:
SetEnvIf Request_URI "\.gif$" object_is_image=yes
SetEnvIf Request_URI "\.jpg$" object_is_image=yes
SetEnvIf Request_URI "\.xbm$" object_is_image=yes
CustomLog logs/access_log common object_is_image=!yes

为了提高系统的访问速度,需要减少Apache日志操作,下面是Apache日志过滤的参考样例:
# filter the localhost visit
SetEnvIf Remote_Addr "127\.0\.0\.1" dontlog
# filter some special directories
SetEnvIf Request_URI "^/system/.*$" dontlog
SetEnvIf Request_URI "^/export/.*$" dontlog
SetEnvIf Request_URI "^/resources/.*$" dontlog
# filter the intranet visit
SetEnvIf Remote_Addr "211\.167\.51\.199" dontlog
# filter the google bot
SetEnvIf Remote_Addr "66\.249\.6[4-9]\.[0-9]+" dontlog
SetEnvIf Remote_Addr "66\.249\.[7-8][0-9]\.[0-9]+" dontlog
SetEnvIf Remote_Addr "66\.249\.9[0-5]\.[0-9]+" dontlog
# filter the microsoft bot: 65.52.0.0 - 65.55.255.255
SetEnvIf Remote_Addr "65\.5[2-5]\.[0-9]+\.[0-9]+" dontlog
CustomLog logs/access_log common env=!dontlog
 

可以通过cronolog 日志截取软件 按每天的记录分隔日志
CustomLog "|/usr/local/sbin/cronolog /usr/local/apache2/logs/access_%Y%m%d.log" combined env=!dontlog
 
具体的setenvif语法信息请查看
Apache > HTTP Server > 文档 > 版本2.2 > 模块 >Apache模块 mod_setenvif

######################################
common  combined  两种日志记录格式
如果需要通过一些log统计程序如awstats来查看apache log  则为获取更详细的日志输入当采用 combined方式
通用日志格式(Common Log Format)
这是一个典型的记录格式:
LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog logs/access_log common
组合日志格式(Combined Log Format)
另一种常用的记录格式是组合日志格式,形式如下:
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
CustomLog log/access_log combined

这种格式与通用日志格式类似,但是多了两个 %{header}i 项
其中,多出来的项是:
"" (\"%{Referer}i\")
"Referer"请求头。此项指明了该请求是被从哪个网页提交过来的,这个网页应该包含有/apache_pb.gif或者其连接。
"Mozilla/4.08 [en] (Win98; I ;Nav)" (\"%{User-agent}i\")
"User-Agent"请求头。此项是客户端提供的浏览器识别信息。
 
关于更详细的 日志信息 可查
Apache > HTTP Server > 文档 > 版本2.2>日志文件
 
阅读(4679) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~