Friday, March 11, 2011

Configuring mod_pagespeed Filters

Rewriting Level

mod_pagespeed offers two default "levels" to simplify configuration: PassThrough and CoreFilters. The CoreFilters set contains filters that the mod_pagespeed team believes are safe for most web sites. By using the CoreFilters set, as mod_pagespeed is updated with new filters, your site will get faster.
To disable the CoreFilters, you can specify
ModPagespeedRewriteLevel PassThrough
and then enable specific filters with the ModPagespeedEnableFilters directive. The default level is CoreFilters. The core set of filters is set to:
add_head
   combine_css
   extend_cache
   inline_css
   inline_javascript
   insert_img_dimensions
   rewrite_images
   trim_urls

Enabling and Disabling Specific Filters

To turn off specific filters in the core set, specify:
ModPagespeedDisableFilters filtera,filterb
For example, if you want to use the core set of filters, but specifically disable rewrite_images and combine_css, you can use:
ModPagespeedDisableFilters rewrite_images,combine_css
The ModPagespeedEnableFilters configuration file directive allows specification of one or more filters by name, separated by commas. You can use any number of ModPagespeedEnableFilters directives, each of which can contain multiple filter names separated by commas. For example:
ModPagespeedRewriteLevel PassThrough
    ModPagespeedEnableFilters combine_css,extend_cache,rewrite_images
    ModPagespeedEnableFilters rewrite_css,rewrite_javascript
The order of the directives in the configuration file is not important: the rewriters are run in the pre-defined order presented in the table:

Filter name In Core Set Brief Description
add_head YesAdds a <head> element to the document if not already present
combine_heads NoCombines multiple <head> elements found in document into one
strip_scripts NoRemoves all script tags from document to help run experiments
outline_css NoExternalize large blocks of CSS into a cacheable file
outline_javascript NoExternalize large blocks of JS into a cacheable file
move_css_to_head NoMoves CSS elements into the <head>
combine_css YesCombines multiple CSS elements into one
rewrite_css NoRewrites CSS files to remove excess whitespace and comments
make_google_analytics_async NoConvert synchronous use of Google Analytics API to asynchronous
rewrite_javascript NoRewrites Javscript files to remove excess whitespace and comments
inline_css YesInlines small CSS files into the HTML document
inline_javascript YesInlines small JS files into the HTML document
rewrite_images YesOptimizes images, re-encoding them, removing excess pixels, and inlining small images
insert_img_dimensions YesAdds width/height attributes to <img> tags that lack them
remove_comments NoRemoves comments in HTML files, though not inline js or css
trim_urls YesShortens URLs by making them relative to the base URL
collapse_whitespace NoRemoves excess whitespace in HTML files (avoiding <pre>, <script>, <style>, and <textarea>)
elide_attributes NoRemoves attributes which are not significant according to the HTML spec
extend_cache YesExtends cache lifetime of all resources by signing URLs with content hash
remove_quotes NoRemoves quotes around HTML attributes that are not lexically required
add_instrumentation NoAdds JavaScript to page to measure latency and send back to the server

Tuning the Filters

Once the rewriters are selected, some of them may also be tuned. These parameters control the inlining and outlining thresholds of various resources.
ModPagespeedCssInlineMaxBytes        2048
    ModPagespeedImgInlineMaxBytes        2048
    ModPagespeedJsInlineMaxBytes         2048
    ModPagespeedCssOutlineMinBytes       3000
    ModPagespeedJsOutlineMinBytes        3000
Note: The default settings are reasonable and intuitive, but as of this writing (Feburary 2011) have not been experimentally tuned.
These directives can be used in .htaccess files and <Directory> scopes.

No comments: