适用于WordPress的Varnish实际应用场景

场景一,来源Varnish官方网站给的一个实际应用样例,不过它们给出的代码有2处错误。下面我给出的这个版本以经改正了原本的2处错误。


sub vcl_recv {
  if (req.http.Accept-Encoding) {
#revisit this list
    if (req.url ~ "\.(gif|jpg|jpeg|swf|flv|mp3|mp4|pdf|ico|png|gz|tgz|bz2)(\?.*|)$") {
      remove req.http.Accept-Encoding;
    } elsif (req.http.Accept-Encoding ~ "gzip") {
      set req.http.Accept-Encoding = "gzip";
    } elsif (req.http.Accept-Encoding ~ "deflate") {
      set req.http.Accept-Encoding = "deflate";
    } else {
      remove req.http.Accept-Encoding;
    }
  }
  if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    unset req.http.cookie;
    set req.url = regsub(req.url, "\?.*$", "");
  }
  if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|feed|ref(id|src)?|v(er|iew))=") {
    set req.url = regsub(req.url, "\?.*$", "");
  }
  if (req.http.cookie) {
    if (req.http.cookie ~ "(wordpress_|wp-settings-)") {
      return(pass);
    } else {
      unset req.http.cookie;
    }
  }
}

sub vcl_fetch {
  if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true" || req.url ~ "xmlrpc.php") {
    return (hit_for_pass);
  }
  if ( (!(req.url ~ "(wp-(login|admin)|login)")) || (req.request == "GET") ) {
    unset beresp.http.set-cookie;
  }
  if (req.url ~ "\.(gif|jpg|jpeg|swf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
    set beresp.ttl = 365d;
  }
}

sub vcl_deliver {
# multi-server webfarm? set a variable here so you can check
# the headers to see which frontend served the request
#   set resp.http.X-Server = "server-01";
   if (obj.hits > 0) {
     set resp.http.X-Cache = "HIT";
   } else {
     set resp.http.X-Cache = "MISS";
   }
}

应用场景样例二:


sub vcl_recv {

          if (req.http.Accept-Encoding) {
              if (req.http.Accept-Encoding ~ "gzip") {
                  # If the browser supports it, we'll use gzip.
                  set req.http.Accept-Encoding = "gzip";
              }
              else if (req.http.Accept-Encoding ~ "deflate") {
                  # Next, try deflate if it is supported.
                  set req.http.Accept-Encoding = "deflate";
              }
              else {
                  # Unknown algorithm. Remove it and send unencoded.
                  unset req.http.Accept-Encoding;
              }
          }

          # Set client IP
          if (req.http.x-forwarded-for) {
              set req.http.X-Forwarded-For =
              req.http.X-Forwarded-For + ", " + client.ip;
          } else {
              set req.http.X-Forwarded-For = client.ip;
          }

         if (req.request != "GET" &&
              req.request != "HEAD" &&
              req.request != "PUT" &&
              req.request != "POST" &&
              req.request != "TRACE" &&
              req.request != "OPTIONS" &&
              req.request != "DELETE") {
                  return (pipe);
          }

          if (req.request != "GET" &&  req.request != "HEAD") {
              # /* We only deal with GET and HEAD by default */
              return (pass);
          }

          # admin users always miss the cache
          if( req.url ~ "^/wp-(login|admin)" ||
              req.http.Cookie ~ "wordpress_logged_in_" ){
                  return (pass);
          }

     # Remove cookies set by Google Analytics (pattern: '__utmABC')
          if (req.http.Cookie) {
              set req.http.Cookie = regsuball(req.http.Cookie,
                  "(^|; ) *__utm.=[^;]+;? *", "\1");
              if (req.http.Cookie == "") {
                  remove req.http.Cookie;
              }
          }

          # always pass through POST requests and those with basic auth
          if (req.http.Authorization || req.request == "POST") {
              return (pass);
          }

          # Do not cache these paths
          if (req.url ~ "^/wp-cron\.php$" ||
              req.url ~ "^/xmlrpc\.php$" ||
              req.url ~ "^/wp-admin/.*$" ||
              req.url ~ "^/wp-includes/.*$" ||
              req.url ~ "\?s=") {
                  return (pass);
          }

          # Define the default grace period to serve cached content
          set req.grace = 30s;

          # By ignoring any other cookies, it is now ok to get a page
          unset req.http.Cookie;
          return (lookup);
      }

      sub vcl_fetch {
          # remove some headers we never want to see
          unset beresp.http.Server;
          unset beresp.http.X-Powered-By;

          # only allow cookies to be set if we're in admin area
          if( beresp.http.Set-Cookie && req.url !~ "^/wp-(login|admin)" ){
              unset beresp.http.Set-Cookie;
          }

          # don't cache response to posted requests or those with basic auth
         if ( req.request == "POST" || req.http.Authorization ) {
             return (hit_for_pass);
         }

         # don't cache search results
         if( req.url ~ "\?s=" ){
             return (hit_for_pass);
         }

# only cache status ok
         if ( beresp.status != 200 ) {
             return (hit_for_pass);
         }

         # If our backend returns 5xx status this will reset the grace time
         # set in vcl_recv so that cached content will be served and
         # the unhealthy backend will not be hammered by requests
         if (beresp.status == 500) {
             set beresp.grace = 60s;
             return (restart);
         }

         # GZip the cached content if possible
         if (beresp.http.content-type ~ "text") {
             set beresp.do_gzip = true;
         }

         # if nothing abovce matched it is now ok to cache the response
         set beresp.ttl = 24h;
         return (deliver);
     }

     sub vcl_deliver {
         # remove some headers added by varnish
         unset resp.http.Via;
         unset resp.http.X-Varnish;
     }

     sub vcl_error {
         if (obj.status == 503) {
                     # set obj.http.location = req.http.Location;
                     set obj.status = 404;
             set obj.response = "Not Found";
                     return (deliver);
         }
     }

以上规则可能只适用于Varnish 3.X版本,Varnish 4.0版由于VCL语法规则作了调整,所以需要调整后才能应用。

发表评论或回复

*选项为必填项,您的电子邮件地址不会被公开。

*
*