Skip to content
Logo Theodo

Warming up Varnish4 cache

Raphaël Dubigny2 min read

Caching data with varnish allows to deal with heavy data traffic at a limited cost.

The question is, how can I serve fresh data even if no one have requested them recently? The solution is to request your cache regularly.

This is how I managed to auto warm up a list of URLs in my varnish cache every hour.

First, I add the urls I want refreshed in a
urls.txt file like this:


Note that this url list is static but you can easily feed it automatically with a background job of yours.

Then, I need to add specific code to my config.vcl file:

acl warmuper_ip {

sub vcl_recv {
    # the script must always refresh the cache
    if (client.ip ~ warmuper_ip && req.http.Cache-Control ~ "no-cache") {
        set req.hash_always_miss = true;

Then, I create a script to actually warmup the varnish cache:

wget --output-document=/dev/null --header='Cache-Control: no-cache' --tries=1 --quiet --base= --input-file=/path/to/urls.txt

In order to test your script you’ll have to look at varnish logs. Here is a command that may help varnishlog -c | grep ReqURL.

Eventually, I add this cron task with crontab -e:

0 * * * * cd /path/to/

And that is it!

Here are some helpful resources:

Liked this article?