Why do I have this feeling that Varnish is caching the pages only visited by same user

StackOverflow https://stackoverflow.com//questions/9595410

  •  08-12-2019
  •  | 
  •  

Question

After started Varnish, I visited the website once. After populating all pages, revisiting all the pages become so fast.

But when I switch to another computer and visit the site, it seems I have to start again this cache populating process.

So it seems that Varnish is not serving cached pages cached by the other user

I hope that I have addressed my question clearly.

Does anyone knows what happened.

Here is the vcl file

backend Tserver {
   .host = "127.0.0.1";
   .port = "8080";
}

acl purge {
   "localhost";
   "127.0.0.1";
   "192.168.3.0"/24;
}

sub vcl_recv {
   if (req.request == "PURGE") {
           if (!client.ip ~ purge) {
                   error 405 "Not allowed.";
           }
           return(lookup);
   }

   #without this, the whole logged in status is messed up
   remove req.http.X-Forwarded-For;
   set req.http.X-Forwarded-For = client.ip;

   // Remove has_js and Google Analytics cookies
   set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js)=[^;]*","");

   #if(req.http.Authorization || req.http.Cookie) {
   #  /* Not cachable by default */
   #  return (pass);
   #}

   // remove a ";" prefix, if present
   #set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");

   // remove empty cookies.
   #if (req.http.Cookie ~ "^\s*$") {
   #  unset req.http.Cookie;
   #}

   // Skip the Vanish cache for install, update, and cron
   if (req.url ~ "install\.php|update\.php|cron\.php") {
     return (pass);
   }


   # Normalize Accept-Encoding to get better cache coherency
   if (req.http.Accept-Encoding) {
     # No point in compressing media that is already compressed
     if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
       remove req.http.Accept-Encoding;
       # MSIE 6 JS bug workaround
     } elsif(req.http.User-Agent ~ "MSIE 6") {
       unset 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 {
       # unkown algorithm
       remove req.http.Accept-Encoding;
     }
   }

   #if (req.url ~ "^/classifieds") {
   #  return(pipe);
   #}

   #if (req.url ~ "^/logout") {
   #  return(pipe);
   #}

   #if (req.url ~ "^/admin") {
   #  return(pipe);
   #}

   # Remove the incoming Cookie header from anonymous requests
   # This is from Drupal 7 book, not sure if this works here
   #if (req.http.Cookie !~ "(^|;\s*)SESS") {
   #  unset req.http.Cookie;
   #}
/*
  if (req.http.Cookie) {
    set req.http.Cookie = ";" req.http.Cookie;
    set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
    set req.http.Cookie = regsuball(req.http.Cookie, ";(LOGGED_IN|VARNISH)=", "; \1=");
    set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
    set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

    if (req.http.Cookie == "") {
        remove req.http.Cookie;
    }
  }
*/

   # ... other vcl_recv rules here ...
   # Don't serve cached content to logged-in users
   # Don't cache Drupal logged-in user sessions
   # LOGGED_IN is the cookie that earlier version of Pressflow sets
   # VARNISH is the cookie which the varnish.module sets

   if (req.http.Cookie ~ "(VARNISH|DRUPAL_UID|LOGGED_IN)") {
     return (pipe);
   }

   // Let's have a little grace
   // When backend cannot generate refreshed content
   // this time will allow expired content to stay longer in grace
   set req.grace = 0s;

   if (req.http.host ~ "^www.test1.com") {
           set req.backend = Tserver;
           if (req.request != "GET" && req.request != "HEAD") {
                   return(pipe);
           }
           else {
                   return(lookup);
           }
   }elsif (req.http.host ~ "^www.test2.com") {
           set req.backend = Tserver;
           if (req.request != "GET" && req.request != "HEAD") {
                   return(pipe);
           }
           else {
                   return(lookup);
           }
   }
   else {
           error 404 "T Cache Server IS Out of Order";
           return(lookup);
   }

   # Drupal js/css doesn't need cookies, cache them
   if (req.url ~ "^/modules/.*\.(js|css)\?") {
           unset req.http.Cookie;
   }


   ## Pass cron jobs and server-status
   if (req.url ~ "cron.php") {
           return (pass);
   }
   if (req.url ~ ".*/server-status$") {
           return (pass);
   }

}

sub vcl_hash {
  if (req.http.Cookie) {
    set req.hash += req.http.Cookie;
  }
}

sub vcl_hit {
   if (req.http.Cookie ~ "LOGGED_IN") {
           set obj.ttl = 0s;
           return (pass);
   }

   if (req.request == "PURGE") {
           set obj.ttl = 0s;
           error 200 "Purged.";
   }
}

sub vcl_miss {
   if (req.request == "PURGE") {
           error 404 "Not in cache.";
   }
}

sub vcl_fetch {
   if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
     unset beresp.http.set-cookie;
   }


   #if (beresp.http.Pragma ~ "nocache") {
   #  return(pass);
   #}

   if (req.request == "GET" && req.url ~ "\.(txt|js)$") {
          set beresp.ttl = 3600s;
   }
   elseif (req.http.Cookie ~ "LOGGED_IN") {
          set beresp.ttl = 0s;
  }
  else {
          // cache time is 30 days then in varnish
          set beresp.ttl = 30d;
   }

}

sub vcl_error {
  set obj.http.Content-Type = "text/html; charset=utf-8";
  set obj.http.Retry-After = "5";
  synthetic {"<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><title>"} obj.status " " obj.response {"</title></head><body><h1>Error "} obj.status " " obj.response {"</h1><p>"} obj.response {"</p><h3>Guru Meditation:</h3><p>XID: "} req.xid {"</p><hr><p>Varnish cache server</p></body></html>"};
  return (deliver);
}

sub vcl_pipe {
  # http://www.varnish-cache.org/ticket/451
  # This forces every pipe request to be the first one.
  set bereq.http.connection = "close";
}
Was it helpful?

Solution

Do you have a user-specific (session) cookie, even if the user is logged out?

OTHER TIPS

Your backend probably spits out "Vary: User-Agent" or "Vary: Accept-Encoding" or something similar. This basically tells Varnish to cache stuff separately for each user. Fix the backend or normalize the header in question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top