Advertisement
meesteridle

varnishd 4.0 -- default.vcl WordPress caching

Aug 25th, 2015
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nginx 4.19 KB | None | 0 0
  1. # Varnish configuration for wordpress
  2. # AdminGeekZ Ltd <sales@admingeekz.com>
  3. # URL: www.admingeekz.com/varnish-wordpress
  4. # Version: 1.5
  5. ## updated -> Brad Tarver
  6. ## updated -> for Varnish 4.0 syntax
  7.  
  8. vcl 4.0;
  9.  
  10. backend default {
  11.     .host = "127.0.0.1";
  12.     .port = "8080";
  13. }
  14.  
  15. #Which hosts are allowed to PURGE the cache
  16. acl purge {
  17.     "172.31.27.119";
  18. }
  19.  
  20.  
  21. sub vcl_recv {
  22.  
  23.     if (req.method == "BAN") {
  24.         if(!client.ip ~ purge) {
  25.             return (synth(405, "Not allowed."));
  26.         }
  27.         ban("req.url ~ "+req.url+" && req.http.host == "+req.http.host);
  28.         return (synth(200, "Banned."));
  29.     }
  30.  
  31.     if (req.method != "GET" &&
  32.         req.method != "HEAD" &&
  33.         req.method != "PUT" &&
  34.         req.method != "POST" &&
  35.         req.method != "TRACE" &&
  36.         req.method != "OPTIONS" &&
  37.         req.method != "DELETE") {
  38.         return (pipe);
  39.     }
  40.  
  41.  
  42.     if (req.method != "GET" && req.method != "HEAD") {
  43.         return (pass);
  44.     }
  45.  
  46.     #Don't cache admin or login pages
  47.     if (req.url ~ "wp-(login|admin)" ||
  48.         req.url ~ "preview=true" ||
  49.         req.url ~ "contact-form") {
  50.         return (pass);
  51.     }
  52.  
  53.     #Don't cache logged in users
  54.     if (req.http.Cookie && req.http.Cookie ~ "(wordpress_|wordpress_logged_in|comment_author_)") {
  55.         return(pass);
  56.     }
  57.  
  58.     #Don't cache ajax requests, urls with ?nocache or comments/login/regiser
  59.     if(req.http.X-Requested-With == "XMLHttpRequest" || req.url ~ "nocache" || req.url ~ "(control.php|wp-comments-post.php|wp-login.php|register.php)") {
  60.         return (pass);
  61.     }
  62.  
  63.     if (req.http.host == "(www.)oncallnetworks.net") {
  64.         set req.backend_hint = default;
  65.     }
  66.     if (req.http.host == "(www.)liveoakpsych.com") {
  67.         set req.backend_hint = default;
  68.     }
  69.     if (req.http.host == "(www.)brandonfc.com") {
  70.         set req.backend_hint = default;
  71.     }
  72.     if (req.http.host == "(www.)adaptingtechnologies.com") {
  73.         set req.backend_hint = default;
  74.     }
  75.     if (req.http.host == "(www.)stalexisjackson.org") {
  76.         set req.backend_hint = default;
  77.     }
  78.     if (req.http.host == "(www.)lugoj.org") {
  79.         set req.backend_hint = default;
  80.     }
  81.     if (req.http.host == "(www.)berial.org") {
  82.         set req.backend_hint = default;
  83.     }
  84.     # redirect berial.info/.net to .org using vcl_synth
  85.     if ( (req.http.host ~ "^(?i)www.berial.net" || req.http.host ~ "^(?i)berial.net" || req.http.host ~ "^(?i)www.berial.info" || req.http.host ~ "^(?i)berial.info") ) {
  86.         return (synth(750, ""));
  87.     }
  88.     if (req.http.host == "mlg.oncallnetworks.net" ) {
  89.         set req.backend_hint = default;
  90.     }
  91.     if (req.http.host == "(www.)idl[e3]mind.net" ) {
  92.         set req.backend_hint = default;
  93.     }
  94.     if (req.http.host == "(www.)badmojobrew\bery|ing|ingco\b.com" ) {
  95.         set req.backend_hint = default;
  96.     }
  97.  
  98.     #Serve stale cache objects for up to 2 minutes if the backend is down
  99.     set req.http.grace = 120;
  100.  
  101.     #Remove all cookies if none of the above match
  102.     unset req.http.cookie;
  103.  
  104.     return (hash);
  105.  
  106. }
  107.  
  108. sub vcl_synth {
  109.     if (resp.status == 750) {
  110.         set resp.status = 301;
  111.         # redirect berial.info/.net to .org
  112.         set resp.http.Location = "http://www.berial.org" + req.url;
  113.         return(deliver);
  114.     }
  115. }
  116.  
  117.  
  118. sub vcl_backend_response {
  119.     #Don't cache error pages
  120.     if (beresp.status >= 400) {
  121.         set beresp.ttl = 0m;
  122.         return(deliver);
  123.     }
  124.  
  125.     if (bereq.url ~ "wp-(login|admin)" ||
  126.         bereq.url ~ "preview=true" ||
  127.         bereq.url ~ "contact-form") {
  128.         set beresp.uncacheable = true;
  129.         set beresp.ttl = 120s;
  130.         return (deliver);
  131.     }
  132.  
  133.     if (bereq.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)") {
  134.         set beresp.uncacheable = true;
  135.         set beresp.ttl = 120s;
  136.         return (deliver);
  137.     }
  138.  
  139.     #Set the default cache time of 12 hours
  140.     set beresp.ttl = 12h;
  141.     return (deliver);
  142. }
  143.  
  144. sub vcl_hash {
  145.  
  146.     #Uncomment if you use multiple domains/subdomains and want to maintain separate caches
  147.     #hash_data(req.http.host);
  148.     #Uncomment if you use SSL and want to maintain separate caches
  149.     #hash_data(req.http.X-Forwarded-Port);
  150.  
  151.     #Set the hash to include the cookie if it exists, to maintain per user cache
  152.     if ( req.http.Cookie ~"(wp-postpass|wordpress_logged_in|comment_author_)" ) {
  153.         hash_data(req.http.Cookie);
  154.     }
  155. }
  156.  
  157.  
  158. #Comment this out if you don't want to see whether there was a HIT or MISS in the headers
  159. sub vcl_deliver {
  160.     if (obj.hits > 0) {
  161.         set resp.http.X-Cache = "HIT";
  162.     } else {
  163.         set resp.http.X-Cache = "MISS";
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement