Weekly Drupal beginner questions thread

VCL FILE

 backend default {
     .host = "127.0.0.1";
.port = "8081";
}

backend b67_43_2_177 {
        .host = "67.43.2.177";
        .port = "8081";
}
acl a67_43_2_177 {
        "67.43.2.177";
}

sub vcl_recv {
        if (server.ip ~ a67_43_2_177) {
                set req.backend = b67_43_2_177;
        }           

}

# Respond to incoming requests.
sub vcl_recv {
   # Use anonymous, cached pages if all backends are down.
   if (!req.backend.healthy) {
      unset req.http.Cookie;
   }

   # Allow the backend to serve up stale content if it is responding slowly.
   set req.grace = 6h;

   # Pipe these paths directly to Apache for streaming.
   #if (req.url ~ "^/admin/content/backup_migrate/export") {
   #  return (pipe);
   #}

   if (req.restarts == 0) {
     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;
     }
   }

   # Do not cache these paths.
   if (req.url ~ "^/status\.php$" ||
       req.url ~ "^/update\.php$" ||
       req.url ~ "^/admin$" ||
       req.url ~ "^/admin/.*$" ||
       req.url ~ "^/flag/.*$" ||
       req.url ~ "^.*/ajax/.*$" ||
       req.url ~ "^.*/ahah/.*$") {
        return (pass);
   }

#   Do not allow outside access to cron.php or install.php.
   #if (req.url ~ "^/(cron|install)\.php$" && !client.ip ~ internal) {
     # Have Varnish throw the error directly.
   #  error 404 "Page not found.";
     # Use a custom error page that you've defined in Drupal at the path "404".
     # set req.url = "/404";
   #}

   # Always cache the following file types for all users. This list of extensions
   # appears twice, once here and again in vcl_fetch so make sure you edit both
   # and keep them equal.
   if (req.url ~ "(?i)\.(pdf|asc|dat|txt|doc|xls|ppt|tgz|csv|png|gif|jpeg|jpg|ico|swf|css|js)(\?.*)?$") {
     unset req.http.Cookie;
   }

  # Remove all cookies that Drupal doesn't need to know about. 
    set req.http.Cookie = ";" + req.http.Cookie;
    set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");    
    set req.http.Cookie = regsuball(req.http.Cookie, ";(SESS[a-z0-9]+|SSESS[a-z0-9]+|NO_CACHE)=", "; \1=");
    set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

       if (req.http.Cookie == "") {
         # If there are no remaining cookies, remove the cookie header. If there
         # aren't any cookie headers, Varnish's default behavior will be to cache
         # the page.
         unset req.http.Cookie;
       }
       else {
         # If there is any cookies left (a session or NO_CACHE cookie), do not
         # cache the page. Pass it on to Apache directly.
         return (pass);
       }
   }
}

# Set a header to track a cache HIT/MISS.
 sub vcl_deliver {
   if (obj.hits > 0) {
     set resp.http.X-Varnish-Cache = "HIT";
   }
   else {
     set resp.http.X-Varnish-Cache = "MISS";
   }
}

# Code determining what to do when serving items from the Apache servers.
# beresp == Back-end response from the web server.
sub vcl_fetch {
    # We need this to cache 404s, 301s, 500s. Otherwise, depending on backend but
    # definitely in Drupal's case these responses are not cacheable by default.
    if (beresp.status == 404 || beresp.status == 301 || beresp.status == 500) {
      set beresp.ttl = 10m;
    }

    # Don't allow static files to set cookies.
    # (?i) denotes case insensitive in PCRE (perl compatible regular expressions).
    # This list of extensions appears twice, once here and again in vcl_recv so
    # make sure you edit both and keep them equal.
    if (req.url ~ "(?i)\.(pdf|asc|dat|txt|doc|xls|ppt|tgz|csv|png|gif|jpeg|jpg|ico|swf|css|js)(\?.*)?$") {
      unset beresp.http.set-cookie;
    }
    # Allow items to be stale if needed.
    set beresp.grace = 6h;

# This is how long Varnish will cache content, and what it will set the cache control headers too.
#  set beresp.ttl = 86400s;
#  set beresp.http.Cache-Control = "public, max-age=300";
#  return (deliver);

               # Varnish determined the object was not cacheable
               if (beresp.ttl <= 0s) {
                  set beresp.http.X-Cacheable = "NO:Not Cacheable";

               # You don't wish to cache content for logged in users
               } elsif (req.http.Cookie ~ "(UserID|_session)") {
                   set beresp.http.X-Cacheable = "NO:Got Session";
                   return(hit_for_pass);

               # You are respecting the Cache-Control=private header from the backend
               } elsif (beresp.http.Cache-Control ~ "private") {
                    set beresp.http.X-Cacheable = "NO:Cache-Control=private";
                    return(hit_for_pass);

               # Varnish determined the object was cacheable
               } else {
                   set beresp.http.X-Cacheable = "YES";
               }

               # ....      
               return(deliver);
}
/r/drupal Thread