清漆:使用一些cookie缓存

时间:2014-07-16 17:27:14

标签: varnish varnish-vcl

我有一个确定客户语言的cookie。 我需要哈希,并告诉Varnish缓存页面,即使包含cookie。

我删除了fetch(backend_response)上的所有cookie,除了我需要的那些。

但是Varnish总是错过缓存,因为我有饼干。

  1. 我需要删除任何ASPSESSIONID cookie,现在将其删除。林 只保留我需要的(语言和货币cookie)。
  2. 我需要“通过”cart.asp(我称之为使用AJAX)
  3. 我需要根据货币cookie和语言cookie缓存页面版本。我需要保留那些饼干。
  4. 提前谢谢

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

1 个答案:

答案 0 :(得分:2)

实现的方法:

  1. 首先获取varnish中的cookie值,并将其分配给名为Language的变体。
  2. 使用vcl_hash中的相同变量使用cookie值创建新哈希。
  3. 取消设置除vcl_recv&中的语言cookie之外的其他cookie 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);    
    }