熱門分類
載入中…
目錄0%

🐧 Linux Proxy Server 流量記錄與 GoAccess 視覺化分析

    🐧 Linux Proxy Server 流量記錄與 GoAccess 視覺化分析

    本篇教你把 Squid Proxy 的流量紀錄(access log)轉成互動式圖表報表,快速看出 哪台用戶端流量最大、熱門網站、HTTP 狀態碼、尖峰時段。做法採「讓 Squid 以 Combined 格式輸出 → 交給 GoAccess 解析」的穩定路線;同時提供 即時看板(Real-Time HTML)每日靜態報表 兩種部署方式。

    📦 一、前置條件

    • 已可用的 Squid 代理主機(建議先完成:安裝、基本 ACL、啟動測試)。
    • 系統:Ubuntu/Debian/RHEL/CentOS/Alma/Rocky 皆可。
    • 權限:root 或 sudo。

    🧾 二、讓 Squid 產生「Combined」風格日誌

    GoAccess 對 COMBINED(Apache/Nginx 通用)支援最好;我們把 Squid 的 access.log 輸出成接近 Combined 的格式,後續解析最省力。

    編輯 /etc/squid/squid.conf(或對應路徑)
    # 1) 定義一個接近 Apache Combined 的 logformat
    #   %>a = client IP, %ui/%un = ident/user, %tl = local time,
    #   "%rm %ru HTTP/%rv" = method + URL + HTTP 版本
    #   %>Hs = 最終狀態碼, %<st = 傳送大小, Referer/User-Agent 取自 request header
    logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %>Hs %<st "%{Referer}>h" "%{User-Agent}>h"
    
    # 2) 以 combined 格式輸出到新檔(避免動到既有 access.log)
    access_log /var/log/squid/access_combined.log combined
    
    # (可留存原生格式作備援)
    # access_log /var/log/squid/access.log squid
    

    重新載入設定:

    sudo squid -k reconfigure
    # 或系統化
    sudo systemctl reload squid

    🛠 三、安裝 GoAccess

    Ubuntu / Debian
    sudo apt update
    sudo apt install -y goaccess
    RHEL / CentOS / Alma / Rocky(建議啟用 EPEL)
    sudo dnf install -y epel-release
    sudo dnf install -y goaccess

    🔍 四、快速試跑(終端版報表)

    # 以 COMBINED 格式解析 Squid 產生的新檔
    sudo goaccess /var/log/squid/access_combined.log \
      --log-format=COMBINED \
      --date-format=%d/%b/%Y --time-format=%T

    畫面將顯示總覽、熱門 URL、用戶端 IP、時段分佈、HTTP 狀態等。

    📊 五、輸出 HTML 報表(靜態檔)

    # 產生一次性 HTML 報表
    sudo mkdir -p /var/www/html/reports
    sudo goaccess /var/log/squid/access_combined.log \
      --log-format=COMBINED \
      --date-format=%d/%b/%Y --time-format=%T \
      -o /var/www/html/reports/proxy-report.html

    用瀏覽器開啟 http(s)://<你的伺服器>/reports/proxy-report.html 即可查看。

    📡 六、即時儀表板(Real-Time HTML)

    需要 WebSocket 服務持續餵資料。以下以「前端 Nginx / 後端 GoAccess」示例:

    1) 直接啟動 GoAccess 即時後端(常駐)
    sudo goaccess /var/log/squid/access_combined.log \
      --log-format=COMBINED \
      --date-format=%d/%b/%Y --time-format=%T \
      --real-time-html \
      -o /var/www/html/reports/proxy-rt.html \
      --daemonize
    2) (可選)Nginx 反向代理 WebSocket(若走 80/443 對外)
    # /etc/nginx/conf.d/goaccess.conf
    server {
      listen 80;
      server_name _;
    
      location /reports/ {
        root /var/www/html;
      }
    
      # GoAccess 內建 WS 預設 :7890(需依實際顯示調整)
      location /ws/ {
        proxy_pass http://127.0.0.1:7890/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
      }
    }
    sudo nginx -t && sudo systemctl reload nginx

    ⏰ 七、每日自動匯出(systemd 版)

    產出 每天一份靜態 HTML,利於留存與分享。

    建立 service 與 timer
    # /etc/systemd/system/goaccess-report.service
    [Unit]
    Description=Daily GoAccess Report (Squid Combined)
    After=network-online.target
    
    [Service]
    Type=oneshot
    ExecStart=/usr/bin/goaccess /var/log/squid/access_combined.log \
      --log-format=COMBINED \
      --date-format=%d/%b/%Y --time-format=%T \
      -o /var/www/html/reports/proxy-report-$(date +%%F).html
    
    # /etc/systemd/system/goaccess-report.timer
    [Unit]
    Description=Run GoAccess daily at 02:10
    
    [Timer]
    OnCalendar=*-*-* 02:10:00
    Persistent=true
    RandomizedDelaySec=90
    
    [Install]
    WantedBy=timers.target
    
    # 套用與啟用
    sudo systemctl daemon-reload
    sudo systemctl enable --now goaccess-report.timer
    systemctl list-timers --all | grep goaccess

    🗺 八、(可選)加上地理位置統計

    若編譯/套件支援 GeoIP2,可加入城市/國家維度。

    # 以 Debian/Ubuntu 為例
    sudo apt install -y geoipupdate
    # 取得 MaxMind 帳號/金鑰後設定 /etc/GeoIP.conf,再更新資料庫
    sudo geoipupdate
    
    # 指定資料庫(路徑依發行版不同)
    sudo goaccess /var/log/squid/access_combined.log \
      --log-format=COMBINED \
      --date-format=%d/%b/%Y --time-format=%T \
      --geoip-database=/usr/share/GeoIP/GeoLite2-City.mmdb \
      -o /var/www/html/reports/proxy-report-geo.html

    🧹 九、隱私與去識別化(分享前建議)

    • 去尾碼化 IP:分享前以最後一段遮罩(例:192.168.1.xxx)。
    • 排除內部網址:GoAccess 支援 --ignore-panel 或先行以 grep -v 過濾。
    • 保留原始檔:報表可公開,原始日誌僅留內部。
    範例:遮罩最後一段 IP 再餵給 GoAccess
    awk '{sub(/\.[0-9]+(\]| )/,".xxx"$1); print}' /var/log/squid/access_combined.log \
      > /var/log/squid/access_combined_masked.log
    
    goaccess /var/log/squid/access_combined_masked.log \
      --log-format=COMBINED -o /var/www/html/reports/proxy-report-masked.html

    🧯 十、常見錯誤排查

    • 時間/日期無法解析:確認 --date-format=%d/%b/%Y --time-format=%T 與日誌的 [10/Nov/2025:13:47:20 +0700] 風格一致。
    • 報表空空的:檢查 access_combined.log 是否有新行寫入,或是否使用了錯誤的檔名。
    • 即時報表無更新:瀏覽器 Console 看看 WS 是否被擋;必要時設定反代或開放 7890。
    • 權限問題:goaccess 能讀取 /var/log/squid,或以 sudo 執行。

    🧭 行動清單

    ✅ 調整 squid.conf 讓 access_combined.log 生成
    ✅ 安裝 GoAccess 並測試終端報表
    ✅ 產出 HTML 報表,放到 /var/www/html/reports/
    ✅ (可選)啟用 --real-time-html 即時看板
    ✅ (可選)建立 systemd timer 每日自動匯出
      

    📘 結語

    Combined 日誌格式 + GoAccess 的方式,能在不更動服務核心架構下,快速擁有可視化流量總覽;搭配 systemd 定時即時儀表板,日常維運與異常排查會更直覺。


    💬 互動留言引導

    這篇有幫到你嗎?使用的是 即時看板 還是 每日報表 呢?留言分享你的部署方式與遇到的坑,我會把常見問題整理到文末 FAQ!

    🔗 延伸閱讀

    — WWFandy・主題筆記

    🔗 分享這篇 LINE Facebook X

    沒有留言:

    字級