Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pagespeed module (Apache) instructions

Using Google Pagespeed module (Apache)

Install

Follow initial install instructions here

Recommended additional modules

Add the following to the bottom (but inside the <IfModule /> block) of /etc/apache2/mods-available/pagespeed.conf

ModPagespeedRewriteLevel OptimizeForBandwidth
ModPagespeedDomain http://yourdomainhere.co.uk
ModPagespeedEnableFilters responsive_images,resize_images,lazyload_images,convert_jpeg_to_progressive
ModPagespeedEnableFilters combine_css,prioritize_critical_css,rewrite_css,fallback_rewrite_css_urls
ModPagespeedEnableFilters canonicalize_javascript_libraries,rewrite_javascript,defer_javascript
ModPagespeedEnableFilters collapse_whitespace,extend_cache,trim_urls
ModPagespeedForceCaching on
ModPagespeedEnableCachePurge on

ModPagespeedRewriteLevel sets the general configuration level for the Pagespeed Module. CoreFilters is the default value however, more recently the OptimizeForBandwidth setting has proven as effective with less issues across website. More information can be found here

ModPagespeedDomain sets the domain to authorise file changes against i.e. when minifying or compressing images etc this must match the domain serving this content. Multiple ModPagespeedDomain can be defined for multiple domains if your server runs multiple sites.

ModPagespeedEnableFilters defines and activates addition filters for content types to be passed through in addition to a core set of filters defined here. Please refer to the reference section for details about each of the filters above. (Note, you can comma separate multiple filters and break onto multiple lines, redeclaring ModPagespeedEnableFilters each time, for improved readibility).

ModPagespeedForceCaching enforces cache control for resources, for more information see https://developers.google.com/speed/pagespeed/module/filter-cache-extend

ModPagespeedEnableCachePurge is required (set to on) in order to allow for purge requests from the admin screens (defaults to /pagespeed_admin & /pagespeed_global_admin).

Using with WordPress sites

The install instructions and configurations will work out of the box for WordPress sites, however, in order to access the admin pages, an exception must be added to your .htaccess file, see the example below:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !pagespeed
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Note: The line RewriteCond %{REQUEST_URI} !pagespeed - this ensures WordPress doesn't try and looking for a page with a slug containing pagespeed, however this will only be preserved if your .htaccess file is readonly as WordPress will refresh this when updating its permalinks. To prevent this you can set the RewriteEngine to off when attempting to access the admin pages within the pagespeed.conf file:

<Location /pagespeed_admin>
    <IfModule mod_rewrite.c>
        RewriteEngine Off
    </IfModule>
</Location>

It's also worth being aware that by default the pagespeed module will be applied across the whole of your WordPress install, including the admin area which can lead to some issues with internal ajax events. To remedy this, add the following to the buttom of the pagespeed.conf file:

<Location /wp-admin/>
    ModPagespeed Off
</Location>

This is disable the pagespeed module for any and all pages (and files) under the /wp-admin directory. Once this has been edited, ensure you run service apache2 restart for the changes to take affect.

Using on a Cloudflare site with SSL

I've been using Cloudflare's free SSL alongside it's other services for a couple of WordPress sites. You can use the pagespeed module with this setup, however, you need to take into account that when using Cloudflare on your site, the visitor's IP address isn't logged, rather the Cloudflare service IP address is passed instead. This will lead to a 403 (Forbbiden) message being returned when attempting to access the /pagespeed_admin pages.

To fix this issue, simply follow the instructions here which will ensure that the visitor IP is passed through correctly from Cloudflare to the server (Apache) and validates correctly to allow access to the admin pages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment