Advertisement
Guest User

Untitled

a guest
Feb 29th, 2012
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 6.11 KB | None | 0 0
  1. backend default {
  2.   .host = "localhost";
  3.   .port = "80";
  4. }
  5.  
  6. acl purge {
  7.   "localhost";
  8.   "127.0.0.1";
  9. }
  10.  
  11.  
  12. sub vcl_recv {
  13.     if (req.restarts == 0) {
  14.         if (req.http.x-forwarded-for) {
  15.             set req.http.X-Forwarded-For =
  16.             req.http.X-Forwarded-For + ", " + client.ip;
  17.         } else {
  18.             set req.http.X-Forwarded-For = client.ip;
  19.         }
  20.     }
  21.  
  22.     if (req.request != "GET" &&
  23.       req.request != "HEAD" &&
  24.       req.request != "PUT" &&
  25.       req.request != "POST" &&
  26.       req.request != "TRACE" &&
  27.       req.request != "OPTIONS" &&
  28.       req.request != "DELETE" &&
  29.       req.request != "PURGE") {
  30.         /* Non-RFC2616 or CONNECT which is weird. */
  31.         return (pipe);
  32.     }
  33.  
  34.     # purge request
  35.     if (req.request == "PURGE") {
  36.         if (!client.ip ~ purge) {
  37.             error 405 "Not allowed.";
  38.         }
  39.         ban("obj.http.X-Purge-Host ~ " + req.http.X-Purge-Host + " && obj.http.X-Purge-URL ~ " + req.http.X-Purge-Regex + " && obj.http.Content-Type ~ " + req.http.X-Purge-Content-Type);
  40.         error 200 "Purged.";
  41.     }
  42.  
  43.     # we only deal with GET and HEAD by default
  44.     if (req.request != "GET" && req.request != "HEAD") {
  45.         return (pass);
  46.     }
  47.         # # Normalize the header, remove the port (in case you're testing this on various TCP ports)
  48.     set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
  49.     # normalize Aceept-Encoding header
  50.     # http://varnish.projects.linpro.no/wiki/FAQ/Compression
  51.     if (req.http.Accept-Encoding) {
  52.         if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") {
  53.             # No point in compressing these
  54.             remove req.http.Accept-Encoding;
  55.         } elsif (req.http.Accept-Encoding ~ "gzip") {
  56.             set req.http.Accept-Encoding = "gzip";
  57.         } elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") {
  58.             set req.http.Accept-Encoding = "deflate";
  59.         } else {
  60.             # unkown algorithm
  61.             remove req.http.Accept-Encoding;
  62.         }
  63.     }
  64.  
  65.     # static files are always cacheable. remove SSL flag and cookie
  66.     # if (req.url ~ "^/(media|js|skin)/.*\.(png|jpg|jpeg|gif|css|js|swf|ico)$") {
  67.     if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
  68.         unset req.http.Https;
  69.         unset req.http.Cookie;
  70.         unset req.http.cookie;
  71.         return (lookup);
  72.     }
  73.  
  74.     # not cacheable by default
  75.     if (req.http.Authorization || req.http.Https) {
  76.         return (pass);
  77.     }
  78.  
  79.         # Are there cookies left with only spaces or that are empty?
  80.         if (req.http.cookie ~ "^ *$") {
  81.                 unset req.http.cookie;
  82.         }
  83.  
  84.     # do not cache any page from
  85.     # - index files
  86.     # - ...
  87.     if (req.url ~ "^/(index)") {
  88.         return (pass);
  89.     }
  90.  
  91.     # as soon as we have a NO_CACHE cookie pass request
  92.     if (req.http.cookie ~ "NO_CACHE=") {
  93.         return (pass);
  94.     }
  95.  
  96.  
  97.     # remove Google gclid parameters
  98.     set req.url = regsuball(req.url,"\?gclid=[^&]+$",""); # strips when QS = "?gclid=AAA"
  99.     set req.url = regsuball(req.url,"\?gclid=[^&]+&","?"); # strips when QS = "?gclid=AAA&foo=bar"
  100.     set req.url = regsuball(req.url,"&gclid=[^&]+",""); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz"
  101.  
  102.     return (lookup);
  103. }
  104.  
  105. sub vcl_hash {
  106.     hash_data(req.url);
  107.     if (req.http.host) {
  108.         hash_data(req.http.host);
  109.     } else {
  110.         hash_data(server.ip);
  111.     }
  112. #    if (!(req.url ~ "^/(media|js|skin)/.*\.(png|jpg|jpeg|gif|css|js|swf|ico)$")) {
  113.  
  114. #        call design_exception;
  115. #    }
  116.     return (hash);
  117. }
  118. #
  119. # sub vcl_hit {
  120. #     return (deliver);
  121. # }
  122. #
  123. # sub vcl_miss {
  124. #     return (fetch);
  125. # }
  126.  
  127. sub vcl_fetch {
  128.     if (beresp.status == 500) {
  129.        set beresp.saintmode = 30s;
  130.        return (restart);
  131.     }
  132.     set beresp.grace = 5m;
  133.  
  134.     # add ban-lurker tags to object
  135.     set beresp.http.X-Purge-URL = req.url;
  136.     set beresp.http.X-Purge-Host = req.http.host;
  137.     set beresp.http.X-Cacheable = "NO:Not Cacheable";
  138.  
  139.     if (beresp.status == 200 || beresp.status == 301 || beresp.status == 404) {
  140.         if (beresp.http.Content-Type ~ "text/html" || beresp.http.Content-Type ~ "text/xml") {
  141.             if ((beresp.http.Set-Cookie ~ "NO_CACHE=") || (beresp.ttl < 1s)) {
  142.                 set beresp.ttl = 0s;
  143.                 return (hit_for_pass);
  144.             }
  145.  
  146.             # marker for vcl_deliver to reset Age:
  147.             set beresp.http.magicmarker = "1";
  148.  
  149.             # Don't cache cookies
  150.             unset beresp.http.set-cookie;
  151.         } else {
  152.             set beresp.http.X-Cacheable = "YES";
  153.             # set default TTL value for static content
  154.             unset beresp.http.expires;
  155.             unset beresp.http.set-cookie;
  156.             unset beresp.http.Cookie;
  157.             set beresp.ttl = 300h;
  158.             #set beresp.http.cache-control = "max-age=3000";
  159.  
  160.         }
  161.         return (deliver);
  162.     }
  163.  
  164.     return (hit_for_pass);
  165. sub vcl_deliver {
  166.  
  167.     set resp.http.Server = "OMG";
  168.     remove resp.http.X-Powered-By;
  169.     remove resp.http.MS-Author-Via;
  170.     # debug info
  171. #    if (resp.http.X-Cache-Debug) {
  172.         if (obj.hits > 0) {
  173.             set resp.http.X-Cache = "HIT";
  174.             set resp.http.X-Cache-Hits = obj.hits;
  175.         } else {
  176.            set resp.http.X-Cache = "MISS";
  177.         }
  178.         set resp.http.X-Cache-Expires = resp.http.Expires;
  179. #    } else {
  180.         # remove Varnish/proxy header
  181.         remove resp.http.X-Varnish;
  182.         remove resp.http.Via;
  183.         remove resp.http.Age;
  184.         remove resp.http.X-Purge-URL;
  185.         remove resp.http.X-Purge-Host;
  186.         #set resp.http.Cache-Control = "max-age=300";
  187.         #set resp.http.Expires = obj.ttl;
  188.         #set resp.http.Age = "0";
  189.  
  190.  #   }
  191.     if (resp.http.magicmarker) {
  192.         # Remove the magic marker
  193.         unset resp.http.magicmarker;
  194.  
  195.         set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";
  196.         set resp.http.Pragma = "no-cache";
  197.         set resp.http.Expires = "Mon, 31 Mar 2008 10:00:00 GMT";
  198.         set resp.http.Age = "0";
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement