Mod_pagespeed: clear the cache

The Apache module mod_pagespeed can be used to automatically make web pages faster without effort. It promises a 50% speed boost. I’ve been using for some time and can confirm that there is a definitive speed increase.

But there is a big problem when I update the styling of my blog. When I change the theme css file, I do not see the changes. In the beginning I thought it was caused by the caching plugin I use. But after deactivating the plugin, the problem persisted. Using Ctrl-F5 did not help either. I also opened the CSS in the browser and pressed Ctrl-F5. Nothing worked.

Finally, I understood that mod_pagespeed was responsible for it. So I just needed to clear its cache. Unfortunately it seems to be only possible from the command line. So first open a shell. Then you need to find out where the cache is. It is configured in pagespeed.conf. So either open the file and search for ModPagespeedFileCachePath or execute the following:

# grep "^ *ModPagespeedFileCachePath" /etc/apache2/mods-enabled/pagespeed.conf
    ModPagespeedFileCachePath            "/var/cache/mod_pagespeed/"

Now you need to go to this directory and touch a file (don’t worry if the file doesn’t exist yet, it will be created by touch if it doesn’t already exist), or just touch the file with it’s complete path:

touch /var/cache/mod_pagespeed/cache.flush

You then have to wait for a few second and reload your web page and you’ll see that the new style is used.

Instead of doing all these manual steps, you can do the following:

touch `grep "^ *ModPagespeedFileCachePath" /etc/apache2/mods-enabled/pagespeed.conf | awk ' { print $2; } ' | sed 's/"//g'`/cache.flush

You need to adapt the path to the apache configuration depending on where it is stored e.g. use the following if it is stored in /etc/httpd/conf.d instead of /etc/apache2/mods-enabled:

touch `grep "^ *ModPagespeedFileCachePath" /etc/httpd/conf.d/pagespeed.conf | awk ' { print $2; } ' | sed 's/"//g'`/cache.flush

(thanks Kieran for the hint).

18 thoughts on “Mod_pagespeed: clear the cache

  1. I am having the same problem with my site. I wanted to change the font-size for my posts but it is not showing up on my site. I wish there was a simple way to flush the cache without having to do all of this.

    Thanks!

    1. There is a much easier way to resolve this.
      1) rename the CSS file (i.e. template2.css)
      2) at the same change the link for the css in the header to the new name

      Voila – Pagespeed picks up the new css and caches that.

      IF it bothers you to have this name change – just change them back after you’ve loaded the page a couple times.

      1. Hi Eric,

        It is of course possible to keep changing the name of the file so that there is no cached version anymore. But if you use a CMS or something similar, this might be more work than calling a script performing the touch. And if this CSS file is used in different locations it becomes very difficult to rename.

    2. Yes, it’d be great if the pagespeed module provided a simpler way to do it. But at least now that this mechanism was introduced it’s at all possible to do it.

      One thing which could be done is to have a URL which when called created the file. Of course you may want to protect the URL with a username and password so that someone else cannot keep flushing the cache without you knowing.

  2. Dreamhost.com doesn’t give access for users to touch a file in /var/mod_pagespeed/cache. Any other suggestions besides renaming your css file? I can’t believe this type of thing isn’t easy to do. It is such a PITA not being able to see your style changes without going through a bunch of hoops. Looks like no one “system engineered” this pagespeed module. Sorry, vent over. Just very frustrated.

  3. Here’s the CentOS version


    touch `grep "^ *ModPagespeedFileCachePath" /etc/httpd/conf.d/pagespeed.conf | awk ' { print $2; } ' | sed 's/"//g'`/cache.flush

  4. Hi guys, if you are using WordPress and having issues with this:

    1) Create a custom wordpress plugin that enqueues your custom CSS file
    2) In the enqueue line, append ?ver=1 to the css link
    3) With easy access to your plugin editor, you can simply keep changing the version number. i.e. ?ver=2 and mod_pagespeed will treat it as a new file.

    I have created a simple plugin that enqueues a custom css and custom js file which you can use if you wish: http://lumico.systems/download/custom-styles-scripts.tgz

    1. Sometimes having parameters in the URL makes problem caching the files, so that’s the reason why I’d rather clear the cache manually whenever I modify the CSS files (which doesn’t happen too often anyway).

  5. That worked for me, but that wasn’t the easiest manipulation, so I seached a little more and…

    from http://kb.peacefulmedia.com/how-to-work-with-mod_pagespeed-clear-cache/ :

    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’

    so here’s a couple of snippets :

    as a PHP file :
    <?php
    /* purge_modspagespeed v1.0
    * Calls the mod_pagespeed module to clear its cache
    * mod_pagespeed configuration (global or by vhost) must have these defined :
    *
    * ModPagespeedEnableCachePurge on
    * ModPagespeedPurgeMethod PURGE
    *
    * put this at document_root of your server or wherever suits you
    */

    $url = "https://&quot;.$_SERVER["HTTP_HOST"];
    echo '’;
    echo “Usage :
    “. $url. “/purge_modspagespeed.php: purges the whole cache
    “. $url. “/purge_modspagespeed.php?page=/path/to/page : purges the page cache
    “;
    echo “”;

    if(isset($_GET[‘page’])) {

    $page = filter_var($_GET[‘page’], FILTER_SANITIZE_STRING);
    echo $page ;
    $url .= $page;
    }
    $ch = curl_init();
    $options = array(
    CURLOPT_URL => $url,
    CURLOPT_HEADER => false,
    CURLOPT_CUSTOMREQUEST => ‘PURGE’,
    CURLOPT_SSL_VERIFYPEER=> false,
    );
    curl_setopt_array($ch, $options);
    curl_exec($ch);
    curl_close($ch);

    ?>

    and as a WordPress snippet :
    <?php
    /*
    * Usage :
    * https://wordpress-site.com/path/to/post/?purge=1 : purges the whole cache
    * https://wordpress-site.com/path/to/post/?purge=1&page=1 : purges the cache for the current_page_only
    *
    * use in function.php or with the Code Snippets plugin
    *
    */

    function bda_purge_modpagespeed() {
    // make sure you call the right protocol (http or https);
    $url = "https://&quot;.$_SERVER["HTTP_HOST"];
    if(isset($_GET['purge'])) {
    if(isset($_GET['page'])) {
    $url .= str_replace('?'.$_SERVER["QUERY_STRING"],'',$_SERVER[REQUEST_URI]);
    }
    echo "Purging $url\n”;
    $ch = curl_init();
    $options = array(
    CURLOPT_URL => $url,
    CURLOPT_HEADER => false,
    CURLOPT_CUSTOMREQUEST => ‘PURGE’,
    CURLOPT_SSL_VERIFYPEER=> false,
    );
    curl_setopt_array($ch, $options);
    curl_exec($ch);
    curl_close($ch);
    echo “”;
    echo ‘Retour‘;
    echo “”;
    exit;
    }
    }
    add_action(‘init’,’bda_purge_modpagespeed’);
    ?>

Leave a Reply

Your email address will not be published. Required fields are marked *