LoginSignup
23
25

More than 5 years have passed since last update.

WEBアプリケーションのレスポンスを3分で改善する、というお話

Last updated at Posted at 2014-03-17

目標

WEBアプリケーション(今回はPHP Laravel4)のレスポンスを3分で改善します。てっとり早く、
Google Developers PageSpeed Insights
https://developers.google.com/speed/pagespeed/insights/
で70点を取れればOKとします。
ちなみに、70点の根拠は・・・Insightsで有名どころをスコアリングしてみれば分かります。(落とし所!)

あ、Qiitaさんのスコアはとてもよろしかったです。さすが。

期待される効果

  • 不要なトラフィックを避ける。(圧縮、キャッシュ)
  • セキュリティが向上する。
  • GoogleのSEOスコアが”たぶん”上がる。

環境

  • httpd 2.2.15-29.el6.centos + mod_php 5.5.9-2.el6.remi
  • WAFとしてPHP Laravel4.1を使っています。 (今回はApache+mod_phpでPHPアプリケーションが動いているベタベタな環境ですが、.htaccessを利用しているなら割りと広く応用できるでしょう)
  • あ、インターネットに接続している(=公開されている)アプリケーションじゃないとだめです。PageSpeed Insightsで計測するので。

手順

1) 現状を知る

https://developers.google.com/speed/pagespeed/insights/
に最適化したいアプリケーションのURLを入れてスコアを出してみます。
私はこのほどリリースした拙作 http://j.inthemaking.net/ でやってみました。
スクリーンショット 2014-03-17 16.45.44.png
モバイル47点、パソコン57点、なかなかダメな結果です。

2) ドキュメントルート直下の.htaccessを開いてみる

PHPのWAFを使っていれば大抵の場合、ドキュメントルートにお馴染みの.htacccesファイルがありますね。内容はどれもmod_rewriteの記述でしょうか。以下はLaravel4.1の場合です。

public/.htaccess
<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

こいつをいじっていきます。

3)チートする!

さて、ここでいきなりチートです。他人のフンドシを使います。
https://github.com/h5bp/server-configs-apache
を利用させてもらいます。

https://github.com/h5bp/server-configs-apache/blob/master/.htaccess
を開き(RAW表示するといいですね)、2)の.htaccessの記述の上にコピペします。

4) ちょっと修正する

これはケースバイケースでしょうけど、私の場合は・・・

mod_rewriteに関するもので不要なものをコメントアウト。

@210行目〜

public/.htaccess
#<IfModule mod_rewrite.c>
#    Options +FollowSymlinks
#  # Options +SymLinksIfOwnerMatch
#    RewriteEngine On
#  # RewriteBase /
#</IfModule>

@238行目〜

public/.htaccess
#<IfModule mod_rewrite.c>
#    RewriteCond %{HTTPS} !=on
#    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
#    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
#</IfModule>

@448行目〜

public/.htaccess
# <IfModule mod_rewrite.c>
#    RewriteCond %{SERVER_PORT} !^443
#    RewriteRule ^ https://example-domain-please-change-me.com%{REQUEST_URI} [R=301,L]
# </IfModule>

Apacheのバージョンが2.3.7以下だと云々とあるので、IfModule mod_filter.c をコメントアウト

@512行目、531行目

public/.htaccess
    # Compress all output labeled with one of the following MIME-types
    # (for Apache versions below 2.3.7, you don't need to enable `mod_filter`
    #  and can remove the `<IfModule mod_filter.c>` and `</IfModule>` lines
    #  as `AddOutputFilterByType` is still in the core directives).
#    <IfModule mod_filter.c>
        AddOutputFilterByType DEFLATE application/atom+xml \
                                      application/javascript \
                                      application/json \
                                      application/ld+json \
                                      application/rss+xml \
                                      application/vnd.ms-fontobject \
                                      application/x-font-ttf \
                                      application/x-web-app-manifest+json \
                                      application/xhtml+xml \
                                      application/xml \
                                      font/opentype \
                                      image/svg+xml \
                                      image/x-icon \
                                      text/css \
                                      text/html \
                                      text/plain \
                                      text/x-component \
                                      text/xml
#    </IfModule>

5) ふたたび計測、効果を確かめる

ふたたび https://developers.google.com/speed/pagespeed/insights/ で計測します。

モバイル71点、パソコン89点にスコアがはね上がりました!大成功!

スクリーンショット 2014-03-17 17.10.50.png

以上であります!


告知

というか宣伝ですが、このエントリーも関係してますが、

2chまとめ巡査長β

http://j.inthemaking.net/
というのを作ってみました。PCでも見れますがスマホ用です。
よろしければ暇つぶしに使ってご感想いただければ嬉しいです。

23
25
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
23
25