Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1682717
  • 博文数量: 391
  • 博客积分: 8464
  • 博客等级: 中将
  • 技术积分: 4589
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-13 15:12
个人简介

狮子的雄心,骆驼的耐力,孩子的执著!

文章分类

全部博文(391)

文章存档

2023年(4)

2018年(9)

2017年(13)

2016年(18)

2014年(7)

2013年(29)

2012年(61)

2011年(49)

2010年(84)

2009年(95)

2008年(22)

分类: 系统运维

2016-12-17 16:46:49


请求本身的情况,将通过在格式字符串中放置各种"%"转义符的方法来记录,它们在写入日志文件时,根据下表的定义进行转换:
 
%a 远端IP地址
%A 本机IP地址
%B 除HTTP头以外传送的字节数
%b 以CLF格式显示的除HTTP头以外传送的字节数,也就是当没有字节传送时显示’-’而不是0。
%{Foobar}C 在请求中传送给服务端的cookieFoobar的内容。
%D 服务器处理本请求所用时间,以微为单位。
%{FOOBAR}e 环境变量FOOBAR的值
%f 文件名
%h 远端主机
%H 请求使用的协议
%{Foobar}i 发送到服务器的请求头Foobar:的内容。
%l 远端登录名(由identd而来,如果支持的话),除非IdentityCheck设为"On",否则将得到一个"-"。
%m 请求的方法
%{Foobar}n 来自另一个模块的注解Foobar的内容。
%{Foobar}o 应答头Foobar:的内容。
%p 服务器服务于该请求的标准端口。
%P 为本请求提供服务的子进程的PID。
%{format}P 服务于该请求的PID或TID(线程ID),format的取值范围为:pid和tid(2.0.46及以后版本)以及hextid(需要 APR1.2.0及以上版本)
%q 查询字符串(若存在则由一个"?"引导,否则返回空串)
%r 请求的第一行
%s 状态。对于内部重定向的请求,这个状态指的是原始请求的状态,—%>s则指的是最后请求的状态。
%t 时间,用普通日志时间格式(标准英语格式)
%{format}t 时间,用strftime(3)指定的格式表示的时间。(默认情况下按本地化格式)
%T 处理完请求所花时间,以秒为单位。
%u 远程用户名(根据验证信息而来;如果返回status(%s)为401,可能是假的)
%U 请求的URL路径,不包含查询字符串。
%v 对该请求提供服务的标准ServerName。
%V 根据UseCanonicalName指令设定的服务器名称。(VirtualHost->ServerName)
%X 请求完成时的连接状态:X= 连接在应答完成前中断。
+= 应答传送完后继续保持连接。
-= 应答传送完后关闭连接。
 
(在1.3以后的版本中,这个指令是%c,但这样就和过去的SSL语法:%{var}c冲突了)
%I 接收的字节数,包括请求头的数据,并且不能为零。要使用这个指令你必须启用mod_logio模块。
%O 发送的字节数,包括请求头的数据,并且不能为零。要使用这个指令你必须启用mod_logio模块。
 
修饰符
可以紧跟在"%"后面加上一个逗号分隔的状态码列表来限制记录的条目。例如,"@0,501{User-agent}i" 只记录状态码400和501发生时的User-agent头内容;不满足条件时用"-"代替。状态码前还可以加上"!"前缀表示否 定,"%!200,304,302{Referer}i"记录所有不同于200,304,302的状态码发生时的Referer头内容。
 
"<"和">"修饰符可以用来指定对于已被内部重定向的请求是选择原始的请求还是选择最终的请求。默认情况下,%s, %U, %T, %D, %r 使用原始请求,而所有其他格式串则选择最终请求。例如,%>s 可以用于记录请求的最终状态,而 % 示例
一些常见的格式串:
 
通用日志格式(CLF)
"%h %l %u %t /"%r/" %>s %b"
 
带虚拟主机的通用日志格式
"%v %h %l %u %t /"%r/" %>s %b"
 
NCSA扩展/组合日志格式
"%h %l %u %t /"%r/" %>s %b /"%{Referer}i/" /"%{User-agent}i/""
 
Referer日志格式
"%{Referer}i -> %U"
 
Agent(Browser)日志格式
"%{User-agent}i"
 
高大全的日志格式:
LogFormat "%{x-forwarded-for}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{Cookie}i\" \"%{User-Agent}i\" \"%{Content-Length}i\""
 
负载均衡时获取真实IP
  1. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
修改为:
  1. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Forwarded-For}i\" " combined

推荐配置:httpd.conf

  1. <IfModule log_config_module>
  2.     #
  3.     # The following directives define some format nicknames for use with
  4.     # a CustomLog directive (see below).
  5.     #
  6.     #LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{Content-Length}i\"" combined
  7.     LogFormat "%{x-forwarded-for}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{Cookie}i\" \"%{User-Agent}i\" \"%{Content-Length}i\"" combined
  8.  
  9.     #LogFormat "%h %l %u %t \"%r\" %>s %b" common
  10.     LogFormat "%{x-forwarded-for}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{Cookie}i\" \"%{User-Agent}i\" \"%{Content-Length}i\"" common
  11.  
  12.     <IfModule logio_module>
  13.       # You need to enable mod_logio.c to use %I and %O
  14.       LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
  15.     </IfModule>
  16.     #
  17.     # The location and format of the access logfile (Common Logfile Format).
  18.     # If you do not define any access logfiles within a <VirtualHost>
  19.     # container, they will be logged here. Contrariwise, if you *do*
  20.     # define per-<VirtualHost> access logfiles, transactions will be
  21.     # logged therein and *not* in this file.
  22.     #
  23.         CustomLog "logs/access_log" common
  24.         #CustomLog "logs/access_log" combined
  25.  
  26.     #
  27.     # If you prefer a logfile with access, agent, and referer information
  28.     # (Combined Logfile Format) you can use the following directive.
  29.     #
  30.     #CustomLog "logs/access_log" combined
  31. </IfModule>

阅读(775) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~