简体   繁体   中英

Varnish: Cache with some cookies

I have a cookie that determine the language of the customer. I need to hash that and also to tell Varnish to cache the page, even with the included cookies.

I removed all cookies on fetch (backend_response) except the ones that I need.

But Varnish always miss the cache, because I have cookies.

  1. I need to remove any ASPSESSIONID cookie, they are removed now. Im just keeping the ones that i need (language and currency cookies).
  2. I need to "pass" over cart.asp (I call that using AJAX)
  3. I need to cache versions of the page depending on currency cookie and language cookie. I need to keep that cookies.

Thank you in advance

    #
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.

# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "111.111.111.111";
    .port = "80";
}

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 ~ "^/cart\.asp$" ||
      req.url ~ "^/AddtoCartInfo\.asp$" ||
      req.url ~ "^.*/ahah/.*$") {
       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, "^ASP","");

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



}

Approach to achieve:

  1. First get the cookie value in varnish and assign it to a variant called Language.
  2. Use the same variable in vcl_hash to create new hash using cookie value.
  3. Unset Rest of the cookies except Language cookie in vcl_recv & vcl_fetch

     sub identify_cookie{ #Call cookie based detection method in vcl_recv. if (req.http.cookie ~ "language=") { #unset all the cookie from request except language set req.http.Language = regsub(req.http.cookie, "(.*?)(language=)([^;]*)(.*)$", "\\3"); } } sub vcl_recv{ call identify_cookie; if(!req.url ~ "wp-(login|admin)" && !req.http.Cookie ~ "language"){ #unset all the cookie from request except language unset req.http.Cookie; } } sub vcl_fetch { if (!req.url ~ "wp-(login|admin)" && !beresp.http.set-Cookie ~ "language"){ #unset all the cookie from response except language unset beresp.http.set-cookie; } } sub vcl_hash { hash_data(req.url); if (req.http.host) { hash_data(req.http.host); } else { hash_data(server.ip); } if (req.http.Language) { #add cookie in hash hash_data(req.http.Language); } return(hash); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM