1
votes

I am trying to fetch data from my (PHP) backend using ESI in Varnish 4.x. I want to save (cache) the result in my Varnish instance. The result is unique for every IP address (client.ip) so the hash for the cached result has to be built with client.ip included. Varnish is not cacheing as expected. I cannot seem to cache based on IP. To give you an idea of what I have tried see below.

unique.js:

var rand = '<esi:include src="/unique.php"/>';

unique.php:

<?php echo rand(); ?>

default.vcl (extract):

sub vcl_recv {
     if (req.url ~ "^/unique.(js|php)") {
         set req.http.marker = client.ip;
     }
}

sub vcl_hash {
     if (req.http.marker) {
         hash_data(req.http.marker);
     }
}

sub vcl_backend_response {
    if (bereq.url == "/unique.js") {
       set beresp.do_esi = true;
       set beresp.ttl = 24h;
    } elseif (bereq.url == "/unique.php") {
       set beresp.ttl = 30d;
    }
}

Obviously I am missing something. Any hints are appreciated!

1

1 Answers

0
votes

I don't think there is quite enough information in the post to give you a definitive answer, but my guess is that the caching is not working properly for a reason unrelated to your ESI code.

As mentioned in multiple places (here is one example), Varnish by default doesn't cache with cookies. So it very well may be that cookies set in JS or by PHP session code are causing your request to not be cached. My guess would be that you need to add:

unset req.http.cookie;

inside your if (req.url ~ "^/unique.(js|php)") { block.