1. 程式人生 > >LAMP-Apache訪問日誌

LAMP-Apache訪問日誌

日誌文件

訪問日誌,記錄了網站訪問者的信息。在一些特殊情況,我們可以通過訪問日誌提取出我們需要的信息。如:網站受攻擊時發起源、統計訪問用戶大數據分布等。


一、網站訪問日誌

1、訪問日誌配置

[[email protected] ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "/data/www/abc.com"
    ServerName abc.com
    ServerAlias www.abc.com
    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^abc.com$
        RewriteRule ^/(.*)$ http://abc.com/$1 [R=301,L]
    </IfModule>
    ErrorLog "logs/abc.com-error_log"                ##錯誤日誌
    CustomLog "logs/abc.com-access_log" common       ##請求日誌
</VirtualHost>

2、查看訪問日誌

[[email protected] ~]# ls /usr/local/apache2.4/logs/
123.com-access_log  abc.com-access_log  access_log  httpd.pid
123.com-error_log   abc.com-error_log   error_log
[[email protected] ~]# tail /usr/local/apache2.4/logs/abc.com-access_log 
192.168.137.100 - - [21/Jul/2017:11:38:13 +0800] "GET HTTP://abc.com/ HTTP/1.1" 403 209
192.168.137.100 - - [21/Jul/2017:12:00:52 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 7
192.168.137.100 - - [21/Jul/2017:12:00:57 +0800] "GET HTTP://www.abc.com/ HTTP/1.1" 200 7
192.168.137.100 - - [21/Jul/2017:12:01:06 +0800] "GET HTTP://abcd.com/ HTTP/1.1" 200 7

▏日誌格式:

[[email protected] ~]# cat /usr/local/apache2.4/conf/httpd.conf | grep -i logformat
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common


3、將common更改成combined,顯示更為詳細的日誌格式

[[email protected] ~]# tail /usr/local/apache2.4/logs/abc.com-access_log 
192.168.137.100 - - [21/Jul/2017:11:38:13 +0800] "GET HTTP://abc.com/ HTTP/1.1" 403 209
192.168.137.100 - - [21/Jul/2017:12:00:52 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 7
192.168.137.100 - - [21/Jul/2017:12:00:57 +0800] "GET HTTP://www.abc.com/ HTTP/1.1" 200 7
192.168.137.100 - - [21/Jul/2017:12:01:06 +0800] "GET HTTP://abcd.com/ HTTP/1.1" 200 7
192.168.137.1 - - [21/Jul/2017:18:07:04 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
192.168.137.1 - - [21/Jul/2017:18:07:06 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/8.0; .NET4.0C; .NET4.0E)"
192.168.137.1 - - [21/Jul/2017:18:07:22 +0800] "GET /1.txt HTTP/1.1" 404 203 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/8.0; .NET4.0C; .NET4.0E)"

默認情況下log日誌格式為:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

其中%h 是記錄訪問者的IP,如果在web的前端有一層代理,那麽這個%h其實就是代理機器的IP,這不是我們想要的。

在這種情況下,%{X-FORWARDED-FOR}i 字段會記錄客戶端真實的IP

▏所以log日誌改為:

LogFormat "%h %{X-FORWARDED-FOR}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined


二、不記錄靜態文件

訪問日誌記錄的大量垃圾靜態文件(如圖片、css、js等)訪問信息,影響運維工作效率。

1、配置虛擬主機

[[email protected] ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 
<VirtualHost *:80>
    DocumentRoot "/data/www/abc.com"
    ServerName abc.com
    ServerAlias www.abc.com
    ErrorLog "logs/abc.com-error_log"
    SetEnvIf Request_URI ".*\.gif$" img
    SetEnvIf Request_URI ".*\.jpg$" img
    SetEnvIf Request_URI ".*\.png$" img
    SetEnvIf Request_URI ".*\.bmp$" img
    SetEnvIf Request_URI ".*\.swf$" img
    SetEnvIf Request_URI ".*\.js$" img
    SetEnvIf Request_URI ".*\.css$" img   ##把以gif,jpg等結尾的全部標記為img
    CustomLog "logs/abc.com-access_log" combined env=!img ##除了img文件,全部都記錄
</VirtualHost>

2、檢查重新加載

[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful

3、驗證效果

[[email protected] ~]# curl -x127.0.0.1:80 abcd.com/123.php -I
HTTP/1.1 404 Not Found
Date: Fri, 21 Jul 2017 10:20:04 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

[[email protected] ~]# curl -x127.0.0.1:80 abcd.com/123.gif -I
HTTP/1.1 404 Not Found
Date: Fri, 21 Jul 2017 10:20:10 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

[[email protected] ~]# curl -x127.0.0.1:80 abcd.com/123.png -I
HTTP/1.1 404 Not Found
Date: Fri, 21 Jul 2017 10:20:17 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

[[email protected] ~]# tail /usr/local/apache2.4/logs/abc.com-access_log 
192.168.137.100 - - [21/Jul/2017:11:38:13 +0800] "GET HTTP://abc.com/ HTTP/1.1" 403 209
192.168.137.100 - - [21/Jul/2017:12:00:52 +0800] "GET HTTP://abc.com/ HTTP/1.1" 200 7
192.168.137.100 - - [21/Jul/2017:12:00:57 +0800] "GET HTTP://www.abc.com/ HTTP/1.1" 200 7
192.168.137.100 - - [21/Jul/2017:12:01:06 +0800] "GET HTTP://abcd.com/ HTTP/1.1" 200 7
192.168.137.1 - - [21/Jul/2017:18:07:04 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
192.168.137.1 - - [21/Jul/2017:18:07:06 +0800] "GET / HTTP/1.1" 200 7 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/8.0; .NET4.0C; .NET4.0E)"
192.168.137.1 - - [21/Jul/2017:18:07:22 +0800] "GET /1.txt HTTP/1.1" 404 203 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; WOW64; Trident/8.0; .NET4.0C; .NET4.0E)"
127.0.0.1 - - [21/Jul/2017:18:20:04 +0800] "HEAD HTTP://abcd.com/123.php HTTP/1.1" 404 - "-" "curl/7.29.0"   ##可以看到過濾後的效果了


三、訪問日誌切割

網站每天都在被訪問,會記錄大量的日誌信息。單個大日誌文件不方便運維人員的查看,而且還不方便清理過期日誌。通過切割日誌能很好的解決這個問題。

1、配置虛擬主機

[[email protected] ~]# cat /usr/local/apache2.4/conf/extra/httpd-vhosts.conf | grep -i customlog 
    CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/abc.com-access_%Y%m%d.log 86400" combined env=!img  ##工具名為:rotatelogs;-l:以當前系統時間為基準

2、檢查重新加載

[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful

3、測試效果

[[email protected] ~]# curl -x127.0.0.1:80 abcd.com/123.php -I
HTTP/1.1 404 Not Found
Date: Fri, 21 Jul 2017 10:32:07 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

[[email protected] ~]# ls  /usr/local/apache2.4/logs/
123.com-access_log  abc.com-access_20170721.log  abc.com-error_log  error_log
123.com-error_log   abc.com-access_log           access_log         httpd.pid

可以看到生成了一個新的日誌文件,後期可以制作一個任務計劃,刪除過期的日誌文件,以防磁盤空間不足。


本文出自 “Gorilla Grodd” 博客,請務必保留此出處http://juispan.blog.51cto.com/943137/1952820

LAMP-Apache訪問日誌