Nginxログ解析ツール「GoAccess」導入ガイド:GA4を捨ててCore Web Vitalsを爆速化!

この記事は約23分で読めます。
この記事が役立ったらブックマーク! あとで読み返したり、環境構築時のリファレンスに活用できます
B! はてなブックマークに追加

Google Analytics(GA4)を導入したものの、「JavaScriptタグの読み込みでページ表示が遅くなる」「レポートが重くて使いにくい」「Core Web Vitalsのスコアを少しでも上げたい」とお悩みではありませんか?
本記事では、外部の計測タグを一切使わず、Nginxのアクセスログと解析ツール「GoAccess」を組み合わせて完全自前のリアルタイム解析環境を構築する方法を解説します。
さらに、ログの保存先をRAMディスク(tmpfs)に配置することで、SSDへの書き込み負荷(TBW)を完全にゼロにしつつ、ヌルヌル動く爆速ダッシュボードを実現します!

1. なぜ今「脱GA4」なのか?サーバーログ解析のメリット

GA4をはじめとするサードパーティ製アクセス解析から、GoAccessによるサーバーログ解析に移行することで、以下の大きなメリットが得られます。

  • Core Web Vitalsの最大化(遅延0ミリ秒) ブラウザ側でJavaScriptを実行する必要がないため、INPやLCPなどのパフォーマンス指標に一切悪影響を与えません。
  • 計測漏れ・ブロックの回避 AdBlockなどのコンテンツブロッカーやCookie規制(ITP)の影響を受けず、サーバーに届いた正確なアクセス数を把握できます。
  • 圧倒的なレスポンスとシンプルなUI GA4の重いレポート画面とはサヨナラ。知りたい情報(アクセス数、転送量、OS/ブラウザ、404エラーログなど)が直感的に一目で分かります。
  • 完全なプライバシーとデータ所有権 アクセスデータを外部企業に送信せず、自宅サーバー内だけで完結・保持できます。

2. システム構成:RAMディスク(tmpfs)でSSD寿命も保護

一般的なログ解析ではSSDへの頻繁な書き込み(I/O)が発生しますが、今回はRAMディスク(/mnt/ramdisk/)上にログを保持してGoAccessに読み込ませます。

Plaintext
[ ユーザーのアクセス ]
        │
        ▼
   [ Nginx ] ──(ログ出力)──▶ [ RAMディスク (/mnt/ramdisk/logs/nginx/) ]
                                        │
                                        ▼ (リアルタイム解析)
                                  [ GoAccess ]
                                        │
                                        ▼ (WebSocket / HTML)
                            [ リアルタイムダッシュボード ]
Plaintext
[ User Request ]
        │
        ▼
   [ Nginx ] ──(Write Log)──▶ [ RAM Disk (/mnt/ramdisk/logs/nginx/) ]
                                        │
                                        ▼ (Real-Time Parsing)
                                  [ GoAccess ]
                                        │
                                        ▼ (WebSocket / HTML)
                            [ Real-Time Dashboard ]

この構成により、「SSDへのログ書き込みゼロ」と「GoAccessの超高速ログ読み込み」を同時に達成できます。

3. GoAccessのインストールとNginxログ設定

STEP 1: GoAccessのインストール

Debian / Ubuntu環境では、公式リポジトリから最新版をインストールします。

Bash
# 公式キーとリポジトリの追加
wget -O - https://deb.goaccess.io/gnugpg.key | gpg --dearmor | sudo tee /usr/share/keyrings/goaccess.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/goaccess.gpg] https://deb.goaccess.io/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/goaccess.list

# インストール
sudo apt update
sudo apt install goaccess -y
Bash
# Add official GPG key and repository
wget -O - https://deb.goaccess.io/gnugpg.key | gpg --dearmor | sudo tee /usr/share/keyrings/goaccess.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/goaccess.gpg] https://deb.goaccess.io/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/goaccess.list

# Install GoAccess
sudo apt update
sudo apt install goaccess -y

STEP 2: Nginxのログフォーマット(log_format)調整

ページの処理速度(レスポンスタイム)も測定できるよう、Nginxの設定に $request_time を含めたログフォーマットを定義します。

/etc/nginx/nginx.conf の http ブロック内の## Logging Settings から ## Virtual Host Configs の直前までを、以下のように書き換えます。

Nginx
  ##
	# Logging Settings
	##

	# レスポンスタイム($request_time)を含めた GoAccess 用のカスタムログフォーマット
	log_format custom_perf '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
	                       '$status $body_bytes_sent "$http_referer" '
	                       '"$http_user_agent" $request_time';

	# RAMディスクへログを出力(バッファリングを有効化して超高速化)
	access_log /mnt/ramdisk/logs/nginx/access.log custom_perf buffer=16k flush=5s;
	error_log  /mnt/ramdisk/logs/nginx/error.log warn;

	# Log format Settings
#	log_format rt_cache '$remote_addr $upstream_response_time $upstream_cache_status [$time_local] '
#	'$host "$request" $status $body_bytes_sent '
#	'"$http_referer" "$http_user_agent" "$server_protocol" "$http3"';
# 【変更後】先頭のIP取得元を「X-Forwarded-For」に明示的に書き換える
	log_format rt_cache '$http_x_forwarded_for $upstream_response_time $upstream_cache_status [$time_local] '
	                    '$host "$request" $status $body_bytes_sent '
	                    '"$http_referer" "$http_user_agent" "$server_protocol" "$http3"';
Nginx
  ##
	# Logging Settings
	##

	# Custom log format including response time for GoAccess
	log_format custom_perf '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
	                       '$status $body_bytes_sent "$http_referer" '
	                       '"$http_user_agent" $request_time';

	# Log output path on RAM Disk (Enable buffering for faster I/O)
	access_log /mnt/ramdisk/logs/nginx/access.log custom_perf buffer=16k flush=5s;
	error_log  /mnt/ramdisk/logs/nginx/error.log warn;

	# Log format Settings
#	log_format rt_cache '$remote_addr $upstream_response_time $upstream_cache_status [$time_local] '
#	'$host "$request" $status $body_bytes_sent '
#	'"$http_referer" "$http_user_agent" "$server_protocol" "$http3"';
	log_format rt_cache '$http_x_forwarded_for $upstream_response_time $upstream_cache_status [$time_local] '
	                    '$host "$request" $status $body_bytes_sent '
	                    '"$http_referer" "$http_user_agent" "$server_protocol" "$http3"';

設定後、Nginxを再読み込みします。

Nginx
sudo nginx -t
sudo systemctl reload nginx

4. リアルタイム・ダッシュボードの構築と自動化

GoAccessには、アクセスログを監視してリアルタイム更新されるHTMLレポート(WebSocket対応)を出力する機能があります。
systemd コマンド内でログフォーマットの %(パーセント記号)が環境変数として誤認識されるトラブルを防ぐため、専用の設定ファイル(goaccess.conf)にログフォーマットを切り離して管理するのが最も確実で安全な構成です。

STEP 1: GoAccess設定ファイル(goaccess.conf)の作成

/etc/goaccess/goaccess.conf を作成し、Nginxで定義したログフォーマットと一致する設定を記述します。

Bash
sudo nano /etc/goaccess/goaccess.conf

ファイル内に以下を記述して保存します。

Plaintext
# ログフォーマットと日付フォーマットの定義 (Log and date format definitions)
time-format %H:%M:%S
date-format %d/%b/%Y
log-format %h %^ %u [%d:%t %^] "%r" %s %b "%R" "%u" %L

STEP 2: Nginxにダッシュボード用ルーティングとアクセス制限を定義

WordPress環境で /stats.html や WebSocket通信(/ws)にアクセスした際、404エラーになるのを防ぐため、サイトの設定ファイル(server ブロック内)に専用の location を追記します。

併せて、自環境のIPアドレスのみを許可するIP制限を設定し、ダッシュボードを保護します。

Nginx
# GoAccess リアルタイムダッシュボード用設定
location /stats.html {
    alias /var/www/html/stats.html;
    access_log off;

    # IPアドレス制限(許可するIPを指定)
    allow 192.168.1.0/24;
    allow 153.156.79.0/24;
    allow 2001:db8:1234::/48;
    deny all;
}

# WebSocket接続用設定(リアルタイム更新用)
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";
    proxy_set_header Host $host;

    # WebSocket接続側にも同じIP制限を適用
    allow 192.168.1.0/24;
    allow 153.156.79.0/24;
    allow 2001:db8:1234::/48;
    deny all;
}
Nginx
# GoAccess Real-Time Dashboard Settings
location /stats.html {
    alias /var/www/html/stats.html;
    access_log off;

    # IP Address Restriction (Specify allowed IPs)
    allow 192.168.1.0/24;
    allow 153.156.79.0/24;
    allow 2001:db8:1234::/48;
    deny all;
}

# WebSocket Connection Settings (For real-time updates)
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";
    proxy_set_header Host $host;

    # Apply the same IP restrictions to the WebSocket connection
    allow 192.168.1.0/24;
    allow 153.156.79.0/24;
    allow 2001:db8:1234::/48;
    deny all;
}

設定を追加したら構文チェックを行い、Nginxを再読み込みします。

Bash
sudo nginx -t
sudo systemctl reload nginx

STEP 3: systemdでバックグラウンド常駐・自動起動化

サーバー起動時にGoAccessが自動でログ監視とダッシュボード更新を開始するよう、systemd サービスを作成します。

/etc/systemd/system/goaccess.service を作成します。

Bash
sudo nano /etc/systemd/system/goaccess.service

以下の内容を記述します。先ほど作成した goaccess.conf を読み込ませることで、シンプルかつ堅牢に動作します。

INI
[Unit]
Description=GoAccess Real-Time Web Log Analytics
After=network.target local-fs.target

[Service]
Type=simple
User=www-data
ExecStart=/usr/bin/goaccess /mnt/ramdisk/logs/nginx/access.log \
  --config-file=/etc/goaccess/goaccess.conf \
  -o /var/www/html/stats.html \
  --real-time-html \
  --ws-url=wss://your-domain.com:443/ws \
  --port=7890
Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target

サービスの有効化と起動を行います。

Bash
# サービスの有効化と起動 (Reload systemd, enable and start the service)
sudo systemctl daemon-reload
sudo systemctl enable --now goaccess

# ステータス確認 (Check service status)
sudo systemctl status goaccess

5. まとめ:脱GA4で得られた結果

GA4のJavaScriptタグを削除し、GoAccessによるログ解析へ一本化したことで、サイト全体のパフォーマンスは劇的に向上しました。

  • PageSpeed Insightsのパフォーマンススコアが向上(特にINPの改善)
  • RAMディスク運用により、1日あたり数GBにおよぶSSD書き込みを削減
  • アクセス解析画面が瞬時に開き、エラーログや404リクエストの特定が容易に

「WEBサイトの表示速度を極限まで速くしたい」「プライバシー配慮やサーバー負荷低減を突き詰めたい」という方は、ぜひ本構成を試してみてください!

コメント

タイトルとURLをコピーしました