I am using HAProxy to load balance a bunch of PHP servers and want to introduce Varnish in the scene now.
HAP sends request to Varnish iff app-login cookie is not available, Varnish doesnt have to do anything here except to serve request(Cache HIT) or send it back to HAP incase of Cache MISS, HAP then selects a PHP server & resources are fetched from it and served to client by HAP through Varnish. I have the following config file.
I don't understand 2 things here;
- in cache miss scenario varnish gives request back to HAP which checks for the app-login cookie, doesn't find it and sends it back to varnish (end less loop situation), I can have Varnish set a cookie and make HAP to check for that and select PHP servers backend on this basis(suggestions welcome).
- Secondly, how can I achieve that when resources are fetched by HAP in case of MISS it sends to varnish and then it is served to client , this way Varnish builds up the cache eventually.
Please also let me know if some thing critical is missed out here.
Thanks in advance
Config File
#BE for Varnish is HAP in this machine
backend default {
.host = "127.0.0.1";
.port = "80";
}
sub vcl_recv {
# HAP sends request to Varnish iff app-login cookie is not available
# Varnish doesnt have to do anything here except to serve request(Cache HIT) or
# send it back to HAP incase of Cache MISS, resouces are then fetched from PHP servers
# and served to client by HAP through Varnish
# We unset the cookies here as they dont affect the response
unset req.http.cookie;
# Lighttpd is already compressing resources, so we dont do it here.
return (lookup); # Control is passed to vcl_hit or vcl_miss
}
sub vcl_hit {
return (deliver);
}
sub vcl_miss {
return (fetch);
}
sub vcl_fetch {
set obj.ttl = 1m;
return (deliver);
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
return (deliver);
}
sub vcl_init {
return (ok);
}
sub vcl_fini {
return (ok);
}
Thanks to Baptiste Assmann I was able to get the response for point 2 I asked - Once the PHP server answers HAProxy, then the response follows the reverse path, so the response pass through Varnish (so it’s cached) then pass back to HAProxy then the client.
For point 1, the loop condition I mentioned(thanks to Baptiste again) can be fixed by putting a ACL in HAP config which routes the traffic to PHP Servers(I am using Varnish's IP as a condition), so when cache miss happens HAP gets request from Varnish, fetches resources and follows the reverse path and goes to Varnish then to HAP and finally to user. I tested this condition and HAP logs clearly states that it’s working:
Call comes to HAP and as the cookie is not available HAP sends it to varnish, cache miss happens and worker0 is used to get resources by HAP. In the next call ( I am caching for 1m) Varnish serves everything from cache(varnishlog told me this) and PHP servers are not contacted.
Thanks