Varnish Cache & Shopping Carts

我们两清 提交于 2019-12-07 14:35:50

问题


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


回答1:


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. (I.e. 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 (i.e. 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 (e.g. shopping cart) pages.


来源:https://stackoverflow.com/questions/7766729/varnish-cache-shopping-carts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!