Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I noticed that logged in user, when clicking on link that leads to the page that is in Varnish cache, sometimes gets that cached page instead user version of that page. If user hits reload button on that page, non-cached version will properly being loaded (which means the cookie is here and he is not logged off).
More or less, i am using this config: https://fourkitchens.atlassian.net/wiki/display/TECH/Configure+Varnish+3...
Thanks for clues!
Comments
Comment #1
heddnIts a browser cache issue. Throw this function in somewhere in your custom site module and call it from MY_MODULE_node_view(). Swap out the filter logic to whatever is appropriate for your site.
Comment #2
heddnComment #3
NenadP CreditAttribution: NenadP commentedThank you for this tip.
But this actually prevents homepage (or any page put in the logic) to be Varnish-ed even for anonymous users. (tested it)-
Almost on every page i have some user-specific elements thaf makes page different for logged in and anonymous user.
I want for logged in users to never be served with cached page while logged in, while logged of users still get cached pages.
Comment #4
heddnWhat you've desribed in #3 as what you want to happen is exactly how Four Kitchen's drupal vcl should work. Authenticated users don't get anything but CSS, images & javascript cached by Varnish per that config. It works this way because the varnish vcl strips off all cookies for css, image and javascript. Anonymous users should get everything cached by varnish because they don't have the cookie in the first place.
The page_no_cache function from #2 tells the browser that it needs to re-fetch the requested content from the remote server. This request will get sent to Varnish and it is up to it to determine if it should serve up the request from its cache or pass it along to the back-end. I add this function on the homepage so that once a user becomes "authenticated" and we redirect them to the homepage, the browser knows it needs to call off to varnish again. This time, remember we are authenticated now, we have our SESS cookie and the homepage renders differently. If we didn't call the above mentioned function on the homepage, then users of the site have to hit F5 or refresh the homepage to see all the new content.
The page_no_cache function will not cause varnish not to cache anything. If varnish isn't caching for anonymous users, then there's something wrong with your vcl. The most common cause for non-caching is when someone messes with the cookie logic. Its there for a purpose and shouldn't be altered unless you have a good reason.
Comment #5
NenadP CreditAttribution: NenadP commentedThanks for your time, please bear with me a little if you have nerve :)
My cookie setup in vcl differs a little. Im not expert for regex and varnish, but here it is:
I added one extra because i have Facebook connect module
Now, my cookies looks like this for anonymous user:
For registered the same, plus
SESS77xxremovedforsecurityxxxa | .mydomain.com
So there is that Facebook connect cookie (and those others), could some of these be reason for strange behaviour ?
(i also noticed difference between regexp for facebook cookie and actual facebook cookie that is set.)
Your function works and i am getting this in httpheaders after visiting the page as registered:
Cache-Control no-cache, must-revalidate, post-check=0, pre-check=0
this one I am getting normally:
Cache-Control public, max-age=10800
But "error" is still there in occasion:
1. User visits the certain page as anonymous
2. Logs in
3. Clicks the link of the page that he visitied as anonymous previosly, he gets cached page (ideally, that should not happen)
4. If he reloads page (f5), he gets backend served page, and further clicking on the link serves hem from backend now (as he have headers altered now).
Thanks for your patience.
Comment #6
heddnThe addition of
set req.http.Cookie = regsuball(req.http.Cookie, ";(SESS[a-z0-9]+|fbs_[a-z0-9]+|NO_CACHE)=", "; \1=");
effectively breaks varnish caching. Remove it from your vcl and caching will start working again. Varnish doesn't work very well with authenticated users. Which is what you are trying to do when you tell it to accept the facebook cookie. You aren't authenticated to drupal, but you are to facebook. However, its the same difference to varnish.
Comment #7
NenadP CreditAttribution: NenadP commentedRemoved the fb cookie, but this is not primary source of my trouble :(.
Your code foces revalidate code direction to be imprinted in http headers. But i need to force logged in user to always get updated page, not from Varnish, for all pages.
So i modified your code this way:
But this now force the visitor, logged in or logged out, to have served pages from backend, no Varnish pages are shown. Now i am even more confused...
I somehow need to tell the browser to forget page that was viewed as anonymous user, once user logs in.
Comment #9
jchin1968 CreditAttribution: jchin1968 commentedTry setting $conf['omit_vary_cookie'] = FALSE in your settings.php file.
Comment #10
ifish CreditAttribution: ifish commentedHas anyone fixed this problem? Having exactly the same problem. Tried so many different things, nothing helps.
Comment #11
ifish CreditAttribution: ifish commentedComment #12
ifish CreditAttribution: ifish commentedThe problem I'm having right now is that I'm trying to use annonymous caching with varnish.
That's the current problem:
User visits some pages such as Home, News, Forum.
User logs in
User visits these pages again (Home, News, Forum)
And when he visits these pages, they're all cached to the clients local browser. Here's the header:
Request URL:http://terrangaming.com/
Request Method:GET
Status Code:200 OK (from cache)
--
What can I do to prevent this from happening? www.terrangaming.com
I've already made these changes to my settings.php but doesn't seem to help:
$conf['cache_backends'] = array('sites/all/modules/varnish/varnish.cache.inc');
$conf['cache_class_cache_page'] = 'VarnishCache';
$conf['reverse_proxy'] = TRUE;
$conf['page_cache_invoke_hooks'] = FALSE;
$conf['cache'] = 1;
$conf['cache_lifetime'] = 0;
$conf['reverse_proxy_addresses'] = array('127.0.0.1');
$conf['omit_vary_cookie'] = FALSE;
Any tips would be great. Thanks in advance.
Comment #13
heddnifish, have you tried #1 (further expounded in #4)?
Comment #14
ifish CreditAttribution: ifish commentedNo, the browser still stores it locally
Request URL:http://terrangaming.com/
Request Method:GET
Status Code:200 OK (from cache)
Tried custom module:
Comment #15
ifish CreditAttribution: ifish commentedIve just thought about meta tags solution. Does anyone know how caching can be disabled with meta tags in drupal?
Comment #16
heddnI don't see 'no-cache, no-store, must-revalidate' cache-control headers when I access http://terrangaming.com/. Have you flushed cache?
Comment #17
ifish CreditAttribution: ifish commentedThat's odd, just incase I cleared caches and even ran cron for 3 times. It doesn't seem to work. I'm not familiar with drupal API at all, is the PHP code correct?
Comment #18
ifish CreditAttribution: ifish commentedFinally got it to working, atleast seems so. The fix was this to modify htaccess if someone is interested:
Comment #19
ifish CreditAttribution: ifish commentedFix:
Comment #20
philsward CreditAttribution: philsward commented@ifish Wish I could say that fixed my issue as well, but I've run into other issues along with not fixing the original issue... Authenticated pages are still being cached as anonymous and if I reload the browser cache, Varnish seems to be caching the authenticated page for anonymous.
Can't win :-/
Comment #21
philsward CreditAttribution: philsward commentedOn a side note, I decided to drop my cache TTL from 300 seconds down to (what came as the default) 20 seconds and my problem might be gone... Time will tell. (dropped all of the .htaccess settings proposed by @ifish and running the normal .htaccess config)
Comment #22
alasda CreditAttribution: alasda commentedCan disable browser cache in varnish config, and only allow for certain items to be cached by browser:
Comment #23
serg2 CreditAttribution: serg2 commentedThere is a note in settings.php that says:
Most guides suggest adding
$conf['omit_vary_cookie'] = TRUE;
but have you tried with it set to false?EDIT: OOPS, just re-read and saw comment #9