For those who are not familiar let me explain what CloudProxy is. CloudProxy is the Web Application Firewall & Caching service from my employer, Sucuri.net. This service is setup at the DNS level and helps filtering all the HTTP requests that get to your website and speeding the performance a the same time.

Below is a simple graphic to explains how it works:

sucuri-cloudproxy-how-it-works
A brief explanation on how Sucuri’s CloudProxy works

How can I clear CloudProxy Cache?

It’s simple there are two ways:

  • Via CloudProxy‘s dashboard on Sucuri’s website
  • Using the API

Clearing the cache via the dashboard

Just follow this steps:

  1. Login to your account via https://login.sucuri.net
  2. Then go to CloudProxy (check your left menu) or simply type this: https://waf.sucuri.net on your browser address bar.
  3. Once on the CloudProxy Dashboard, click on settings and select the website you want to clear the cache for.
  4. Once there click on Performance, and then simply on the button to clear the cache. And in less than 2 minutes the cache will be trashed from all the CDN servers.

You can also check their tutorial on their Knowledge Base: CloudProxy – Clearing Cache

This is convenient when you are not making that many changes into your website. However if you are pushing changes everyday or building an webapp, then you need something easier.

Clearing the cache via PHP

I am currently building a web app with some friends and we faced the issue that after deploying each change, I needed to login to my account and clear the cache manually. This becomes tediously when you are committing changes several times a day 5 days a week. So I had to come up with a simple solution to get the job done.

That is why I turned to CloudProxy’s API, which offers me the ability to clear the cache by calling a simple string. You can see the basic code  below:

<?php
/**
 * Simple Script in PHP to clear sucuri's cloudproxy cache via php 
 * 
 * Author: Salvador Aguilar
 * Email: sal.aguilar81@gmail.com
 * Web: salrocks.com
 */
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://waf.sucuri.net/api?v2',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        // this is the Sucuri CloudProxy API key for this website
        k => 'your-cloudproxy-api-key-goes-here',
        // this is the Sucuri CloudProxy API secret for this website
        s => 'your-cloudproxy-api-secret-goes-here',
        // this is the Sucuri CloudProxy API action for this website
        a => 'clear_cache'
    )
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
echo '<pre>' . $resp . '</re>';
// Close request to clear up some resources
curl_close($curl);

https://gist.github.com/riper81/2f070c485f8172364bf4efd7a30f2b2c.js

That is the initial version of the script, if you want other features or to fork it you can get it from GitHub: https://github.com/riper81/clear_cache_cloudproxy_php 

What’s next?

Once you have the file, you simply put it on your website root and you edit with the proper values from your Sucuri account and then simply run it via a web browser or command line and you cache will be cleared.

I added this script into my deployment script so I can update from my git repo and then clear the cache from Sucuri. Making things easy for us 🙂

If you have an idea, hit me up!

Advertisements