Apache日志管理分类
日志文件是用户管理和监控 Apache 安全的非常好的第一手资料,它清晰地记录了客户端访问 Apache 服务器资源的每一条记录,以及在访问中出现的错误信息,可以这样说,Apache 可以记录 Web 访问中感兴趣的几乎所有信息。
一、工作记录
实践案例:天气预报15天查询(http://tqybw.net)
分析原因:网站上线时能过日志分析网站运行状态,找出错误改进,用来监控分析网站的访问情况!
记录时间:2012-12-12
二、详细说明
当运行 Apache 服务器时生成 4 个标准的日志文件:
- 错误日志
- 访问日志
- 传输日志
- Cookie 日志
其中比较常见的是访问日志(access_log)和错误日志(error_log),其中传输日志和 cookie 日志被 Apache 2.0 以上的版本丢弃,所以本文不讨论这两种日志。当然,如果使用 SSL 服务的话,还可能存在 ssl_access_log、ssl_error_log 和 ssl_request_log 三种日志文件。
另外,值得注意的是:上述几种日志文件如果长度过大,还可能生成注入 access_log.1,error_log.2 等的额外文件,其格式与含义与上述几种文件相同,只不过系统自动为其进行命名而已。
日志相关的配置指令
Apache 中提供如下 4 条与日志相关的配置指令:
- ErrorLog 指令:用于指定错误日志的存放路径,使用语法为:ErrorLog 文件名;
- LogLevel:用于指定错误日志的错误登记,使用语法为:Loglevel 等级;
- LogFormat:用于为日志记录格式命名,使用语法为:LogFormat 记录格式说明字符串 格式称谓;
- CustomLog:用于指定访问日志存放路径和记录格式,指定访问日志由指定的程序生成并指定日志的记录格式,使用语法为:CustomLog 日志文件名 格式称谓。
在上述几个文件当中,除了 error_log 和 ssl_error_log 之外,所有日志文件以由 CustomLog 和 LogFormat 指令指定的格式生成。这些指令在 httpd.conf 文件中出现。使用 LogFormat 指令可以定义新的日志文件格式:
1
|
LogFormat “%h %l %u %t \ “%> %s %b “common
|
假定使用的是 common 日志格式或者 combined 日志格式,这两种格式都在默认的配置文件中定义。表 1 列出了 LogFormat 语句可以使用的变量:
表 1. LogFormat 语句的变量
1
2
3
4
5
6
7
8
9
10
|
//
//
The location and
format
of the access logfile(Common Logfile Format).
//
If you
do
not define any access logfiles within a
//
container, they will be logged here. Contrariwise,
if
you *
do
*
//
define per- access logfiles, transactions will be
//
logged therein and *not*
in
this
file
.
//
CustomLog logs
/access_log
common
ErrorLog logs
/error_log
|
日志记录等级和分类
一般说来,Apache 中的错误日志记录等级有如表 2 所示的八类:
表 2. 错误日志记录的等级
另外,在 Apache 中,将访问日志分为如下 4 类:
- 普通日志格式(common log format,CLF):大多数日志分析软件都支持这种格式,其在 LogFormat 指定中定义的昵称为 common;
- 参考日志格式(referer log format):记录客户访问站点的用户身份,其在 LogFormat 指定中定义的昵称为 referer;
- 代理日志格式(agent log format):记录请求的用户代理,其在 LogFormat 指定中定义的昵称为 agent;
- 综合日志格式(combined log format):即结合上述三种格式的日志信息,其在 LogFormat 指定中定义的昵称为 combined。
在实际的使用过程中,由于综合日志格式有效地结合了其他 3 种日志格式和信息,所以在配制访问日志时,可以有两种方式:
(1)分别使用 3 个文件进行分别记录,相应配置示例如下:
1
2
3
4
5
6
|
LogFormat “%h %l %u %t \ “%r\” %>s %b” common
LogFormat “%{Referer}i->%U” referer
LogFormat “%{Apache User-agent}i” agent
CustomLog logs
/access_log
common
CustomLog logs
/referer_log
referer
CustomLog logs
/agent_log
agent
|
(2)使用一个综合文件进行记录,相应配置示例如下:
1
2
3
|
LogFormat “%h %l %u %t \ “%r\” %>s %b \”%{Referer}i\” \
“%{Apache User-Agent}i\””combined
CustomLog logs
/access_log
combined
|