Search code examples
cookiesvarnishvarnish-vcl

how to use varnish cache with set-cookie named mp3list and phpsessionid


i am new to php and i am interested to use varnish to improve site performance..

i installed varnish latest version : 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

i use cookie named ( mp3list and phpsessionid) so i cant cache my pages,,

i used vcl with

$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);
  }  

Now i removed mp3list using this,, still phpsessid is there,, how to remove 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

can anyone plz guide me how to bypass phpsessid and edit VCL and use varnish effectively

advance thanks for you ........


Solution

  • As you are noticing PHP sessions and Varnish do not intermix very nicely, ie the phpsessions destroys the cachebility of the urls within varnish as it makes them all unique.

    Options: 1. disable php sessions altogether (doubt thats a real option for you). 2. Only enable php sessions on the pages you absolutely need it, so the rest can be cached. 3. split the session-related php logic into separate php files and include those using ESI into your main pages. that will allow you to partially cache the pages (the non-ESI bits).