[精讚] [會員登入]
11625

APACHE的記錄檔格式 LogFormat 語法

在APACHE中有定義一些記錄的語法模版 在 /etc/httpd/conf/httpd.conf 中: LogForm

分享此文連結 //n.sfs.tw/10147

分享連結 APACHE的記錄檔格式 LogFormat 語法@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-25 11:09:59 最後編修
2016-11-04 01:24:46 By 張○○
 

自動目錄

在APACHE中有定義一些記錄的語法模版

在 /etc/httpd/conf/httpd.conf 中:

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

然後我們可以定義要寫入的記錄檔格式,預設是combined,例如預設的存取記錄為:

CustomLog logs/access_log combined

當然你可以把上面的"combined'" 改為你自訂的格式

我們也可以指定自己的網站記錄的格式,例如一個vh:

<VirtualHost *:80>
    ServerName n.sfs.tw
    ...
    CustomLog logs/z-access_log combined
</VirtualHost>

由於在 /etc/httpd 目錄中預設有一個logs的連結 (centos6)

下面的第5行,所以記錄檔路徑可指定 logs/your_log_name 就丟到 /var/log/httpd/之中,原理是這樣的

# ls -l /etc/httpd/
總計 8
drwxr-xr-x. 2 root root 4096 2016-11-04 01:22 conf
drwxr-xr-x. 2 root root 4096 2016-11-04 01:21 conf.d
lrwxrwxrwx. 1 root root   19 2016-09-25 22:57 logs -> ../../var/log/httpd
lrwxrwxrwx. 1 root root   29 2016-09-25 22:57 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx. 1 root root   19 2016-09-25 22:57 run -> ../../var/run/httpd

COMBINED 格式

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

他存起來的記錄長得像這樣:

59.126.205.242 - - [04/Jan/2009:23:30:57 +0800] "GET /main/images/login/login_05.png HTTP/1.1" 200 1355 "http://exmaple.com/i1.php?fun=login_show" "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 FirePHP/0.2.1"

說明如下:
1. 59.126.205.242 是客戶端IP
2. 第一個 '-' 是 %l 因為此網頁不必認證,所以logname 為空
3. 第二個 '-' 是 %u 因為此網頁不必認證,所以username 為空
4. [04/Jan/2009:23:30:57 +0800] 是時間
5. "GET /main/images/login/login_05.png HTTP/1.1" 為客戶端的 request
6. 200  %s 是狀態,%s 是取得最後的狀態,數字200代表 OK
7. 1355 排除掉 HTTP的headers後的長度(Bytes)
8. "http://example.com/main/i1.php?fun=login_show" 記錄 referer。也就是導到此request的頁面的呼叫者,這裡可以當來源分析。
9. "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 FirePHP/0.2.1" 是客戶端的瀏覽器狀態。

可使用的參數說明

%...a:          Remote IP-address
%...A:          Local IP-address
%...B:          Bytes sent, excluding HTTP headers.
%...b:          Bytes sent, excluding HTTP headers. In CLF format
        i.e. a '-' rather than a 0 when no bytes are sent.
%...c:          Connection status when response was completed.
                'X' = connection aborted before the response completed.
                '+' = connection may be kept alive after the response is sent.
                '-' = connection will be closed after the response is sent.
%...{FOOBAR}e:  The contents of the environment variable FOOBAR
%...f:          Filename
%...h:          Remote host
%...H       The request protocol
%...{Foobar}i:  The contents of Foobar: header line(s) in the request
                sent to the server.
%...l:          Remote logname (from identd, if supplied)
%...m       The request method
%...{Foobar}n:  The contents of note "Foobar" from another module.
%...{Foobar}o:  The contents of Foobar: header line(s) in the reply.
%...p:          The canonical Port of the server serving the request
%...P:          The process ID of the child that serviced the request.
%...q       The query string (prepended with a ? if a query string exists,
        otherwise an empty string)
%...r:          First line of request
%...s:          Status.  For requests that got internally redirected, this is
                the status of the *original* request --- %...>s for the last.
%...t:          Time, in common log format time format (standard english format)
%...{format}t:  The time, in the form given by format, which should
                be in strftime(3) format. (potentially localized)
%...T:          The time taken to serve the request, in seconds.
%...u:          Remote user (from auth; may be bogus if return status (%s) is 401)
%...U:          The URL path requested, not including any query string.
%...v:          The canonical ServerName of the server serving the request.
%...V:          The server name according to the UseCanonicalName setting.

參考資料

[1] http://httpd.apache.org/docs/1.3/mod/mod_log_config.html

 

END

你可能感興趣的文章

強迫網頁預設編碼語系 大部分的瀏覽器如果編碼設成「自動偵測」時都能正確的分析。但世界並不會這麼平順完美,明明我這樣設定utf8他就是用big5丟出來...

[Centos7] 解決資安的幾個問題 因為資安的關係,接到了一些弱點掃描出現的問題,解決方法做個整理。

[Apache] 自訂錯誤頁面及移掉歡迎頁 Apache 有一個歡迎頁,以正式網站來說,出現這頁有點不專業,該移除它..

[APACHE] 關於RewriteCond $1 RewriteCond $1的意義和解釋

[Apache] 利用.htaccess防盜圖 利用.htaccess防盜圖,同時也介紹一個產生防盜圖.htaccess檔的網站

APACHE的記錄檔格式 LogFormat 語法 在APACHE中有定義一些記錄的語法模版 在 /etc/httpd/conf/httpd.conf 中: LogForm

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

Smarty安裝 smarty 是著名的樣版引擎,非常的好用,用多了突然發現拿掉smarty反而不會寫php了,以下是安裝過程..

[PHP] 檢查檔案是否是圖檔 使用getimagesize函數檢查檔案是否是圖檔

讓radio box 或checkbox 好按 在行動裝置下,radio box及checkbox變得很難按得到

[Mysql] 資料型態int, float, double, text, char, varchar, blob大小 Mysql中資料型態int, float, text, char, varchar, blob大小

踩地雷公式解 由數學排列法演算出踩地雷公式解,主要有三種解:唯一解、歸納解、猜測