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!