簡體   English   中英

清漆:緩存一些cookie

[英]Varnish: Cache with some cookies

我有一個cookie,它確定客戶的語言。 我需要對此進行哈希處理,並告訴Varnish緩存頁面,即使包含了Cookie。

我刪除了所有需要獲取的Cookie(backend_response),但我需要的除外。

但是Varnish總是會錯過緩存,因為我有cookie。

  1. 我需要刪除任何ASPSESSIONID cookie,它們現在已刪除。 我只是保留我需要的語言(語言和貨幣cookie)。
  2. 我需要在cart.asp上“傳遞”(我使用AJAX稱呼它)
  3. 我需要根據貨幣cookie和語言cookie來緩存頁面的版本。 我需要保留這些cookie。

先感謝您

    #
# 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. 首先,獲取清漆中的cookie值,並將其分配給名為Language的變體。
  2. 在vcl_hash中使用相同的變量來使用cookie值創建新的哈希。
  3. 取消設置其余的cookie,但vcl_recv和vcl_fetch中的Language cookie

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM