Advertisement
Guest User

Varnish VCL for Magento

a guest
Apr 13th, 2011
2,340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.24 KB | None | 0 0
  1. backend apache {
  2.   .host = "127.0.0.1";
  3.   .port = "81";
  4.   .max_connections = 256;
  5.   .connect_timeout = 30s;
  6.   .first_byte_timeout = 30s;
  7.   .between_bytes_timeout = 30s;
  8.  
  9. # need to create ping.html in public_html directory with just a line like "PONG!"
  10.   .probe = {
  11.     .url = "/ping.html";
  12.     .timeout = 10s;
  13.     .window = 8;
  14.     .threshold = 3;
  15.   }
  16. }
  17.  
  18. acl purge {
  19.   "localhost";
  20. }
  21.  
  22. sub vcl_recv {
  23. set req.http.host = "example.net";
  24.   #purge all
  25.   if (req.request == "PURGE") {
  26.     if (!client.ip ~ purge) {
  27.       error 405 "Not allowed.";
  28.     }
  29.     if (req.url ~ "varnish/index/purgeall/key/#Fj1nzljh") {
  30.      purge( ".*" );
  31.     }
  32.   }
  33.  
  34.   #described below need to create random "DynamicPage" value; this functionallity we use
  35.   #to create several caches for 1 dynamic page.
  36.   #for example, "int i = rand() % 2 + 1;" means that we have 2 different copies of one dynamic page
  37. #  C{
  38. #   char buffer [33];
  39. #   int i = rand() % 10 + 1;
  40. #   sprintf(buffer, "%d", i);
  41. #   VRT_SetHdr(sp, HDR_REQ, "\014DynamicPage:", buffer, vrt_magic_string_end);
  42. #  }C
  43.  
  44.   if (req.request != "GET" &&
  45.   req.request != "HEAD" &&
  46.   req.request != "PUT" &&
  47.   req.request != "POST" &&
  48.   req.request != "TRACE" &&
  49.   req.request != "OPTIONS" &&
  50.   req.request != "DELETE") {
  51.     return (pipe);
  52.   }
  53.  
  54.   # do not cache POST requests
  55.   if (req.request == "POST") {
  56.     return (pipe);
  57.   }
  58.  
  59.   #we should not cache any page for Magento backend
  60.   if (req.request == "GET" && (req.url ~ "^/admin") || req.url ~ "^/index.php/admin") {
  61.     return (pass);
  62.   }
  63.  
  64.   #we should not cache any page for checkout and customer modules
  65.   if (req.request == "GET" && (req.url ~ "^/checkout" || req.url ~ "^/customer")) {
  66.     return (pass);
  67.   }
  68.  
  69.   #do not cache till session end
  70.   if (req.http.cookie ~ "nocache_stable") {
  71.     return (pass);
  72.   }
  73.  
  74.   #unique identifier witch tell Varnish use cache or not
  75.   if (req.http.cookie ~ "nocache") {
  76.     return (pass);
  77.   }
  78.  
  79.   if (req.request == "GET" && (req.url ~ "\.(png|jpg|jpeg|gif)$" || req.url ~ "print.css")) {
  80.     return (lookup);
  81.   }
  82.  
  83.   #Even though there are few possible values for Accept-Encoding, Varnish treats
  84.   #them literally rather than semantically, so even a small difference which makes
  85.   #no difference to the backend can reduce cache efficiency by making Varnish cache
  86.   #too many different versions of an object.
  87.   #http://varnish.projects.linpro.no/wiki/FAQ/Compression
  88.   if (req.http.Accept-Encoding) {
  89.     if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
  90.       # No point in compressing these
  91.       remove req.http.Accept-Encoding;
  92.     } elsif (req.http.Accept-Encoding ~ "gzip") {
  93.       set req.http.Accept-Encoding = "gzip";
  94.     } elsif (req.http.Accept-Encoding ~ "deflate") {
  95.       set req.http.Accept-Encoding = "deflate";
  96.     } else {
  97.       # unkown algorithm
  98.       remove req.http.Accept-Encoding;
  99.     }
  100.   }
  101.  
  102.   return (lookup);
  103. }
  104.  
  105. sub vcl_pipe {
  106.   # Note that only the first request to the backend will have
  107.   # X-Forwarded-For set.  If you use X-Forwarded-For and want to
  108.   # have it set for all requests, make sure to have:
  109.   # set req.http.connection = "close";
  110.   # here.  It is not set by default as it might break some broken web
  111.   # applications, like IIS with NTLM authentication.
  112.   return (pipe);
  113. }
  114.  
  115. sub vcl_pass {
  116.   return (pass);
  117. }
  118.  
  119. sub vcl_hit {
  120.   if (!obj.cacheable) {
  121.     return (pass);
  122.   }
  123.   return (deliver);
  124. }
  125.  
  126. sub vcl_miss {
  127.   return (fetch);
  128. }
  129.  
  130. sub vcl_fetch {
  131.   return (deliver);
  132. }
  133.  
  134. sub vcl_deliver {
  135.  
  136.   if (obj.hits > 0) {
  137.     set resp.http.X-Cache = "HIT";
  138.   } else {
  139.     set resp.http.X-Cache = "MISS";
  140.   }
  141. }
  142.  
  143. sub vcl_error {
  144.  set obj.http.Content-Type = "text/html; charset=utf-8";
  145.  synthetic {"
  146. <?xml version="1.0" encoding="utf-8"?>
  147. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  148.  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  149.  <html>
  150.  <head>
  151.  <title>"} obj.status " " obj.response {"</title>
  152.  </head>
  153.  <body>
  154.  <h1>Error "} obj.status " " obj.response {"</h1>
  155.  <p>"} obj.response {"</p>
  156.  <h3> :</h3>
  157.  <p>URL: "} obj.http.bhash {"</p>
  158.  <p>XID: "} req.xid {"</p>
  159.  <hr>
  160.  <address>
  161.  <a href="http://www.varnish-cache.org/">Nexcess cache server</a>
  162.  </address>
  163.  </body>
  164.  </html>
  165.  "};
  166. return (deliver);
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement