[轉貼]如何在 Apache 設定 (Custom Log) Access Log 與 Error Log

[轉貼]如何在 Apache 設定 (Custom Log) Access Log 與 Error Log

        Apache Log 檔中包含了很多的資訊,包括哪個 IP 在存取你的網頁、哪個時間是尖峰時刻、用什麼瀏覽器來你的網站的、是直接存取還是間接存取你的網頁、等等狀態。

    1. 編輯 http.conf 檔
[root@host ~]$ cd /etc/httpd/conf 
[root@host ~]$ vim httpd.conf
    1. 找到根目錄及 LOG 位置
ServerRoot "/etc/httpd" #Apache的根目錄位置 
ErrorLog "logs/error_log" #Error Log所在位置 
CustomLog "logs/access_log common #Access Log所在位置 
/* 如果原本是 "#" 註解掉的,記得要把它解開喔。 */
    1. 設定符合你的 LOG 檔
#CustomLog "logs/access_log" common /* 將這一行注解掉 */
CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/access_log.%Y-%m-%d 86400" common /* 將 rotatelogs 寫到你預設的 LOG 格式 */
#access_log 命名結果為 access_log.2013-06-06 
#86400 代表記錄一天 
    1. LOG 格式
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" common  
#這邊可以設定你想要用的 log 格式 
    1. 設定 VirtualHost 的 LOG
/* 先把 http.conf 的 LOG 全部註解掉! */
[root@host ~]$ cd /etc/httpd/conf.d
[root@host ~]$ vim vh.conf
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"
 
    1. 網站使用 CDN 的 LogFormat
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" common
/* 網站若使用CDN,access_log 記錄 ip 會有問題 */
    1. 小提醒

CustomLog “|/usr/sbin/rotatelogs /etc/httpd/logs/site1/access_log.%Y-%m-%d 1M” common
# 是指 LOG 檔達到 1M 就進行 Log Rotate

    1. 常用參數說明:

%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 點開始的時間

 

資料來源:http://kikinote.net/article/3780.html

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *