Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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;

  1. 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).
  2. 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);
}
share|improve this question
    
You've overridden all the functions with just a return value on top - that's pretty bad. The default configuration will probably do you a whole much better than this. –  Kvisle Jan 8 '13 at 7:06
    
@Kvisle thanks for the comment, can you please throw some more light on point 2 i mentioned in the problem statement. –  APZ Jan 8 '13 at 7:39

1 Answer 1

up vote 1 down vote accepted

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:

108.108.108.108:21478 [08/Jan/2013:11:58:16.**637**] inbound varnish/varnish0 1/0/0/1803/2182    200 45872 – - —- 2/2/0/1/0 0/0 {abc.xxx.com} “GET / HTTP/1.1″

192.168.1.1:37029 [08/Jan/2013:11:58:16.**639**] inbound worker/worker0 0/0/0/1796/1802 200 45776 – - –NI 2/2/0/1/0 0/0 {abc.xxx..com} “GET / HTTP/1.1″

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.