LoginSignup
8
9

More than 5 years have passed since last update.

index.html を gzip 圧縮して Apache に設置

Posted at

Apache に設置するコンテンツを gzip 圧縮することで、どのくらい負荷が軽くなるものか試してみた。今回は index.html を圧縮してみたが、当然JSやCSS等もイケる(※)。実験サーバ環境は諸事情により非公開。

※画像もgzip圧縮できなくもないが、既に画像フォーマットそのもので圧縮がかかっているので意味が無い。テキスト形式のファイルのみ圧縮をかけるのが一般的。

測定に用いるコマンド

$ ab -n 100 -c 10 -H "Accept-Encoding: gzip,deflate" http://xxxx/

リクエスト数100、同時接続数10。圧縮コンテンツを受け付けることをサーバに伝えるため、Accept-Encoding ヘッダを付与する。

圧縮前

ファイル構成

  • web_folder
    • index.html … 1MBのテキスト (とりあえず円周率100万桁)
    • .htaccess

.htaccess

.htaccess
DirectoryIndex index.html

測定結果

$ ab -n 100 -c 10 -H "Accept-Encoding: gzip,deflate" http://xxxx/
Time taken for tests:   9.524 seconds
Total transferred:      112207900 bytes

リアルタイム圧縮

コンテンツはそのまま設置するだけなので楽。Apacheが圧縮責務を負うため、ややCPU負荷が上がる。

ファイル構成

  • web_folder
    • index.html … Apacheが勝手に圧縮してくれるため、設置するファイルは無加工.
    • .htaccess

httpd.conf

httpd.conf
# 以下2行が必要.
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so

.htaccess

.htaccess
DirectoryIndex index.html

SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|ico)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary

測定結果

$ ab -n 100 -c 10 -H "Accept-Encoding: gzip,deflate" http://xxxx/
Time taken for tests:   5.616 seconds
Total transferred:      54302600 bytes

円周率というランダムなコンテンツのため、それほど圧縮率は良くない。容量が半分になる程度。総時間も半分近くになった。

事前圧縮

コンテンツをあらかじめ圧縮しておくことでApacheの圧縮責務がなくなるため、負荷は当然軽くなる。

ファイル構成

  • web_folder
    • index.html.gz … あらかじめ gzip コマンドで圧縮しておく.
    • .htaccess

htaccess.conf

htaccess.conf
# 普通は既に有効になってるはずだが、一応これが必要.
LoadModule rewrite_module modules/mod_rewrite.so

.htaccess

.htaccess
DirectoryIndex index.html.gz

RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule .+ %{REQUEST_URI}.gz

<FilesMatch "\.html\.gz$">
    ForceType text/html
    AddEncoding x-gzip .gz
</FilesMatch>

測定結果

$ ab -n 100 -c 10 -H "Accept-Encoding: gzip,deflate" http://xxxx/
Time taken for tests:   4.731 seconds
Total transferred:      54302800 bytes

リアルタイム圧縮に比べると圧縮フェーズが無いため当然軽い。

以上

CPU負荷値を計測していないので雑だが、処理時間を見る限り、リアルタイム圧縮でも割とイケるな、という印象。活用していきたい。
ファイル数が極少数の場合は事前圧縮も活用していきたい。

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9