blog:UbuntuのNginxでPagespeedを使う

Pagespeedは比較的昔からあるWebサイトの最適化手法の一つです。Googleさんが提供しています。
本来はWebサイトの設計やコードそのものを見直して転送量を含めたロードタイムを削減したりするのですが
すでに運用中のサイト全体に行うのはちょっと無理があります。
そこで必要な部分をサーバーサイドで自動的に行ってくれるモジュールを導入することで
Webサイトへの変更なしで効果を得ることが出来ます。
メリットは大きいですが、場合によってはデメリットが生じることがあるようなので
導入の判断は慎重にした方がよいかもしれません。
うちは遊びで運用してるので見切り発車で入れてみました^q^

PageSpeed Module – Make the Web Faster — Google Developers
https://developers.google.com/speed/pagespeed/module?hl=ja

Apacheはモジュールの形になるので導入が比較的容易なのですが、
Nginxはソースからビルドし直さないと行けないので結構面倒です。
バージョン管理も大変になるので、UbuntuのPPA(Personal Package Archives for Ubuntu)
を探してみたら良さそうなのを見つけました。

↓ここから
https://launchpad.net/ubuntu/+ppas?name_filter=nginx

↓これを使ってみた。
https://launchpad.net/~sandyd/+archive/nginx-current-pagespeed

いわゆる野良ビルドなので信頼性という点では??ですが、
面倒なことは楽したい方にはお薦めです^q^

add-apt-repositoryを使うのでまだ未導入の場合は

apt-get install python-software-properties

とする

# add-apt-repository ppa:sandyd/nginx-current-pagespeed
 This PPA contains the latest version of Nginx built with the Pagespeed modulet.

Original package backported from Debian and pagespeed support added

IMPORTANT NOTE:
On Trusty, and possibly future releases, add the following to /etc/apt/preferences.d/nginx-current-pagespeed

Package: *
Pin: release o=LP-PPA-sandyd-nginx-current-pagespeed
Pin-Priority: 600

From trusty onwards, Ubuntu will be synced with debian unstable (See ProposedMigration https://wiki.ubuntu.com/ProposedMigration), which will require the fix above to continue using the PPA.
 More info: https://launchpad.net/~sandyd/+archive/nginx-current-pagespeed
Press [ENTER] to continue or ctrl-c to cancel adding it

gpg: keyring `/tmp/tmppit1oi9v/secring.gpg' created
gpg: keyring `/tmp/tmppit1oi9v/pubring.gpg' created
gpg: requesting key F2F46C93 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmppit1oi9v/trustdb.gpg: trustdb created
gpg: key F2F46C93: public key "Launchpad ~dolphinaura" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK

2014-04-24現在でtrustyの1.4.7がパッケージにリストされているので
14.04LTSでも問題ありません。

# nginx -V
nginx version: nginx/1.4.7
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_flv_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_mp4_module --with-http_perl_module --with-http_random_index_module --with-http_secure_link_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --add-module=/build/buildd/nginx-1.4.7/debian/modules/headers-more-nginx-module --add-module=/build/buildd/nginx-1.4.7/debian/modules/nginx-auth-pam --add-module=/build/buildd/nginx-1.4.7/debian/modules/nginx-cache-purge --add-module=/build/buildd/nginx-1.4.7/debian/modules/nginx-dav-ext-module --add-module=/build/buildd/nginx-1.4.7/debian/modules/nginx-development-kit --add-module=/build/buildd/nginx-1.4.7/debian/modules/nginx-echo --add-module=/build/buildd/nginx-1.4.7/debian/modules/ngx-fancyindex --add-module=/build/buildd/nginx-1.4.7/debian/modules/nginx-http-push --add-module=/build/buildd/nginx-1.4.7/debian/modules/nginx-lua --add-module=/build/buildd/nginx-1.4.7/debian/modules/nginx-upload-progress --add-module=/build/buildd/nginx-1.4.7/debian/modules/nginx-upstream-fair --add-module=/build/buildd/nginx-1.4.7/debian/modules/ngx_pagespeed --add-module=/build/buildd/nginx-1.4.7/debian/modules/ngx_http_substitutions_filter_module

設定はここからもってきた
バーチャルホストごとに設定したい場合は下の方に設定例がありますね

PageSpeed Configuration – PageSpeed Module — Google Developers
https://developers.google.com/speed/pagespeed/module/configuration

pagespeed on;

# Needs to exist and be writable by nginx.
pagespeed FileCachePath /var/cache/ngx_pagespeed_cache;

# Ensure requests for pagespeed optimized resources go to the pagespeed handler
# and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
  add_header "" "";
}

動作確認

# curl -I http://localhost/wp/
HTTP/1.1 200 OK
Server: nginx/1.4.7
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.5.9-1ubuntu4
X-Pingback: http://www.nekolove.jp/wp/xmlrpc.php
Date: Thu, 24 Apr 2014 01:52:34 GMT
X-Page-Speed: 1.7.30.4-3847
Cache-Control: max-age=0, no-cache

X-Page-Speedが出ていればOK
うちの場合は劇的に変わるものではないっぽいですなー

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