天下のGoogle様がmod_pagespeedなるモジュールを公開していたのでその考察を。

mod_pagespeedとは、Googleがサーバーによって提供される、HTMLなり、Javascriptなり、CSSなりを圧縮、整形、キャッシュするなどして表示と転送の高速化をさせるという一貫の作業を自動化してくれるモジュールらしい。

ということで、いまこの鯖もmod_pagespeedを組み込んで動いていたりします。

・インストールとか

インストールはrpmなり、debなりがここにあるので、ダウンロードして、サーバーにインストールさせてあげましょう。

インストールすると初期設定もされるので、Apacheを再起動するだけというお手軽設計!

・設定とか

初期設定のままだと標準で

add_head

combine_css

rewrite_css

rewrite_javascript

inline_css

inline_javascript

rewrite_images

insert_img_dimensions

extend_cache

が読み込まれるらしいのですが、もう少し設定しなければいけないところがあるので、ちまちま設定。

設定ファイルはCentOSだと/etc/httpd/conf.d/pagespeed.confに配置されているので、それをちまちまいじる。

まず設定ファイルの

1
2
3
<IfModule !mod_deflate.c>
LoadModule deflate_module /usr/lib/httpd/modules/mod_deflate.so
</IfModule>

ここはgzip圧縮のモジュールが読み込まれてなかったら読み込んでくれる文らしい。

詳しい設定がされていないので、実用だとこのまま使うのはおすすめではないらしいけど、テストなのでこのまま。

1
2
3
<IfModule pagespeed_module>
SetOutputFilter MOD_PAGESPEED_OUTPUT_FILTER
ModPagespeed on

このModPagespeedの設定を変更することで、モジュールの有効化と無効化を設定できるらしい。

1
2
3
4
5
6
7
8
# ModPagespeedUrlPrefix is the prefix assigned to some types of
# rewritten resources. This directive will be removed in a future
# release. For now, you must specify a URL and path for a hostname
# that is served by this Apache
# instance. http://yourhostname/mod_pagespeed is a good default
# value (replace localhost, below, with the hostname of your
# Apache instance).
ModPagespeedUrlPrefix                "http://geekhost.net/mod_pagespeed/"

このURLはアクセス可能なものを設定する・・・らしいけど、何に使うのかは不明。

1
2
3
4
5
6
7
# The ModPagespeedFileCachePath and
# ModPagespeedGeneratedFilePrefix directories must exist and be
# writable by the apache user (as specified by the User
# directive).
ModPagespeedFileCachePath            "/var/mod_pagespeed/cache/"

ModPagespeedGeneratedFilePrefix      "/var/mod_pagespeed/files/"

ここの設定で一時ファイルを置くフォルダの設定をする。rpmとかでインストールするとフォルダも作成されるっぽい。

移動したり、削除してしまった場合はapacheの書き込み権限のあるフォルダを指定しないとサイトにアクセスするたびにエラーを吐くようになる。

1
2
3
4
5
6
7
8
9
# Override the mod_pagespeed 'rewrite level'. The default level
# "CoreFilters" uses a set of rewrite filters that are generally
# safe for most web pages. Most sites should not need to change
# this value and can instead fine-tune the configuration using the
# ModPagespeedDisableFilters and ModPagespeedEnableFilters
# directives, below. Valid values for ModPagespeedRewriteLevel are
# PassThrough and CoreFilters.
#
ModPagespeedRewriteLevel CoreFilters

このModPagespeedRewriteLevelの設定は、フィルターをセットで読み込むものらしい。標準だと、

add_head

combine_css

rewrite_css

rewrite_javascript

inline_css

inline_javascript

rewrite_images

insert_img_dimensions

extend_cache

のフィルターを読み込むらしい。

1
2
3
4
5
6
7
8
# Explicitly disables specific filters. This is useful in
# conjuction with ModPagespeedRewriteLevel. For instance, if one
# of the filters in the CoreFilters needs to be disabled for a
# site, that filter can be added to
# ModPagespeedDisableFilters. This directive contains a
# comma-separated list of filter names, and can be repeated.
#
# ModPagespeedDisableFilters rewrite_javascript

ModPagespeedDisableFiltersに読み込みたくないフィルターを設定すると、そのフィルターを除外してくれる。複数のフィルターを消したいときは、カンマ区切りで書くか、いくつも書くといいらしい。

1
2
3
4
5
6
7
8
9
10
11
12
13
# Explicitly enables specific filters. This is useful in
# conjuction with ModPagespeedRewriteLevel. For instance, filters
# not included in the CoreFilters may be enabled using this
# directive. This directive contains a comma-separated list of
# filter names, and can be repeated.
#
ModPagespeedEnableFilters add_head,add_instrumentation,extend_cache
ModPagespeedEnableFilters rewrite_images,insert_img_dimensions
ModPagespeedEnableFilters inline_css,combine_css,rewrite_css,move_css_to_head
#    ModPagespeedCssInlineMaxBytes 5120000
ModPagespeedEnableFilters rewrite_javascript
#    ModPagespeedJsInlineMaxBytes 5120000
ModPagespeedEnableFilters collapse_whitespace,remove_comments,remove_quotes

ModPagespeedEnableFiltersに読み込みたいフィルターを書き込むと、そのフィルターを実行してくれるらしい。

複数読み込みたい場合は、カンマで区切るか、いくつも書くといいらしい。

ここらへんはちょっとカスタマイズしてある。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# ModPagespeedDomain
# authorizes rewriting of JS, CSS, and Image files found in this
# domain. By default only resources with the same origin as the
# HTML file are rewritten. For example:
#
#   ModPagespeedDomain cdn.myhost.com
#
# This will allow resources found on http://cdn.myhost.com to be
# rewritten in addition to those in the same domain as the HTML.
#
# Wildcards (* and ?) are allowed in the domain specification. Be
# careful when using them as if you rewrite domains that do not
# send you traffic, then the site receiving the traffic will not
# know how to serve the rewritten content.

ModPagespeedDomainには、pagespeedを実行してもらいたいドメインを設定する。

複数指定する場合は、ワイルドカードか、複数書くといい。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Enables server-side instrumentation and statistics.  If this rewriter is
# enabled, then each rewritten HTML page will have instrumentation javacript
# added that sends latency beacons to /mod_pagespeed_beacon.  These
# statistics can be accessed at /mod_pagespeed_statistics.  You must also
# enable the mod_pagespeed_statistics and mod_pagespeed_beacon handlers
# below.
#
# ModPagespeedEnableFilters add_instrumentation

# This handles the client-side instrumentation callbacks which are injected
# by the add_instrumentation filter.
# You can use a different location by adding the ModPagespeedBeaconUrl
# directive; see the documentation on add_instrumentation.
#
<Location /beacon>
SetHandler mod_pagespeed_beacon
</Location>
ModPagespeedBeaconUrl "http://geekhost.net/beacon?ets=

ここのビーコンを設定すると、ページの読み込み時間を測定できるらしい。

ビーコンの場所を変更したいときはModPagespeedBeaconUrlに場所を書くといいらしい。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 # This page lets you view statistics about the mod_pagespeed module.
<Location /mod_pagespeed_statistics>
# Order allow,deny
# You may insert other "Allow from" lines to add hosts you want to
# allow to look at generated statistics.  Another possibility is
# to comment out the "Order" and "Allow" options from the config
# file, to allow any client that can reach your server to examine
# statistics.  This might be appropriate in an experimental setup or
# if the Apache server is protected by a reverse proxy that will
# filter URLs in some fashion.
# Allow from localhost
SetHandler mod_pagespeed_statistics
</Location>
</IfModule>

ここの設定をすると、読み込みの詳細などをページ上で確認できるらしい。

適度にAllow,Denyで制限をかけましょう。

・フィルター一覧
ModPagespeedEnableFiltersで追加できるフィルターの一覧。

add_head <head></head>のないHTMLファイルにヘッダタグを自動的に追加してくれる。
add_instrumentation ページ読み込み時間の測定を有効化する。
collapse_whitespace HTMLの余分な空白を削除する。
combine_css 複数のCSSファイルを結合する。
combine_heads HEADタグが複数ある場合に結合してくれる。
elide_atttributes タグ内でいらない文章を削除してくれる。
cache_extend ファイルのローカルキャッシュ時間を設定してくれる。
inline_css CSSファイルをHTML内に展開してくれる。
inline_javascript JavascriptファイルをHTML内に展開してくれる。
rewrite_javascript Javascriptファイルを最適化してくれる。
move_css_to_head HTML内に散らばったCSSをHEADタグ内にまとめてくれる。
rewrite_images 小さいイメージファイルをdataスキーマに変換してくれる。
outline_css inline_cssの逆で、CSSを外部ファイルにまとめてくれる。
outline_javascript inline_javascriptの逆で、Javascriptを外部ファイルにまとめてくれる
remove_comments HTML表示に直接関係のない、コメントを削除してくれる。
remove_quotes 容量削減のため、タグ内の削除できるクオータ「”」を削除してくれる
rewrite_css CSSを最適化してくれる。

・考察

rewrite_javascriptやrewrite_cssなどのJavascriptとCSSをいじるフィルターは動く場合と、動かない場合があった。

また、外部サイトのCSSファイルやJavascriptは対象外。

inline_javascriptは、場合によっては急に</script>タグが挿入され、きちんと動作しなくなることがあった。

・まとめ

とりあえず、まだβと言うことで、しばらく様子見かな。

CSS Pocket Reference (Pocket Reference (O’Reilly))

No related posts.