简体   繁体   中英

Varnish Cache & Shopping Carts

I've been looking into Varnish recently and doing some heavy Google searching. We've recently set this up in anticipation for Christmas on our server that runs an ecommerce site. We'll be having increased return visits over the next few weeks, so using Varnish seemed like a good idea.

I was wondering if someone could clarify something for me - when using Varnish, if a user arrives for the first time it detects a new cookie (as sessions are used for the shopping cart) and caches the pages they visit.

However, if we wanted to cache more than a customers repeat visits, I saw on a seperate stackoverflow thread that editing the .vcl with the following will help it cache pages even if session_start is used:

     sub vcl_recv {
     unset req.http.Cookie;
     return (lookup);
     }

     sub vcl_fetch {
     unset beresp.http.Set-Cookie;
     set beresp.ttl = 24h;
     return(deliver);
     }

My question is - I'm a little confused as to the specifics of -what- would be cached. Would this cache someone's shopping cart as well, thus serving this to all visitors to that product page?

Apologies if I'm missing a basic point of Varnish here; worst case scenario it'll help take the load off for return visits, but caching more of the site would be even better!

Thanks very much in advance

Varnish will not (by default) cache pages with set Cookies. Cookies introduce state to HTTP and thus Varnish can't know whether or not the page has changed.

Your above code snippet though will not fix your issue. It will just remove Cookie headers altogether. (Ie you PHP backend will not receive session cookies.)

Depending on how exactly your site looks like, you would either want to:

  • Leave behavior as is: If you for example have some place on the page saying the username of the logged in user you don't want that to be cached. Basically in this case Varnish will only cache contents for guest (ie users without sessions)
  • If you don't have such a user-dependent box on every page you can cache everything apart from the user-dependent (eg shopping cart) pages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM