Pick our brains...

How to Work With Mod_Pagespeed & Clear the Pagespeed Cache

Mod_pagespeed is an *amazing* tool for improving page load speeds and improving the performance speed of your website. In many cases, it can double the speed of your website, cutting the average page load time in half, or more!

This page speed optimization tool has been fully implemented on the latest WordPress-optimized web hosting platforms at Liquid Web, our personal favorite for web hosting solutions.

But mod_pagespeed also introduces some challenges. For one, how do you clear the cache??

First let’s note that it’s possible to simply disable pagespeed via a url parameter by just tacking on the parameter of “ModPagespeed=off”. For example:

http://www.example.com/about?ModPagespeed=off

This will disable the page re-writing that pagespeed does, but it will not clear the cache for files that are already cached.

Another option is to turn off pagespeed entirely, via the config file:

/usr/local/apache/conf/pagespeed.conf
Add a line at the top, and then restart apache.

ModPagespeed off

or if you really want to turn pagespeed off:

ModPagespeed unplugged

For further information about the configuration of mod_pagespeed, please see this article.

To actually clear the cache, there are a few options, depending upon how your server is configured. The case we see the most is the following.

First, let’s find out where the cache files exist. (Note that this is rather particular to the server that you’re working with. You may need to adjust the command to your specific environment.)

grep "^ *ModPagespeedFileCachePath" /etc/httpd/conf/pagespeed.conf

This gives us the directory of the cache files.

ModPagespeedFileCachePath "/var/cache/pagespeed/"

Now, all we need to do is create / modify the file that signals the mod_pagespeed module to clear the cache.

touch /var/cache/pagespeed/cache.flush

We also need to restart the httpd service so that the changes kick in.

service httpd restart

We’ve also seen instances where one needs to stop and start the memcache daemon, in addition to flushing the pagespeed cache as above. We can restart memcached with these commands.

service memcached stop
service memcached start

So in summary, to clear the mod_pagespeed cache on this particular server, simply execute these commands:

touch /var/cache/pagespeed/cache.flush
service httpd restart

Sometimes followed by:

service memcached stop
service memcached start

 

Clearing the Pagespeed Cache with a PURGE Command

As of mod_pagespeed version 1.9.32.1, there is also another method for clearing / purging the cache. But your server does need to be configured for it.

For Apache servers, the configuration for enabling purging looks like this:

ModPagespeedEnableCachePurge on
ModPagespeedPurgeMethod PURGE     (optional)

Once the cache purge function has been enabled, you can then clear the cache by sending a PURGE request via curl. To clear the entire cache, the command looks like this:

curl --request PURGE 'http://www.example.com/*'

You can also clear the cache for just a single page, like this:

curl --request PURGE 'http://www.example.com/path/file.ext'

Note that if you’re using Cloudflare.com for your nameservers and dns, or if you’re running the Sucuri.net cloud proxy firewall, the situation is a bit more complicated. The above curl commands won’t work, because it is blocked by the firewall and security protections. So in this case we would need to revert back to the ssh command line method mentioned above.

Or hey, if you’re not in a hurry, go get a cup of coffee or tea, and come back in a bit. The mod_pagespeed cache will clear itself with a bit of time 😉

And now you know how to find the cache files, how to manually clear them, how to restart the httpd process, and also how enabling the purge functionality can turn this process into a simple curl command.

Ahoy!