当前位置 主页 > 服务器问题 > Linux/apache问题 >

    apache日志文件详解和实用分析命令

    栏目:Linux/apache问题 时间:2018-09-27 14:04

    这篇文章主要介绍了apache日志文件每条数据的请意义,以及一些实用日志分析命令,需要的朋友可以参考下

    一.日志分析
    如果apache的安装时采用默认的配置,那么在/logs目录下就会生成两个文件,分别是access_log和error_log
    1).access_log
    access_log为访问日志,记录所有对apache服务器进行请求的访问,它的位置和内容由CustomLog指令控制,LogFormat指令可以用来简化该日志的内容和格式
    例如,我的其中一台服务器配置如下:
    复制代码 代码如下:
    CustomLog "| /usr/sbin/rotatelogs /var/log/apache2/%Y_%m_%d_other_vhosts_access.log 86400 480" vhost_combined


    -rw-r--r-- 1 root root 22310750 12-05 23:59 2010_12_05_other_vhosts_access.log
    -rw-r--r-- 1 root root 26873180 12-06 23:59 2010_12_06_other_vhosts_access.log
    -rw-r--r-- 1 root root 26810003 12-07 23:59 2010_12_07_other_vhosts_access.log
    -rw-r--r-- 1 root root 24530219 12-08 23:59 2010_12_08_other_vhosts_access.log
    -rw-r--r-- 1 root root 24536681 12-09 23:59 2010_12_09_other_vhosts_access.log
    -rw-r--r-- 1 root root 14003409 12-10 14:57 2010_12_10_other_vhosts_access.log

    #通过CustomLog指令,每天一天生成一个独立的日志文件,同时也写了定时器将一周前的日志文件全部清除,这样可以显得更清晰,既可以分离每一天的日志又可以清除一定时间以前的日志通过制,LogFormat定义日志的记录格式

    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedproxy
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    LogFormat "%{Referer}i -> %U" referer
    LogFormat "%{User-agent}i" agent

    随意的tail一个access_log文件,下面是一条经典的访问记录
    复制代码 代码如下:
    218.19.140.242 - - [10/Dec/2010:09:31:17 +0800] "GET /query/trendxml/district/todayreturn/month/2009-12-14/2010-12-09/haizhu_tianhe.xml HTTP/1.1" 200 1933 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)"

    一共是有9项,将他们一一拆开来说明:
    复制代码 代码如下:
    218.19.140.242
    -
    -
    [10/Dec/2010:09:31:17 +0800]
    "GET /query/trendxml/district/todayreturn/month/2009-12-14/2010-12-09/haizhu_tianhe.xml HTTP/1.1"
    200
    1933
    "-"
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)"

    1)  218.19.140.242 这是一个请求到apache服务器的客户端ip,默认的情况下,第一项信息只是远程主机的ip地址,但我们如果需要apache查出主机的名字,可以将 HostnameLookups设置为on,但这种做法是不推荐使用,因为它大大的减缓了服务器.另外这里的ip地址不一定就是客户主机的ip地址,如果 客户端使用了代理服务器,那么这里的ip就是代理服务器的地址,而不是原机.
    2) - 这一项是空白,使用"-"来代替,这个位置是用于标注访问者的标示,这个信息是由identd的客户端存在,除非IdentityCheck为on,非则apache是不会去获取该部分的信息(ps:不太理解,基本上这一项都是为空,奉上原文)
    The "hyphen" in the output indicates that the requested piece of information is not available. In this case, the information that is not available is the RFC 1413 identity of the client determined by identd on the clients machine. This information is highly unreliable and should almost never be used except on tightly controlled internal networks. Apache httpd will not even attempt to determine this information unless IdentityCheck is set to On.