Looking for a method to purge Varnish cache?
Varnish cache is useful to speed up websites and reduce the load on the website servers.
However, when the website has new contents, showing the cached results can be a headache
At Bobcares, we receive requests regarding varnish cache as a part of our Server Management Services.
Today, let’s see how our Support Engineers clear varnish cache.
What is Varnish Cache?
Basically, Varnish Cache is a web application accelerator also known as a caching HTTP reverse proxy.
Varnish Cache is really fast and speeds up website delivery depending on the server architecture.
One of the key features of Varnish Cache is the flexibility of its configuration language, VCL.
A purge takes place when we pick an object from the cache and discard it.
How to purge Varnish cache
Recently one of our customers contacted us to purge his Varnish. He had updated the website contents and wanted Varnish to notify Varnish. With the presence of cached content, visitors will still see the old website.
Let us discuss the different methods used by our Support Engineers to clear the cache.
1. Using Varnish restart
To clear Varnish cache, we just restart Varnish. This will clear the entire varnish cache in the server. We use the command
service varnish restart
It is the quickest way to purge the entire cache.
2. Purge using VCL
To purge a specific object from the cache we need to add a function into VCL. We add the following contents in the Varnish configuration file. The default Varnish configuration file is at location /etc/varnish/default.vcl.
acl purge {
"localhost";
"1xx.1xx.5x.0"/24;
}
sub vcl_recv {
# allow PURGE from localhost and 1xx.1xx.5x.0
if (req.method == "PURGE") {
if (!client.ip ~ purge) {
return(synth(405,"Not allowed."));
}
return (purge);
}
}
We add the details and save the file.
We can purge varnish cache for a specific URL using CURL.
curl -X PURGE URL-to-PURGE
Example:
curl -X PURGE http://www.domain.com/
3. Using the command line
Similarly, we can purge the cache from the command line as well.
The command to purge all varnish cache,
varnishadm 'ban req.url ~ .'
To purge a particular domain or particular URL
varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret "ban req.http.host ~ www.domain.com && req.url http://www.domain.com"
Again, to purge all .html pages from cache from a domain
varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret "ban req.http.host ~ www.domain.com && req.url .html"
[Trouble in purging Varnish cache? Our Expert Engineers are available 24×7.]
Conclusion
In short, we have discussed the methods to purge Varnish cache. Also, we have discussed how our Support Engineers purge varnish cache and help users see the updated website content.
0 Comments