Varnish is an HTTP accelerator that makes the site faster. Varnish uses caching to increase the performance of the website.
Whenever someone requests something from a website, varnish fetches the available contents from its cache to provide a faster response to the request.
Whenever a developer makes any changes to the website, it becomes mandatory to clear the cache to reflect the desired changes. In order to clear the cache, we need to restart the service.
However, this is not a good practice to log in to the server and execute the command every time.
So, to ease the process, varnish provides a few methods to purge the cache. We can send a “BAN” or “PURGE” header or request a particular URL for purging the cache.
In this tutorial, we will show you how to allow purging from a particular IP.
Varnish Configurations
In the default varnish configuration, we define IP addresses inside the acl. If the sender’s IP address matches with the IP address mentioned inside the acl, then they can purge the cache.
But the problem is when you are using Cloudflare, varnish does not get the original IP of the sender. Instead, it gets the Cloudflare’s IP address using which the purging can not be done.
So we need to define the original IP of the sender in the varnish.
First, you need to comment or delete the default purging configuration lines inside vcl_recv, which is located in/etc/varnish/default.vcl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#acl purge { # "localhost"; # "127.0.0.1"; #} # Allow purging from ACL #if (req.method == "PURGE") { # If not allowed then a error 405 is returned # if (!client.ip ~ purge) { # return(synth(405, "This IP is not allowed to send PURGE requests.")); # } # If allowed, do a cache_lookup -> vlc_hit() or vlc_miss() # return (purge); # } # These lines are different for varnish 3 and varnish 4 |
For varnish 6, add the following lines inside vcl_recv
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if (req.restarts == 0) { if (req.http.X-Forwarded-For) { set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip; } else { set req.http.X-Forwarded-For = client.ip; } } if (req.request == "PURGE" || req.url == "/purge") { # Replace these IP with your IP if (req.http.X-Forwarded-For !~ "(209.152.41.21|105.45.120.37)") { return(synth(405, "This IP is not allowed to send PURGE requests.")); } ban("req.url ~ /"); return (purge); } |
1 |
Suppose your site is varnish.example.com. To clear the cache, you have to hit the URL varnish.example.com/purge.
If you wish to use any specific term other than ‘purge’ to clear the cache, then you can do so. You just need to update that word in the varnish configuration file.
For example, if you wish to use the ‘Clear’ word to purge the cache, then you need to update the code like this –
if (req.request == “BAN” || req.url == “/clear”) {
To purge the cache from the command line you need to type the following command
1 |
curl -X PURGE varnish.example.com # For varnish 6 |
Here in the blog, I am allowing two IP addresses to allow purging.
If you wish to add more addresses, then you can add the IP address in the format “(your IP | your IP | your IP)“.
if (req.http.X-Forwarded-For !~ “(IP-address|IP-address|IP-address)”) {
Now you can purge the cache remotely and allow only particular IP addresses for purging.
Need Support?
Thank You for reading this Blog!
For further more interesting blogs, keep in touch with us. If you need any kind of support, simply raise a ticket at https://webkul.uvdesk.com/en/.
You may also visit our Odoo development services and quality Odoo Extensions.
For further help or queries, please contact us or raise a ticket.