LoginSignup
50
51

More than 3 years have passed since last update.

新しい圧縮アルゴリズムBrotliをnginxで試す

Last updated at Posted at 2015-11-30

この記事は、nginx Advent Calendar 2015の1日目の記事です。

Brotli

BrotliはGoogleが最近公開した新しい圧縮アルゴリズムとその実装です。zopfliと違って従来のよく利用されているdeflateアルゴリズムと互換性はありませんが、deflateアルゴリズムと同じくくらい高速でなおかつ圧縮率がさらに向上しているのが特徴です。

今回はこのBrotliによるコンテンツ圧縮をnginxで利用する方法について紹介します。

ngx_brotli

ngx_brotlingx_http_gzip_moduleおよびngx_http_gzip_static_moduleのbrotli版に相当するnginxモジュールです。以下のようにnginxでgzip圧縮をしたことがある人ならなんとなくどう使うかわかるディレクティブがあります。(ディレクティブはほかにもいろいろとあります。詳しくはngx_brotliのREADME.mdを参照して下さい)

ディレクティブ名 解説
brotli on | off brotliによる圧縮を有効/無効にする
brotli_types mime-type ... 圧縮対象のMIME typeを列挙する
brotli_static on | off | always 事前にbrotliで圧縮したファイルを配信する

libbrotliをインストールする

ngx_brotliをインストールするには事前にlibbrotliをインストールする必要があります。

$ git clone https://github.com/bagder/libbrotli
$ cd libbrotli
$ ./autogen.sh
$ ./configure
$ make
$ sudo make install

ngx_brotliをインストールする

続いてngx_brotliをインストールする。nginxのモジュールはnginx本体と一緒にビルドしなければならないのでここではnginx-buildを使ってインストールする。

$ brew tap cubicdaiya/nginx-build
$ brew install nginx-build
$ echo "[ngx_brotli]\nform=git\nurl=https://github.com/google/ngx_brotli.git" > modules3rd.ini
$ nginx-build -d work -v 1.9.7 -m modules3rd.ini
$ cd work/nginx/1.9.7/nginx-1.9.7
$ sudo make install

次にnginx.confに以下の設定を書く。

server {
    listen 8080;
    root html;
    brotli on;
}

これでリクエストヘッダにAccept-Encoding: brを付けてindex.htmlをリクエストすると、

$ curl -H "Accept-Encoding: br" -I http://127.0.0.1:8080/index.html
HTTP/1.1 200 OK
Server: nginx/1.9.7
Date: Mon, 30 Nov 2015 14:41:29 GMT
Content-Type: text/html
Last-Modified: Wed, 06 Aug 2014 17:07:04 GMT
Connection: keep-alive
ETag: W/"53e260b8-264"
Content-Encoding: br
$

という具合にContent-Encoding: brとなってるのが確認できます。

50
51
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
50
51