drupal7+varnish: varnish总是miss的原因



最近发现不知什么原因,匿名用户访问的页面都返回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.

你可能感兴趣的:(varnish)