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

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

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

我安装的是Varnish的最新版本:4.0.2Varnish

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

mp3list我使用名为 (和)的 cookie 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,以有效地使用 varnish 吗?

答案1

我不太清楚你想要什么,你想删除 PHPSESSID cookie?这里有一个指南; https://www.varnish-cache.org/trac/wiki/VCLExampleRemovingSomeCookies

相关内容