How to purge varnish cache remotely when using cloudflare

Updated 20 October 2023

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

For varnish 6, add the following lines inside vcl_recv

 

 

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

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.

author
. . .

Leave a Comment

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


Be the first to comment.

Start a Project






    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home