Advertisement
Guest User

Varnish 3.0 vcl

a guest
Dec 1st, 2012
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Redirect requests to Apache, running on port 8000 on localhost
  2. backend apache {
  3.         .host = "127.0.0.1";
  4.         .port = "8000";
  5. }
  6.  
  7. acl purge {
  8.         "localhost";
  9.         "127.0.0.1";
  10.         "127.0.1.1";
  11.         "testing.domain.com";
  12. }
  13.  
  14. sub vcl_recv {
  15.  
  16.         # remove ?ver=xxxxx strings from urls so css and js files are cached.
  17.         # Watch out when upgrading WordPress, need to restart Varnish or flush cache.
  18.        set req.url = regsub(req.url, "\?ver=.*$", "");
  19.  
  20.         # Remove "replytocom" from requests to make caching better.
  21.        set req.url = regsub(req.url, "\?replytocom=.*$", "");
  22.  
  23.        remove req.http.X-Forwarded-For;
  24.        set    req.http.X-Forwarded-For = client.ip;
  25.  
  26.         ## If the request to the backend returns a code other than 200, restart the loop
  27.         ## If the number of restarts reaches the value of the parameter max_restarts,
  28.         ## the request will be error'ed.  max_restarts defaults to 4.  This prevents
  29.         ## an eternal loop in the event that, e.g., the object does not exist at all.
  30. #       if (beresp.status != 200 && beresp.status != 403 && beresp.status != 404) {
  31. #               return(restart);
  32. #       }
  33.  
  34.         # Exclude this site because it breaks if cached
  35.         #if ( req.http.host == "testing.domain.com" ) {
  36.         #    return( pass );
  37.         #}
  38.  
  39.         # Serve objects up to 2 minutes past their expiry if the backend is slow to respond.
  40.         set req.grace = 120s;
  41.  
  42.         # Strip cookies for static files:
  43.        if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$") {
  44.                unset req.http.Cookie;
  45.                return(lookup);
  46.        }
  47.  
  48.         # Remove has_js and Google Analytics __* cookies.
  49.        set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js)=[^;]*", "");
  50.  
  51.         # Remove a ";" prefix, if present.
  52.        set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
  53.  
  54.         # Remove empty cookies.
  55.        if (req.http.Cookie ~ "^\s*$") {
  56.                unset req.http.Cookie;
  57.        }
  58.  
  59.         if (req.request == "PURGE") {
  60.                 if (!client.ip ~ purge) {
  61.                         error 405 "Not allowed.";
  62.                 }
  63.                 return (lookup);
  64.         }
  65.  
  66.         if (req.request == "BAN") {
  67.                 if (!client.ip ~ purge) {
  68.                     error 405 "Not allowed.";
  69.                 }
  70.                 ban("req.url ~ " + req.url + " && req.http.host == " + req.http.host);
  71.                 error 200 "Ban added";
  72.         }
  73.  
  74.         # Pass anything other than GET and HEAD directly.
  75.         if (req.request != "GET" && req.request != "HEAD") {
  76.             return( pass );
  77.         } /* We only deal with GET and HEAD by default */
  78.  
  79.         # remove cookies for comments cookie to make caching better.
  80.        set req.http.cookie = regsub(req.http.cookie, "1231111111111111122222222333333=[^;]+(; )?", "");
  81.  
  82.         # never cache the admin pages, or the server-status page
  83.         if (req.request == "GET" && (req.url ~ "(wp-admin|bb-admin|server-status|feed)")) {
  84.             return(pipe);
  85.         }
  86.         # don't cache authenticated sessions
  87.         if (req.http.Cookie && req.http.Cookie ~ "(wordpress_|PHPSESSID)") {
  88.             return(pass);
  89.         }
  90.         # don't cache ajax requests
  91.         if(req.http.X-Requested-With == "XMLHttpRequest" || req.url ~ "nocache" || req.url ~ "(control.php|wp-comments-post.php|wp-login.php|bb-login.php|bb-reset-password.php|register.php)") {
  92.             return (pass);
  93.         }
  94.         return( lookup );
  95. }
  96.  
  97. sub vcl_hash {
  98.         # Each cached page has to be identified by a key that unlocks it.
  99.         # Add the browser cookie only if a WordPress cookie found.
  100.         if ( req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)" ) {
  101.         #if (req.http.Cookie) {
  102.                 #set req.hash += req.http.Cookie;
  103.                 hash_data(req.http.Cookie);
  104.         }
  105. }
  106.  
  107. # Called after a document has been successfully retrieved from the backend.
  108. sub vcl_fetch {
  109.  
  110.         # Uncomment to make the default cache "time to live" is 5 minutes, handy
  111.         # but it may cache stale pages unless purged. (TODO)
  112.         # By default Varnish will use the headers sent to it by Apache (the backend server)
  113.         # to figure out the correct TTL.
  114.         # WP Super Cache sends a TTL of 3 seconds, set in wp-content/cache/.htaccess
  115.  
  116.         set beresp.ttl   = 24h;
  117.  
  118.         # Strip cookies for static files and set a long cache expiry time.
  119.         if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$") {
  120.                 unset beresp.http.set-cookie;
  121.                 set beresp.ttl   = 24h;
  122.         }
  123.  
  124.         # If WordPress cookies found then page is not cacheable
  125.         if (req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)") {
  126.             # set beresp.cacheable = false;#versions less than 3
  127.             #beresp.ttl>0 is cacheable so 0 will not be cached
  128.             set beresp.ttl = 0s;
  129.         } else {
  130.             # set beresp.cacheable = true;
  131.             set beresp.ttl=24h; #cache for 24hrs
  132.         }
  133.  
  134.         # Varnish determined the object was not cacheable
  135.         if (!beresp.ttl > 0s) {
  136.             set beresp.http.X-Cacheable = "NO:Not Cacheable";
  137.         } else if ( req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_|UserID|_session)" ) {
  138.                 # You don't wish to cache content for logged in users
  139.                 set beresp.http.X-Cacheable = "NO:Got Session";
  140.                 return(hit_for_pass);
  141.         }  else if ( beresp.http.Cache-Control ~ "private") {
  142.             # You are respecting the Cache-Control=private header from the backend
  143.             set beresp.http.X-Cacheable = "NO:Cache-Control=private";
  144.             return(hit_for_pass);
  145.         } else if ( beresp.ttl < 1s ) {
  146.             # You are extending the lifetime of the object artificially
  147.             set beresp.ttl   = 300s;
  148.             set beresp.grace = 300s;
  149.             set beresp.http.X-Cacheable = "YES:Forced";
  150.         }  else {
  151.             # Varnish determined the object was cacheable
  152.             set beresp.http.X-Cacheable = "YES";
  153.         }
  154.  
  155.         if (beresp.status == 404 || beresp.status >= 500) {
  156.             set beresp.ttl = 0s;
  157.         }
  158.  
  159.         ## Remove the X-Forwarded-For header if it exists.
  160.         remove req.http.X-Forwarded-For;
  161.  
  162.         ## insert the client IP address as X-Forwarded-For. This is the normal IP address of the user.
  163.         set    req.http.X-Forwarded-For = req.http.rlnclientipaddr;
  164.  
  165.         ## Added security, the "w00tw00t" attacks are pretty annoying so lets block it before it reaches our webserver
  166.         if (req.url ~ "^/w00tw00t") {
  167.                 error 403 "Not permitted";
  168.         }
  169.  
  170.         if (req.url ~ "^/phpmyadmin") {error 403;}
  171.         if (req.url ~ "^/PhpMyAdmin") {error 403;}
  172.         if (req.url ~ "^/databases") {error 403;}
  173.         if (req.url ~ "^/pma") {error 403;}
  174.         if (req.url ~ "^/Toata")  {error 403;}
  175.  
  176.         ## Deliver the content
  177.         return(deliver);
  178. }
  179.  
  180. ## Deliver
  181. #sub vcl_deliver {
  182.         ## We'll be hiding some headers added by Varnish. We want to make sure people are not seeing we're using Varnish.
  183.         ## Since we're not caching (yet), why bother telling people we use it?
  184.         #remove resp.http.X-Varnish;
  185.         #remove resp.http.Via;
  186.         #remove resp.http.Age;
  187.  
  188.         ## We'd like to hide the X-Powered-By headers. Nobody has to know we can run PHP and have version xyz of it.
  189.         #remove resp.http.X-Powered-By;
  190. #}
  191.  
  192. sub vcl_deliver {
  193.   // Debugging
  194.   if (obj.hits > 0) {
  195.     set resp.http.X-Cache = "HIT";
  196.   } else {
  197.     set resp.http.X-Cache = "MISS";
  198.   }
  199.  
  200.   // Remove some headers: PHP version
  201.   unset resp.http.X-Powered-By;
  202.  
  203.   // Remove some headers: Apache version & OS
  204.   unset resp.http.Server;
  205.  
  206.   return (deliver);
  207. }
  208.  
  209. sub vcl_error {
  210.   set obj.http.Content-Type = "text/html; charset=utf-8";
  211.   set obj.http.Retry-After = "5";
  212.   synthetic {"
  213. <?xml version="1.0" encoding="utf-8"?>
  214. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  215. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  216. <html>
  217.  <head>
  218.    <title>"} + obj.status + " " + obj.response + {"</title>
  219.  </head>
  220.  <body>
  221.    <h1>Error "} + obj.status + " " + obj.response + {"</h1>
  222.    <p>"} + obj.response + {"</p>
  223.    <h3>Guru Meditation:</h3>
  224.    <p>XID: "} + req.xid + {"</p>
  225.    <hr>
  226.    <p>Varnish cache server</p>
  227.  </body>
  228. </html>
  229. "};
  230.   return (deliver);
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement