Apache日誌提供有助於檢測伺服器常見問題的詳細資訊。要記錄網站的訪問日誌,必須啟用mod_log_configmodule
。
apache配置檔中有三個指令,即 -
TransferLog
:創建日誌檔。LogFormat
:指定自定義格式。CustomLog
:創建和格式化日誌檔。
TransferLog
指令在apache配置檔中可用,它根據設置參數輪轉虛擬主機日誌檔。
<VirtualHost www.example.com>
ServerAdmin webmaster@xuhuhu.com
DocumentRoot /usr/www/example/httpd/htdocs/
ServerName www.example.com
ServerAlias example.com www.example
ErrorLog /usr/www/example/httpd/logs/error_log
TransferLog /usr/www/example/httpd/logs/accesslog
CustomLog /usr/www/example/httpd/logs/accesslog combined
</VirtualHost>
兩種類型的Apache日誌格式
- 通用日誌格式
- 組合日誌格式
可以通過編輯apache配置檔來啟用它們,即apache2.conf
(Debian/ubuntu)或httpd.conf
(基於rpm的系統)檔。
通用日誌格式
LogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog logs/access_log.log common
Apache生成的通用日誌內容示例如下 -
[Wed Oct 11 14:32:52 2000] [error] [client 127.0.0.1] client denied by server configuration: /export/home/live/ap/htdocs/test
組合日誌格式
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
CustomLog log/access_log.log combined
在上面格式中,
%h
是遠程主機%l
是由identd確定的用戶的身份%u
是HTTP身份驗證確定的用戶名%t
是伺服器處理完請求的時間。%r
是來自客戶端的請求行(“GET/HTTP/1.0”)。%> s
是從伺服器發送到客戶端的狀態代碼(500,404等)%b
是客戶端回應的大小(以位元組為單位)Referer
是鏈接到此URL的頁面。
用戶代理是流覽器標識字串。
Apache生成的組合日誌:
199.180.11.91 - - [06/Mar/2019:04:22:58 +0100] "GET /robots.txt HTTP/1.1" 404 1228 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)"
自定義日誌為伺服器上的每個虛擬主機創建單獨的日誌檔。它需要在配置檔的虛擬主機部分中指定。
可以看到下麵提到的虛擬主機配置,生成的日誌將為該虛擬主機自定義,並且將組合格式。
上一篇:
Apache Web伺服器安全
下一篇:
配置Web伺服器過程