GoAccess 是一個開源的即時 Web 日誌分析工具,可以在終端機直接分析 access log,也能產生 HTML 報告。這篇整理常用指令和設定。
什麼是 GoAccess?
GoAccess 是用 C 語言撰寫的高效能日誌分析工具,特色是:
- 不需要資料庫,直接讀取 log 檔分析
- 支援即時(realtime)串流分析
- 可輸出終端機互動介面、靜態 HTML 報告或 JSON/CSV
- 支援 Apache、Nginx、Amazon S3、CloudFront 等多種日誌格式
安裝
1
2
3
4
5
6
7
8
|
# Ubuntu/Debian
sudo apt-get install goaccess
# CentOS/RHEL
sudo yum install goaccess
# macOS(Homebrew)
brew install goaccess
|
常用指令
基本分析(輸出 HTML 報告)
1
|
goaccess access_log.log -o report.html --log-format=COMBINED
|
在終端機互動查看
1
|
goaccess access_log.log --log-format=COMBINED
|
執行後會進入互動式 TUI 介面,可用鍵盤上下瀏覽各統計區塊。按 q 離開。
即時分析(串流模式)
1
|
tail -f /var/log/nginx/access.log | goaccess --log-format=COMBINED -
|
GoAccess 內建幾種常用格式:
| 格式名稱 |
適用場景 |
COMBINED |
Apache / Nginx 組合日誌格式(最常用) |
COMMON |
Apache Common Log 格式 |
W3C |
IIS W3C 格式 |
CLOUDFRONT |
Amazon CloudFront 日誌 |
CLOUDSTORAGE |
Google Cloud Storage 日誌 |
1
2
3
4
5
6
7
8
|
# Nginx 預設格式(COMBINED)
goaccess access.log --log-format=COMBINED -o report.html
# Apache Common 格式
goaccess access.log --log-format=COMMON -o report.html
# JBoss / Tomcat access log(通常是 COMBINED 格式)
goaccess access_log.log --log-format=COMBINED -o report.html
|
如果 log 格式不是標準格式,可以自訂:
1
2
3
4
5
|
goaccess access.log \
--log-format='%h %^[%d:%t %^] "%r" %s %b "%R" "%u"' \
--date-format='%d/%b/%Y' \
--time-format='%H:%M:%S' \
-o report.html
|
使用設定檔
GoAccess 支援設定檔,避免每次都要輸入長指令:
1
2
3
4
5
|
# 下載官方設定範本
wget https://raw.githubusercontent.com/allinurl/goaccess/master/config/goaccess.conf
# 編輯設定檔
vi goaccess.conf
|
在 goaccess.conf 中指定格式:
1
2
3
|
time-format %H:%M:%S
date-format %d/%b/%Y
log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
|
然後使用設定檔執行:
1
|
goaccess access.log --config-file=/path/to/goaccess.conf -o report.html
|
常用選項
| 選項 |
說明 |
-o report.html |
輸出 HTML 報告 |
--real-time-html |
產生即時更新的 HTML 報告 |
--log-format=COMBINED |
指定日誌格式 |
--ignore-crawlers |
忽略爬蟲流量 |
--exclude-ip=x.x.x.x |
排除特定 IP |
--output=json |
輸出 JSON 格式 |
--no-global-config |
不讀取全域設定檔 |
HTML 報告包含的統計資訊
GoAccess 產生的 HTML 報告涵蓋:
- 每日獨立訪客(Unique Visitors)、請求數、頻寬
- 最多請求的 URL(Requested Files)
- HTTP 狀態碼分布(404、500 等)
- 訪客來源國家(Geo Location)
- 瀏覽器和作業系統統計
- 請求來源 IP 排行
參考資料