Varnish Cache

Ashish aashisn at hotmail.com
Sat Apr 27 13:51:51 CEST 2013


Hi , I am quite new on varnish, so far its been really impressive doing 
test and quite everything that i wanted to achieve with it,
I did some test with single server, and everything looked good, interms 
of cache hit ratio and all. but now i am unable to see anything as such 
as cache at all.

Could anyone please show me pointer what in my rule is causing not to cache?

( Things not working so far)
# Cache as wished: /json/showcase 1s , robot.txt 4 days, CSS/JPG/JS/ 3600s
# If everything is busted serve whatever we got for 6 h


=> vcl

################## Define Backend Servers 
##########################################
backend box1 { .host = "xx.xx.xx.xx"; .probe = { .url = "/"; .timeout = 
34 ms; .interval = 3s;    .window = 5;  .threshold = 3; } }
backend box2 { .host = "xx.xx.xx.xx"; .probe = { .url = "/"; .timeout = 
34 ms; .interval = 3s;    .window = 5;  .threshold = 3; } }

acl unwanted { "188.223.122.81"; }

sub vcl_recv {  if (client.ip ~ unwanted) { error 403 "You seem to be 
blocked man.";}

                                              if (req.http.user-agent ~ 
"^someuseragent")  {
                 error 403 "If you think This is Mistake Please use 
different User-Agent..";}
             }


####################################### Start of Load Balancing 
########################

director my_cluster random {

         { .backend = box1; .weight = 2; }
         { .backend = box2; .weight = 2; }
}

###################################### ENd of Load Balancing 
############################

###################################### Start of Request Handling 
########################
#sub vcl_recv {
                 if (req.http.host ~ "^(.*\.)?myweb\.com$") {
                     set req.backend = my_cluster;
                     }

                     if (req.request != "GET" &&
                       req.request != "HEAD" &&
                       req.request != "PUT" &&
                       req.request != "POST" &&
                       req.request != "TRACE" &&
                       req.request != "OPTIONS" &&
                       req.request != "DELETE") {
                         /* Non-RFC2616 or CONNECT which is weird. */
                         return (pipe);
                     }
                     if (req.request != "GET" && req.request != "HEAD") {
                         /* We only deal with GET and HEAD by default */
                         return (pass);
                     }
                     if (req.http.Authorization || req.http.Cookie) {
                         /* Not cacheable by default */
                         return (pass);
                     }
                      if (req.request == "GET" || req.request == "HEAD") {
                     return (lookup);
         }
  }

  ####################################### DO not cache anything on probe 
##############


sub vcl_recv {
    # This box fqdn is diff then above actual domains
    if (req.http.host ~ "^(.*\.)?\.myboxes\.com$") {
      return(pass);
    }
}

######################################### Start of forced TTL 
############################
sub vcl_fetch {
                if (req.url ~ "/json/showcase") {
                 set beresp.ttl = 1s;
                }


                  if (beresp.http.cache-control !~ "s-maxage" && req.url 
~ "\.(png|gif|jpg)$") {
                         set beresp.ttl = 120s;
                  }

                 if(req.url == "/robots.txt") {
                 # Robots.txt is updated rarely and should be cached for 
4 days
                 # Purge manually as required
                 set beresp.ttl = 96h;
                 }

                 # Set TTL for these product
                 if (req.url ~ 
"\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm)$") {
                             unset beresp.http.set-cookie;
                             set beresp.ttl = 3600s;
                }

                # Allow items to be stale if needed (i.e. if we are down 
server wise).
                set beresp.grace = 6h;
}




More information about the varnish-misc mailing list