- Apache Log 檔中包含了很多的資訊,包括哪個 IP 在存取你的網頁、哪個時間是尖峰時刻、用什麼瀏覽器來你的網站的、是直接存取還是間接存取你的網頁、等等狀態。
- 編輯 http.conf 檔
- 找到根目錄及 LOG 位置
- 設定符合你的 LOG 檔
- LOG 格式
- 設定 VirtualHost 的 LOG
- 網站使用 CDN 的 LogFormat
- 小提醒
- 常用參數說明:
[shell]cd /etc/httpd/conf
[shell]vim httpd.conf
[vim]ServerRoot "/etc/httpd"[/vim][tell]Apache的根目錄位置[/tell]
[vim]ErrorLog "logs/error_log"[/vim][tell]Error Log所在位置[/tell]
[vim]CustomLog "logs/access_log common[/vim][tell]Access Log所在位置[/tell]
[com]如果原本是 "#" 註解掉的,記得要把它解開喔。[/com]
[vim]#CustomLog "logs/access_log" common[/vim][com]將這一行注解掉[/com]
[vim]CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/access_log.%Y-%m-%d 86400" common[/vim][com]將 rotatelogs 寫到你預設的 LOG 格式[/com]
[tell]access_log 命名結果為 access_log.2013-06-06[/tell]
[tell]86400 代表記錄一天[/tell]
[vim]LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined[/vim]
[vim]LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" common [/vim]
[tell]這邊可以設定你想要用的 log 格式[/tell]
[com]先把 http.conf 的 LOG 全部註解掉![/com]
[shell]cd /etc/httpd/conf.d
[shell]vim vh.conf
[vim]NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/html/site1
ServerName site1.com.tw
CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/site1/access_log.%Y-%m-%d 86400" common
ErrorLog "|/usr/sbin/rotatelogs /etc/httpd/logs/site1/error_log.%Y-%m-%d 86400"
<VirtualHost *:80>
DocumentRoot /var/www/html/site2
ServerName site2.com.tw
CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/site2/access_log.%Y-%m-%d 86400" common
ErrorLog "|/usr/sbin/rotatelogs /etc/httpd/logs/site2/error_log.%Y-%m-%d 86400"
[/vim]
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" common
[com]網站若使用CDN,access_log 記錄 ip 會有問題[/com]
CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/site1/access_log.%Y-%m-%d 1M" common
# 是指 LOG 檔達到 1M 就進行 Log Rotate
%a - 3-character weekday name (localized)
%B - full month name (localized)
%b - 3-character month name (localized)
%c - date and time (localized)
%d - 2-digit day of month
%H - 2-digit hour (24 hour clock)
%I - 2-digit hour (12 hour clock)
%j - 3-digit day of year
%M - 2-digit minute
%m - 2-digit month
%p - am/pm of 12 hour clock (localized)
%S - 2-digit second
%U - 2-digit week of year (Sunday first day of week)
%W - 2-digit week of year (Monday first day of week)
%w - 1-digit weekday (Sunday first day of week)
%X - time (localized)
%x - date (localized)
%Y - 4-digit year
%y - 2-digit year
%Z - time zone name
%% - literal
%s - 1970/01/01 00 點開始的時間