drupal7+varnish: varnish总是miss的原因

本文详细介绍了如何解决Drupal 7网站中匿名用户访问导致Varnish缓存失效的问题。通过分析Varnish与Drupal 7之间的交互,我们发现了在没有session的情况下,Drupal 7会始终返回no-cache头部并丢弃session cookie。当模板中出现错误,错误字符串被放入$_SESSION时,Drupal 7会开始session操作,从而导致所有匿名访问都返回varnishmiss。文章提供了解决方案,帮助开发者理解并解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



最近发现不知什么原因,匿名用户访问的页面都返回varnish miss,这样造成后台的访问压力非常大。经过几天的debug,总算把问题解决了。下面是问题的总结。

[How to isolate the issue]

If you see the normal anonymous page, it’s HTTP Header always show ‘Cache-Control: public, max-age=xxx’, and can see ‘X-Cache:Hit’.

 

For the problem page, header will be ‘Cache-Control: no-cache,must-revalidate,post-check=0,pre-check=0’, also first access can see ‘Set-Cookie: SESSxxx=xxx’.

 

If drupal7 session cookie exists, following request will always miss cache.

 

[Reason]

For varnish, it depends on apache’s response to determine whether a request will be cached. If apache response header contains ‘Cache-Control: public max-age=xxx’, it will be cached for max-age. If header contains ‘Cache-Control: no-cache’, it will always pass the request to apache.

 

For anonymous user, drupal7 will not start session by default. This is for performance consideration. If session exists, drupal7 will always return no cache header and drop a session cookie.

 

But if any data put into $_SESSION, the user session will be started. Dive into the code, in common.inc, function drupal_page_footer will call session.inc’s function drupal_session_commit(). It will check if any data in $_SESSION then call drupal_session_start() to start a session.

If some errors in template, the error string will put into $_SESSION, then drupal7 will start the session.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值