2026年5月20日
2026年5月20日
WordPressのGzip圧縮を設定する方法
はじめに
Gzip圧縮はHTML・CSS・JavaScriptなどのテキストリソースを転送前に圧縮し、ファイルサイズを60〜80%削減します。サーバー側で設定するだけで全ページに適用され、ページ読み込み速度を大幅に改善できます。
症状・原因
- PageSpeed Insightsで「テキスト圧縮を有効にする」を指摘される
- HTMLやCSSのダウンロードサイズが大きい
- レンタルサーバーでGzipが無効になっている
- Content-Encoding: gzip ヘッダーが返ってきていない
解決手順
ステップ1:Gzip圧縮が有効かを確認する
# curlでContent-Encodingヘッダーを確認
curl -H "Accept-Encoding: gzip" -I https://example.com/ | grep -i content-encoding
# Content-Encoding: gzip ← 有効
# (空欄) ← 無効
# レスポンスサイズの比較
curl -H "Accept-Encoding: gzip" -s -o /dev/null -w "%{size_download}" https://example.com/
curl -s -o /dev/null -w "%{size_download}" https://example.com/
# 圧縮ありの方が小さければGzip有効
# オンラインツールでも確認可能
# Check GZIP compression: https://www.giftofspeed.com/gzip-test/
ステップ2:Apacheでmod_deflateを設定する
# .htaccess に追加
<IfModule mod_deflate.c>
# テキスト系ファイルを圧縮
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom+xml
AddOutputFilterByType DEFLATE image/svg+xml
# 古いブラウザ対策(gzipが壊れる場合の除外)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# プロキシキャッシュを正しく動作させる
Header append Vary Accept-Encoding
</IfModule>
# Brotli(mod_brotliが利用可能な場合)
<IfModule mod_brotli.c>
AddOutputFilterByType BROTLI_COMPRESS text/html text/css application/javascript
BrotliCompressionQuality 6
</IfModule>
ステップ3:Nginxでgzip圧縮を設定する
# /etc/nginx/nginx.conf または /etc/nginx/conf.d/gzip.conf
http {
# Gzip有効化
gzip on;
gzip_vary on; # Vary: Accept-Encoding ヘッダーを付与
gzip_proxied any; # プロキシ経由のリクエストも圧縮
gzip_comp_level 6; # 圧縮レベル(1-9、6が速度とサイズのバランス)
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256; # 256バイト以上のファイルのみ圧縮
# 圧縮するMIMEタイプ
gzip_types
text/plain
text/css
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/rss+xml
application/atom+xml
image/svg+xml
font/truetype
font/opentype
application/vnd.ms-fontobject;
# Brotli(ngx_brotliモジュールが必要)
brotli on;
brotli_comp_level 6;
brotli_types text/html text/css application/javascript;
}
ステップ4:PHPでGzip圧縮を有効化する(最終手段)
// functions.php(サーバー設定が変更できない場合)
function mytheme_enable_gzip(): void {
// ob_gzhandler はサーバー側のgzipと競合する可能性あり
// サーバー設定で解決できない場合の最終手段として使用
if (!headers_sent() && extension_loaded('zlib')) {
ob_start('ob_gzhandler');
}
}
// add_action('init', 'mytheme_enable_gzip');
// ※ サーバー設定での対応を優先すること
// wp-config.php: WordPressの組み込みgzipを有効化
// (wp_compress_scripts / wp_compress_styles)
// これはWordPressのキャッシュ機能であり、転送圧縮とは別物
define('COMPRESS_SCRIPTS', true);
define('COMPRESS_CSS', true);
define('ENFORCE_GZIP', true);
ステップ5:静的ファイルを事前圧縮する(.gz/.brファイル)
# .gz ファイルを事前生成(Nginxのgzip_staticモジュール用)
gzip -9 -k wp-content/themes/mytheme/assets/css/style.min.css
gzip -9 -k wp-content/themes/mytheme/assets/js/main.min.js
# Brotliファイルを事前生成
brotli -9 wp-content/themes/mytheme/assets/css/style.min.css
# Nginxでgzip_staticを有効化
# nginx.conf に: gzip_static on;
# nginx.conf: 事前圧縮ファイルを優先配信
server {
location ~* \.(css|js|html|svg)$ {
gzip_static on; # .gz ファイルを優先して配信
brotli_static on; # .br ファイルを優先して配信
expires 30d;
add_header Cache-Control "public, immutable";
}
}
注意事項
- 画像(JPEG・PNG・WebP)・動画・ZIPファイルはすでに圧縮済みのため、Gzip対象から除外してください。圧縮しても効果がなく、CPUを無駄に消費します
ob_gzhandlerをWordPressで使う場合、WordPressのzlib.output_compressionと競合する可能性があります。php.iniの設定を確認してください- CDNを使用している場合、CDN側でもGzip/Brotliが有効になっているか確認してください
まとめ
ApacheはHTMLタグを .htaccess の mod_deflate に追加するだけでGzipが有効になります。NginxはHTTPブロックに gzip on と gzip_types を設定します。圧縮レベルは6が速度とサイズのバランスが良い値です。curl -H "Accept-Encoding: gzip" -I で Content-Encoding: gzip が返ればGzipは正常に動作しています。