How can users purge varnish cache on their own?

Update: Starting from v1.8.0 end users are now able to purge their own domain's cache via cPanel. Read more on this Varnish feature here.

You can either use the No-Cache directive available in WHM or use the sample PHP script below.

Here's a sample PHP script (call it purge.php):

# get param
$url = $_POST["url"];
$host = $_POST["host"];

  $ip = "127.0.0.1";
  $port = "80";

  $timeout = 1;
  $verbose = 1;

  # inits
  $sock = fsockopen ($ip,$port,$errno, $errstr,$timeout);
  if (!$sock) { echo "connections failed $errno $errstr"; exit; }

  if ( !($url || $host) ) { echo "No params"; exit; }

  stream_set_timeout($sock,$timeout);

  $pcommand = "purge";
  # Send command
  $pcommand .= ".hash $url#$host#";

  put ($pcommand);
  put ("quit");

  fclose ($sock);

  function readit() {
    global $sock,$verbose;
    if (!$verbose) { return; }
    while ($sockstr = fgets($sock,1024)) {
      $str .= "rcv: " . $sockstr . "
"
; } if ($verbose) { echo "$str\n"; } } function put($str) { global $sock,$verbose; fwrite ($sock, $str . "\r\n"); if ($verbose) { echo "send: $str
\n"
; } readit(); } ?>

And the HTML page that calls the above script (call it purge.html):

<html>
<head>
        <title>Purge Varnish cache</title>
</head>

<style type="text/css">
  body {
        font-size: 10px;
  }
  h1 {
        font-weight: bold;
        color: #000000;
        border-bottom: 1px solid #C6EC8C;
        margin-bottom: 2em;
  }
  label {
        font-size: 160%;
        float: left;
        text-align: right;
        margin-right: 0.5em;
        display: block
  }
  input[type="text"] {
        width: 500px;
  }
  .submit input {
        margin-left: 0em;
        margin-bottom: 1em;
  }
</style>

<body>

  <h1>Makes Varnish purge the supplied URL from its cache</h1>

  <form action="purge.php" method="post">
        <p><label>URL</label> <input type="text" name="url"></p>
        <p><label>HOST</label> <input type="text" name="host"></p>
        <p class="submit"><input value="Submit" type="submit"></p>
  </form>

</body>
</html>

Your could, for example, expand on the above code to prompt for a domain name or URL before proceeding.

  • 9 משתמשים שמצאו מאמר זה מועיל
?האם התשובה שקיבלתם הייתה מועילה

מאמרים קשורים

How do I uninstall the cPanel Varnish Plugin?

Here are the steps to remove the cPanel Varnish Plugin from your server. It's recommended that...

I've installed the cPanel Varnish Plugin, now what?

No action is required from your end. The installation script takes care of installing and...

Where can I download the installer?

The installer is in the Client Area location. Simply login to your account at...