Varnish cached single "logged in" homepage, how to stop it?

Events happening in the community are now at Drupal community events on www.drupal.org.
jusfeel's picture

I set up Varnish. But it cached the logged in homepage which I think it's because the ending of logging-in would actually redirect user to the homepage but cookie at that time has not been set yet for Varnish to be aware of and "pass" .

Tell me if I understand it wrong. ( After logged in, I visited other pages, they are not cached as it should not. No page should be cached once user logged in. ( What Pressflow cookie "LOGGED_IN" is for ). What happened to other pages should also happen on homepage but it's not. )

More importantly, how to stop this? I think I need to write something into Vanrish vcl config but don't know how.

Comments

What's in your VCL?

gchaix's picture

Can you post your VCL? Or are you using the defaults?
Drupal 6 or Drupal 7?
Do you have the Varnish module installed and configured properly?
What are the settings on the Drupal admin performance page?

VCL and other settings

jusfeel's picture
backend testserver {
   .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);
   }
   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)=[^;]*","");
   // 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;
     }
   }

   # ... 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 (pass);
   }

   // 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.test.com") {
           set req.backend = testserver;
           if (req.request != "GET" && req.request != "HEAD") {
                   return(pipe);
           }
           else {
                   return(lookup);
           }
   }elsif (req.http.host ~ "^www.test2.com") {
           set req.backend = testserver;
           if (req.request != "GET" && req.request != "HEAD") {
                   return(pipe);
           }
           else {
                   return(lookup);
           }
   }
   else {
           error 404 "test 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_hit {
   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;
   }
   else {
         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";
}

It's actually Drupal5.

I have Varnish module installed. There is not much to configure from the backend. Only on the settings.php, added

'reverse_proxy' => TRUE,
'reverse_proxy_addresses' => array(
  '127.0.0.1', // Reverse proxy host A
),

to the conf array.

On the Performance page, cache is disabled. Aggregate and compress CSS files is also disabled.

For varnish to work properly,

cdoyle's picture

For varnish to work properly, you need to replace Drupal core with PF5 (https://launchpad.net/pressflow/5.x). Take a look at http://groups.drupal.org/node/47734. The first version of the vcl in that post was written for varnish 1.x so you would have to modify it to work with 2.x (use beresp).

Thank you

jusfeel's picture

Thank you for the reply. I'll be checking your link. Actually, it's PF5.x now.
One thing I want to achieve is to stop any caching behavior once user logged in. Obviously, Varnish is not doing that with "LOGGED_IN" cookie set already.

Could be...

lassekt's picture

Sounds like you store per user cache. Have you tried this:

# Routine used to determine the cache key if storing/retrieving a cached page.
sub vcl_hash {
  if (req.http.Cookie) {
  # We don't want per user cache
  # set req.hash += req.http.Cookie;
      return (pass);
  }
}

use Global Redirect

mototribe's picture

I used the Global Redirect module with the "force homepage redirect" option to fix the exact same problem. Credits to http://drupal.stackexchange.com/questions/28464/how-to-prevent-caching-o...

update: doesn't work for Firefox

High performance

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: