這段清漆程式碼有什麼作用?

這段清漆程式碼有什麼作用?

我在清漆配置中有這段程式碼,但不確定它的作用!此配置是否會快取我的客戶端請求?有什麼問題嗎?

sub vcl_backend_response {
    if (beresp.status != 200) {
        return (pass);
    }
    set beresp.http.X-Backend = beresp.backend.name;


    unset beresp.http.cookie;
    unset beresp.http.Set-Cookie;

    if (bereq.http.x-render-type == "test" && beresp.http.Content-Type ~ "text/html") {
        set beresp.http.Cache-Control = "no-store";
    }

    set beresp.http.Cache-Control = "no-store";
    if (bereq.http.x-render-type == "test" && beresp.http.Content-Type ~ "text/html") {
        return (pass);
    }

    return (deliver);
}

答案1

這段 VCL 程式碼似乎指定了一些關於何時繞過快取的規則。然而,它的寫法並沒有太大意義。

繞過快取

return(pass)不是繞過vcl_backend_response上下文中快取的正確方法。當傳入請求繞過快取時return(pass)使用。vcl_recv

繞過vcl_backend_response快取意味著阻止將傳入物件儲存在快取中。最佳實踐要求您這樣做set beresp.uncacheable = true,分配一個 TTL,然後return(deliver)。這可以確保該物件在一定時間內繞過緩存,直到下一個後端響應滿足所需的條件。

我的啟用beresp.uncacheable,您確保該物件最終出現在等待清單中並成為請求合併的候選人。

刪除cookie

刪除 cookie 通常有助於提高命中率。在後端上下文中,您將刪除Set-Cookie標頭。這是在vcl_backend_responsethrough中正確完成的unset beresp.http.Set-Cookie,但是這是無條件完成的。

這意味著Set-Cookie不會發生任何操作,這可能會導致行為不一致。不確定刪除這些 cookie 是否需要先決條件。

您也可以透過刪除傳入的 cookie unset req.http.Cookie。但vcl_backend_response運行中似乎有類似的呼叫unset beresp.http.Cookie

這表示Cookie將收到響應標頭。這似乎不太可能。

重寫VCL

這就是我在沒有任何其他上下文的情況下重寫此 VCL 程式碼的方式:

vcl 4.1;

backend default {
    .host = "127.0.0.1";
    .port = "80";
}

sub vcl_recv {
    unset req.http.Cookie;
}

sub vcl_backend_response {
    if(beresp.status != 200) {
        set beresp.ttl = 120s;
        set beresp.uncacheable = true;
        return(deliver);
    }

    set beresp.http.X-Backend = beresp.backend.name;
    unset beresp.http.Set-Cookie;
    set beresp.http.Cache-Control = "no-store";

    if (bereq.http.x-render-type == "test" && beresp.http.Content-Type ~ "text/html") {
        set beresp.ttl = 120s;
        set beresp.uncacheable = true;
        return(deliver);
    }

    return (deliver);
}

警告:我不建議將其複製/貼上到您的生產環境中。我有一種感覺,在寫這個 VCL 時,有些地方被偷工減料了。由於我沒有任何其他上下文,因此我不想建議使用它。

相關內容