如何在 Apache 設定 (Custom Log) Access Log 與 Error Log

Subtitle

作者: 七味粉 |
文章2013-06-07
56 分享
6
    Apache Log 檔中包含了很多的資訊,包括哪個 IP 在存取你的網頁、哪個時間是尖峰時刻、用什麼瀏覽器來你的網站的、是直接存取還是間接存取你的網頁、等等狀態。
  1. 編輯 http.conf 檔

  2. 
    
    [shell]cd /etc/httpd/conf
    [shell]vim httpd.conf

  3. 找到根目錄及 LOG 位置

  4. 
    
    [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]


  5. 設定符合你的 LOG 檔

  6. 
    
    [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]

  7. LOG 格式

  8. 
    
    [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]

  9. 設定 VirtualHost 的 LOG

  10. 
    
    [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]

  11. 網站使用 CDN 的 LogFormat

  12. 
    
    LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" common
    [com]網站若使用CDN,access_log 記錄 ip 會有問題[/com]

  13. 小提醒

  14. CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/site1/access_log.%Y-%m-%d 1M" common
    # 是指 LOG 檔達到 1M 就進行 Log Rotate
  15. 常用參數說明:

  16. %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 點開始的時間
貼心小提醒
1.您目前未登入,若於此狀態進行文章收藏,紀錄可能無法長期保留。
2.建議登入後進行收藏,好文會幫你保存著,隨時要看也不怕找不到唷~

KiKiNote持續為您提供最優質的資訊內容,謝謝您!
不再顯示此視窗

複製成功