如何使用名为mp3list和phpsessionid的set-cookie使用varnish cache

时间:2014-12-06 08:08:01

标签: cookies varnish varnish-vcl

我是php的新手,我有兴趣使用清漆来提高网站性能。

我安装了varnish最新版本:4.0.2 varnish

HTTP/1.1 200 OK Date: Sat, 06 Dec 2014 07:24:47 GMT Server: Apache/2.2.29 (Unix) mod_ssl/2.2.29 OpenSSL/1.0.1e-fips mod_bwlimited/1.4 X-Powered-By: PHP/5.4.34 Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Set-Cookie: PHPSESSID=86dc704d405a80d0c012de043cd9408b; path=/ Set-Cookie: Mp3List=WDVMG2G4; expires=Tue, 03-Dec-2024 07:24:47 GMT Vary: Accept-Encoding Content-Type: text/html X-Varnish: 2 Age: 0 Via: 1.1 varnish-v4 Connection: keep-alive

我使用cookie命名(mp3list和phpsessionid)所以我无法缓存我的页面,

我用vcl和

$sub vcl_recv {
  call normalize_req_url;
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
# If the requested URL starts like "/cart.asp" then immediately pass it to the given
# backend and DO NOT cache the result ("pass" basically means "bypass the cache").
if (req.url ~ "^/playlist\.php$") {
   return (pass);
   }
}
sub vcl_backend_response {
 # Happens after we have read the response headers from the backend.
 #
 # Here you clean the response headers, removing silly Set-Cookie headers
 # and other mistakes your backend does.
 if (beresp.http.Set-Cookie)
 {
  set beresp.http.Set-Cookie = regsub(beresp.http.Set-Cookie, "^php","");
  if (beresp.http.Set-Cookie == "")
    {
        unset beresp.http.Set-Cookie;
    }
  }
 unset beresp.http.Cache-Control;
 set beresp.http.Cache-Control = "public";
 }
 sub vcl_deliver {
 # Happens when we have all the pieces we need, and are about to send the
 # response to the client.
 #
 # You can do accounting or modifying the final object here.
 # Was a HIT or a MISS?

 if ( obj.hits > 0 )
 {
  set resp.http.X-Cache = "HIT";
 }
 else
 {
  set resp.http.X-Cache = "MISS";
 } 
 # And add the number of hits in the header:
  set resp.http.X-Cache-Hits = obj.hits;
 }
 sub normalize_req_url {
 # Strip out Google Analytics campaign variables. They are only needed
 # by the javascript running on the page
 # utm_source, utm_medium, utm_campaign, gclid, ...
 if(req.url ~ "(\?|&)(gclid|cx|ie|cof|siteurl|zanpid|origin|utm_[a-z]+|mr:[A-z]+)=") {
    set req.url = regsuball(req.url, "(gclid|cx|ie|cof|siteurl|zanpid|origin|utm_[a-z]+|mr:[A-               z]+)=[%.-_A-z0-9]+&?", "");
  }
  set req.url = regsub(req.url, "(\?&?)$", "");
  }
  sub vcl_hash {
  hash_data(req.url);
  if (req.http.host) {
  hash_data(req.http.host);
  } else {
  hash_data(server.ip);
  }
  hash_data(req.http.Cookie);
  }  

现在我用这个删除了mp3list,还有phpsessid就在那里,如何删除phpsessid

HTTP/1.1 200 OK
Date: Sat, 06 Dec 2014 07:44:46 GMT
Server: Apache/2.2.29 (Unix) mod_ssl/2.2.29 OpenSSL/1.0.1e-fips mod_bwlimited/1.4
X-Powered-By: PHP/5.4.34
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Vary: Accept-Encoding
Content-Type: text/html
Set-Cookie: PHPSESSID=ce934af9a97bd7d0fd14304bd49f8fe2; path=/
Cache-Control: public
X-Varnish: 163843
Age: 0
Via: 1.1 varnish-v4 
X-Cache: MISS
X-Cache-Hits: 0
Connection: keep-alive

任何人都可以指导我如何绕过phpsessid并编辑VCL并有效使用清漆

预先谢谢你........

1 个答案:

答案 0 :(得分:0)

正如您注意到PHP会话和Varnish不能很好地混合,即phpsessions会破坏varnish中url的可缓存性,因为它使它们都是唯一的。

选项: 1.完全禁用php会话(怀疑这是一个真正的选择)。 2.只在您绝对需要的页面上启用php会话,以便其余的可以缓存。 3.将与会话相关的php逻辑拆分为单独的php文件,并将ESI包含在主页中。这将允许您部分缓存页面(非ESI位)。

相关问题