Varnish 保留缓存的时间不超过几分钟

Varnish 保留缓存的时间不超过几分钟

我正在运行一个 Magento 2 网站,该网站运行非常慢,因为 Varnish 不会将其缓存保留超过几分钟。这是 default.vcl 文件:

# VCL version 5.0 is not supported so it should be 4.0 even though actually used Varnish version is 6
vcl 4.0;

import std;
# The minimal Varnish version is 6.0
# For SSL offloading, pass the following header in your proxy server or load balancer: 'SSL-OFFLOADED: https'

backend default {
    .host = "172.26.12.6";
    .port = "8080";
    .first_byte_timeout = 600s;
    .probe = {
        .url = "/health_check.php";
        .timeout = 2s;
        .interval = 5s;
        .window = 10;
        .threshold = 5;
   }
}

acl purge {
    "172.26.12.6";
}

sub vcl_recv {

    if (req.restarts > 0) {
       set req.hash_always_miss = true;
    }

    if (req.method == "PURGE") {
        if (client.ip !~ purge) {
            return (synth(405, "Method not allowed"));
        }
        # To use the X-Pool header for purging varnish during automated deployments, make sure the X-Pool header
        # has been added to the response in your backend server config. This is used, for example, by the
        # capistrano-magento2 gem for purging old content from varnish during it's deploy routine.
        if (!req.http.X-Magento-Tags-Pattern && !req.http.X-Pool) {
            return (synth(400, "X-Magento-Tags-Pattern or X-Pool header required"));
        }
        if (req.http.X-Magento-Tags-Pattern) {
          ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
        }
        if (req.http.X-Pool) {
          ban("obj.http.X-Pool ~ " + req.http.X-Pool);
        }
        return (synth(200, "Purged"));
    }

    if (req.method != "GET" &&
        req.method != "HEAD" &&
        req.method != "PUT" &&
        req.method != "POST" &&
        req.method != "TRACE" &&
        req.method != "OPTIONS" &&
        req.method != "DELETE") {
          /* Non-RFC2616 or CONNECT which is weird. */
          return (pipe);
    }

    # We only deal with GET and HEAD by default
    if (req.method != "GET" && req.method != "HEAD") {
        return (pass);
    }

    # Bypass shopping cart, checkout and search requests
    if (req.url ~ "/checkout" || req.url ~ "/catalogsearch") {
        return (pass);
    }

    # Bypass health check requests
    if (req.url ~ "/health_check.php") {
        return (pass);
    }

    # Set initial grace period usage status
    set req.http.grace = "none";

    # normalize url in case of leading HTTP scheme and domain
    set req.url = regsub(req.url, "^http[s]?://", "");

    # collect all cookies
    std.collect(req.http.Cookie);

    # Compression filter. See https://www.varnish-cache.org/trac/wiki/FAQ/Compression
    if (req.http.Accept-Encoding) {
        if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") {
            # No point in compressing these
            unset req.http.Accept-Encoding;
        } elsif (req.http.Accept-Encoding ~ "gzip") {
            set req.http.Accept-Encoding = "gzip";
        } elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") {
            set req.http.Accept-Encoding = "deflate";
        } else {
            # unknown algorithm
            unset req.http.Accept-Encoding;
        }
    }

    # Remove all marketing get parameters to minimize the cache objects
    if (req.url ~ "(\?|&)(gclid|cx|ie|cof|siteurl|zanpid|origin|fbclid|mc_[a-z]+|utm_[a-z]+|_bta_[a-z]+)=") {
        set req.url = regsuball(req.url, "(gclid|cx|ie|cof|siteurl|zanpid|origin|fbclid|mc_[a-z]+|utm_[a-z]+|_bta_[a-z]+)=[-_A-z0-9+()%.]+&?", "");
        set req.url = regsub(req.url, "[?|&]+$", "");
    }

    # Static files caching
    if (req.url ~ "^/(pub/)?(media|static)/") {
        # Static files should not be cached by default
        return (pass);

        # But if you use a few locales and don't use CDN you can enable caching static files by commenting previous line (#return (pass);) and uncommenting next 3 lines
        #unset req.http.Https;
        #unset req.http.SSL-OFFLOADED;
        #unset req.http.Cookie;
    }


    return (hash);
}

sub vcl_hash {
    if (req.http.cookie ~ "X-Magento-Vary=") {
        hash_data(regsub(req.http.cookie, "^.*?X-Magento-Vary=([^;]+);*.*$", "\1"));
    }

    # For multi site configurations to not cache each other's content
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }

    # To make sure http users don't see ssl warning
    if (req.http.SSL-OFFLOADED) {
        hash_data(req.http.SSL-OFFLOADED);
    }
    

    if (req.url ~ "/graphql") {
        call process_graphql_headers;
    }
}

sub process_graphql_headers {
    if (req.http.Store) {
        hash_data(req.http.Store);
    }
    if (req.http.Content-Currency) {
        hash_data(req.http.Content-Currency);
    }
}

sub vcl_backend_response {

    set beresp.grace = 3d;

    if (beresp.http.content-type ~ "text") {
        set beresp.do_esi = true;
    }

    if (bereq.url ~ "\.js$" || beresp.http.content-type ~ "text") {
        set beresp.do_gzip = true;
    }

    if (beresp.http.X-Magento-Debug) {
        set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control;
    }

    # cache only successfully responses and 404s
    if (beresp.status != 200 && beresp.status != 404) {
        set beresp.ttl = 0s;
        set beresp.uncacheable = true;
        return (deliver);
    } elsif (beresp.http.Cache-Control ~ "private") {
        set beresp.uncacheable = true;
        set beresp.ttl = 604800s;
        return (deliver);
    }

    # validate if we need to cache it and prevent from setting cookie
    if (beresp.ttl > 0s && (bereq.method == "GET" || bereq.method == "HEAD")) {
        unset beresp.http.set-cookie;
    }

   # If page is not cacheable then bypass varnish for 2 minutes as Hit-For-Pass
   if (beresp.ttl <= 0s ||
       beresp.http.Surrogate-control ~ "no-store" ||
       (!beresp.http.Surrogate-Control &&
       beresp.http.Cache-Control ~ "no-cache|no-store") ||
       beresp.http.Vary == "*") {
        # Mark as Hit-For-Pass for the next 2 minutes
        set beresp.ttl = 120s;
        set beresp.uncacheable = true;
    }

    return (deliver);
}

sub vcl_deliver {
    if (resp.http.X-Magento-Debug) {
        if (resp.http.x-varnish ~ " ") {
            set resp.http.X-Magento-Cache-Debug = "HIT";
            set resp.http.Grace = req.http.grace;
        } else {
            set resp.http.X-Magento-Cache-Debug = "MISS";
        }
    } else {
        unset resp.http.Age;
    }

    # Not letting browser to cache non-static files.
    if (resp.http.Cache-Control !~ "private" && req.url !~ "^/(pub/)?(media|static)/") {
        set resp.http.Pragma = "no-cache";
        set resp.http.Expires = "-1";
        set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, max-age=0";
    }

    unset resp.http.X-Magento-Debug;
    unset resp.http.X-Magento-Tags;
    unset resp.http.X-Powered-By;
    unset resp.http.Server;
    unset resp.http.X-Varnish;
    unset resp.http.Via;
    unset resp.http.Link;
}

sub vcl_hit {
    if (obj.ttl >= 0s) {
        # Hit within TTL period
        return (deliver);
    }
    if (std.healthy(req.backend_hint)) {
        if (obj.ttl + 300s > 0s) {
            # Hit after TTL expiration, but within grace period
            set req.http.grace = "normal (healthy server)";
            return (deliver);
        } else {
            # Hit after TTL and grace expiration
            return (restart);
        }
    } else {
        # server is not healthy, retrieve from cache
        set req.http.grace = "unlimited (unhealthy server)";
        return (deliver);
    }
}

当我加载任何页面时,它大约需要 5-8 秒才能加载。在接下来的 5-10 分钟内,它将在大约 150 毫秒内加载。大约 10 分钟后,它将恢复到大约 5-8 秒的加载时间,并继续循环。我已在管理面板中将其设置为将缓存保留 1 周。它还有 3GB 的内存可供使用,几乎不会使用超过 30MB 的内存。

我不确定是什么原因造成的。如能提供任何帮助,我将不胜感激。

更新 1:

当 Varnish 缓存该页面时,以下是 Varnishlog:

*   << Request  >> 73852     
-   Begin          req 73851 rxreq
-   Timestamp      Start: 1621443817.046353 0.000000 0.000000
-   Timestamp      Req: 1621443817.046353 0.000000 0.000000
-   VCL_use        boot
-   ReqStart       172.26.12.6 41170 a0
-   ReqMethod      GET
-   ReqURL         /exampleproduct.html
-   ReqProtocol    HTTP/1.0
-   ReqHeader      Host: example.com
-   ReqHeader      X-Forwarded-Host: example.com
-   ReqHeader      X-Real-IP: 172.69.142.27
-   ReqHeader      X-Forwarded-For: 2601:204:dd00:94c:20c:29ff:fef6:270b, 172.69.142.27
-   ReqHeader      Ssl-Offloaded: 1
-   ReqHeader      X-Forwarded-Proto: https
-   ReqHeader      X-Forwarded-Port: 443
-   ReqHeader      Connection: close
-   ReqHeader      Accept-Encoding: gzip
-   ReqHeader      CF-IPCountry: US
-   ReqHeader      CF-RAY: 651ee64ff9455f88-SMF
-   ReqHeader      CF-Visitor: {"scheme":"https"}
-   ReqHeader      cache-control: max-age=0
-   ReqHeader      sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"
-   ReqHeader      sec-ch-ua-mobile: ?0
-   ReqHeader      upgrade-insecure-requests: 1
-   ReqHeader      user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
-   ReqHeader      accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
-   ReqHeader      sec-fetch-site: same-origin
-   ReqHeader      sec-fetch-mode: navigate
-   ReqHeader      sec-fetch-user: ?1
-   ReqHeader      sec-fetch-dest: document
-   ReqHeader      referer: https://example.com/exampleproduct.html
-   ReqHeader      accept-language: en-US,en;q=0.9,es-ES;q=0.8,es;q=0.7
-   ReqHeader      cookie: _gid=GA1.2.1875408680.1621020942; mage-messages=; cf_clearance=bbfb28e3e23443eceb422f7ee7f35f9fe10d7f1b-1621358137-0-150; form_key=nKb2t0FfKDugritm; mage-cache-storage=%7B%7D; mage-cache-storage-section-invalidation=%7B%7D; PHPSESSID=nif7ckijs0ha
-   ReqHeader      CF-Connecting-IP: 2601:204:dd00:94c:20c:29ff:fef6:270b
-   ReqHeader      CDN-Loop: cloudflare
-   ReqHeader      cf-request-id: 0a272e45fd00005f8890abd000000001
-   ReqUnset       X-Forwarded-For: 2601:204:dd00:94c:20c:29ff:fef6:270b, 172.69.142.27
-   ReqHeader      X-Forwarded-For: 2601:204:dd00:94c:20c:29ff:fef6:270b, 172.69.142.27, 172.26.12.6
-   VCL_call       RECV
-   ReqHeader      grace: none
-   ReqURL         /exampleproduct.html
-   ReqUnset       Accept-Encoding: gzip
-   ReqHeader      Accept-Encoding: gzip
-   VCL_return     hash
-   VCL_call       HASH
-   VCL_return     lookup
-   Hit            530763 1209209.552413 259200.000000 0.000000
-   VCL_call       HIT
-   VCL_return     deliver
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Server: nginx/1.18.0 (Ubuntu)
-   RespHeader     Date: Wed, 19 May 2021 16:57:06 GMT
-   RespHeader     Content-Type: text/html; charset=UTF-8
-   RespHeader     Vary: Accept-Encoding
-   RespHeader     Pragma: cache
-   RespHeader     Cache-Control: max-age=1209600, public, s-maxage=1209600
-   RespHeader     Expires: Wed, 02 Jun 2021 16:57:03 GMT
-   RespHeader     X-Magento-Tags: store,cms_b,ubmegamenu_group,ubmegamenu_item,cms_b_contact-numbers,cms_b_google_translate,cms_b_company-info,cms_b_customer-support,cms_b_contact-us,cat_p_1380,cat_p,cat_p_1923,cat_p_2270,cat_p_2281,cat_p_2287,cat_p_1920,cms_b_gtm_header,
-   RespHeader     X-Content-Type-Options: nosniff
-   RespHeader     X-XSS-Protection: 1; mode=block
-   RespHeader     X-Frame-Options: SAMEORIGIN
-   RespHeader     Access-Control-Allow-Origin: *
-   RespHeader     Content-Encoding: gzip
-   RespHeader     X-Varnish: 73852 530763
-   RespHeader     Age: 390
-   RespHeader     Via: 1.1 varnish (Varnish/6.2)
-   VCL_call       DELIVER
-   RespUnset      Age: 390
-   RespUnset      Pragma: cache
-   RespHeader     Pragma: no-cache
-   RespUnset      Expires: Wed, 02 Jun 2021 16:57:03 GMT
-   RespHeader     Expires: -1
-   RespUnset      Cache-Control: max-age=1209600, public, s-maxage=1209600
-   RespHeader     Cache-Control: no-store, no-cache, must-revalidate, max-age=0
-   RespUnset      X-Magento-Tags: store,cms_b,ubmegamenu_group,ubmegamenu_item,cms_b_contact-numbers,cms_b_google_translate,cms_b_company-info,cms_b_customer-support,cms_b_contact-us,cat_p_1380,cat_p,cat_p_1923,cat_p_2270,cat_p_2281,cat_p_2287,cat_p_1920,cms_b_gtm_header,
-   RespUnset      Server: nginx/1.18.0 (Ubuntu)
-   RespUnset      X-Varnish: 73852 530763
-   RespUnset      Via: 1.1 varnish (Varnish/6.2)
-   VCL_return     deliver
-   Timestamp      Process: 1621443817.046443 0.000089 0.000089
-   Filters        
-   RespHeader     Accept-Ranges: bytes
-   RespHeader     Content-Length: 35904
-   RespHeader     Connection: close
-   Timestamp      Resp: 1621443817.046527 0.000174 0.000085
-   ReqAcct        2365 0 2365 428 35904 36332
-   End      

  

这是大约 10 分钟后 Varnish 没有缓存该页面时的页面(我认为它捕获了两个请求,这是一个活跃的网站):

*   << Request  >> 397487    
-   Begin          req 397486 rxreq
-   Timestamp      Start: 1621444231.047850 0.000000 0.000000
-   Timestamp      Req: 1621444231.047850 0.000000 0.000000
-   VCL_use        boot
-   ReqStart       172.26.12.6 58138 a0
-   ReqMethod      GET
-   ReqURL         /exampleproduct.html
-   ReqProtocol    HTTP/1.0
-   ReqHeader      Host: example.com
-   ReqHeader      X-Forwarded-Host: example.com
-   ReqHeader      X-Real-IP: 172.69.142.57
-   ReqHeader      X-Forwarded-For: 2601:204:dd00:94c:20c:29ff:fef6:270b, 172.69.142.57
-   ReqHeader      Ssl-Offloaded: 1
-   ReqHeader      X-Forwarded-Proto: https
-   ReqHeader      X-Forwarded-Port: 443
-   ReqHeader      Connection: close
-   ReqHeader      Accept-Encoding: gzip
-   ReqHeader      CF-IPCountry: US
-   ReqHeader      CF-RAY: 651ef06b6a105ffa-SMF
-   ReqHeader      CF-Visitor: {"scheme":"https"}
-   ReqHeader      cache-control: max-age=0
-   ReqHeader      sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"
-   ReqHeader      sec-ch-ua-mobile: ?0
-   ReqHeader      upgrade-insecure-requests: 1
-   ReqHeader      user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
-   ReqHeader      accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
-   ReqHeader      sec-fetch-site: same-origin
-   ReqHeader      sec-fetch-mode: navigate
-   ReqHeader      sec-fetch-user: ?1
-   ReqHeader      sec-fetch-dest: document
-   ReqHeader      referer: https://example.com/exampleproduct.html
-   ReqHeader      accept-language: en-US,en;q=0.9,es-ES;q=0.8,es;q=0.7
-   ReqHeader      cookie: _gid=GA1.2.1875408680.1621020942; mage-messages=; cf_clearance=bbfb28e3e23443eceb422f7ee7f35f9fe10d7f1b-1621358137-0-150; form_key=nKb2t0FfKDugritm; mage-cache-storage=%7B%7D; mage-cache-storage-section-invalidation=%7B%7D; PHPSESSID=nif7ckijs0ha
-   ReqHeader      CF-Connecting-IP: 2601:204:dd00:94c:20c:29ff:fef6:270b
-   ReqHeader      CDN-Loop: cloudflare
-   ReqHeader      cf-request-id: 0a2734971e00005ffa220b9000000001
-   ReqUnset       X-Forwarded-For: 2601:204:dd00:94c:20c:29ff:fef6:270b, 172.69.142.57
-   ReqHeader      X-Forwarded-For: 2601:204:dd00:94c:20c:29ff:fef6:270b, 172.69.142.57, 172.26.12.6
-   VCL_call       RECV
-   ReqHeader      grace: none
-   ReqURL         /exampleproduct.html
-   ReqUnset       Accept-Encoding: gzip
-   ReqHeader      Accept-Encoding: gzip
-   VCL_return     hash
-   VCL_call       HASH
-   VCL_return     lookup
-   VCL_call       MISS
-   VCL_return     fetch
-   Link           bereq 397488 fetch
-   Timestamp      Fetch: 1621444234.406568 3.358718 3.358718
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Server: nginx/1.18.0 (Ubuntu)
-   RespHeader     Date: Wed, 19 May 2021 17:10:34 GMT
-   RespHeader     Content-Type: text/html; charset=UTF-8
-   RespHeader     Vary: Accept-Encoding
-   RespHeader     Pragma: cache
-   RespHeader     Cache-Control: max-age=1209600, public, s-maxage=1209600
-   RespHeader     Expires: Wed, 02 Jun 2021 17:10:31 GMT
-   RespHeader     X-Magento-Tags: store,cms_b,ubmegamenu_group,ubmegamenu_item,cms_b_contact-numbers,cms_b_google_translate,cms_b_company-info,cms_b_customer-support,cms_b_contact-us,cat_p_1380,cat_p,cat_p_1923,cat_p_2270,cat_p_2281,cat_p_2287,cat_p_1920,cms_b_gtm_header,
-   RespHeader     X-Content-Type-Options: nosniff
-   RespHeader     X-XSS-Protection: 1; mode=block
-   RespHeader     X-Frame-Options: SAMEORIGIN
-   RespHeader     Access-Control-Allow-Origin: *
-   RespHeader     Content-Encoding: gzip
-   RespHeader     X-Varnish: 397487
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish (Varnish/6.2)
-   VCL_call       DELIVER
-   RespUnset      Age: 0
-   RespUnset      Pragma: cache
-   RespHeader     Pragma: no-cache
-   RespUnset      Expires: Wed, 02 Jun 2021 17:10:31 GMT
-   RespHeader     Expires: -1
-   RespUnset      Cache-Control: max-age=1209600, public, s-maxage=1209600
-   RespHeader     Cache-Control: no-store, no-cache, must-revalidate, max-age=0
-   RespUnset      X-Magento-Tags: store,cms_b,ubmegamenu_group,ubmegamenu_item,cms_b_contact-numbers,cms_b_google_translate,cms_b_company-info,cms_b_customer-support,cms_b_contact-us,cat_p_1380,cat_p,cat_p_1923,cat_p_2270,cat_p_2281,cat_p_2287,cat_p_1920,cms_b_gtm_header,
-   RespUnset      Server: nginx/1.18.0 (Ubuntu)
-   RespUnset      X-Varnish: 397487
-   RespUnset      Via: 1.1 varnish (Varnish/6.2)
-   VCL_return     deliver
-   Timestamp      Process: 1621444234.406603 3.358753 0.000035
-   Filters        
-   RespHeader     Accept-Ranges: bytes
-   RespHeader     Content-Length: 35904
-   RespHeader     Connection: close
-   Timestamp      Resp: 1621444234.406701 3.358851 0.000099
-   ReqAcct        2339 0 2339 428 35904 36332
-   End            
**  << BeReq    >> 397488    
--  Begin          bereq 397487 fetch
--  VCL_use        boot
--  Timestamp      Start: 1621444231.047964 0.000000 0.000000
--  BereqMethod    GET
--  BereqURL       /exampleproduct.html
--  BereqProtocol  HTTP/1.0
--  BereqHeader    Host: example.com
--  BereqHeader    X-Forwarded-Host: example.com
--  BereqHeader    X-Real-IP: 172.69.142.57
--  BereqHeader    Ssl-Offloaded: 1
--  BereqHeader    X-Forwarded-Port: 443
--  BereqHeader    CF-IPCountry: US
--  BereqHeader    CF-RAY: 651ef06b6a105ffa-SMF
--  BereqHeader    CF-Visitor: {"scheme":"https"}
--  BereqHeader    sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"
--  BereqHeader    sec-ch-ua-mobile: ?0
--  BereqHeader    upgrade-insecure-requests: 1
--  BereqHeader    user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
--  BereqHeader    accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
--  BereqHeader    sec-fetch-site: same-origin
--  BereqHeader    sec-fetch-mode: navigate
--  BereqHeader    sec-fetch-user: ?1
--  BereqHeader    sec-fetch-dest: document
--  BereqHeader    referer: https://example.com/exampleproduct.html
--  BereqHeader    accept-language: en-US,en;q=0.9,es-ES;q=0.8,es;q=0.7
--  BereqHeader    cookie: _gid=GA1.2.1875408680.1621020942; mage-messages=; cf_clearance=bbfb28e3e23443eceb422f7ee7f35f9fe10d7f1b-1621358137-0-150; form_key=nKb2t0FfKDugritm; mage-cache-storage=%7B%7D; mage-cache-storage-section-invalidation=%7B%7D; PHPSESSID=nif7ckijs0ha
--  BereqHeader    CF-Connecting-IP: 2601:204:dd00:94c:20c:29ff:fef6:270b
--  BereqHeader    CDN-Loop: cloudflare
--  BereqHeader    cf-request-id: 0a2734971e00005ffa220b9000000001
--  BereqHeader    X-Forwarded-For: 2601:204:dd00:94c:20c:29ff:fef6:270b, 172.69.142.57, 172.26.12.6
--  BereqHeader    grace: none
--  BereqHeader    Accept-Encoding: gzip
--  BereqProtocol  HTTP/1.1
--  BereqHeader    X-Varnish: 397488
--  VCL_call       BACKEND_FETCH
--  VCL_return     fetch
--  BackendOpen    33 default 172.26.12.6 8080 172.26.14.47 39730
--  BackendStart   172.26.12.6 8080
--  Timestamp      Bereq: 1621444231.048055 0.000091 0.000091
--  Timestamp      Beresp: 1621444234.395396 3.347432 3.347342
--  BerespProtocol HTTP/1.1
--  BerespStatus   200
--  BerespReason   OK
--  BerespHeader   Server: nginx/1.18.0 (Ubuntu)
--  BerespHeader   Date: Wed, 19 May 2021 17:10:34 GMT
--  BerespHeader   Content-Type: text/html; charset=UTF-8
--  BerespHeader   Transfer-Encoding: chunked
--  BerespHeader   Connection: keep-alive
--  BerespHeader   Vary: Accept-Encoding
--  BerespHeader   Set-Cookie: PHPSESSID=nif7ckijs0hadsu747olm449bh; expires=Wed, 19-May-2021 19:10:31 GMT; Max-Age=7200; path=/; domain=example.com; secure; HttpOnly
--  BerespHeader   Set-Cookie: form_key=nKb2t0FfKDugritm; expires=Wed, 19-May-2021 19:10:31 GMT; Max-Age=7200; path=/; domain=example.com; secure
--  BerespHeader   Pragma: cache
--  BerespHeader   Cache-Control: max-age=1209600, public, s-maxage=1209600
--  BerespHeader   Expires: Wed, 02 Jun 2021 17:10:31 GMT
--  BerespHeader   X-Magento-Tags: store,cms_b,ubmegamenu_group,ubmegamenu_item,cms_b_contact-numbers,cms_b_google_translate,cms_b_company-info,cms_b_customer-support,cms_b_contact-us,cat_p_1380,cat_p,cat_p_1923,cat_p_2270,cat_p_2281,cat_p_2287,cat_p_1920,cms_b_gtm_header,
--  BerespHeader   X-Content-Type-Options: nosniff
--  BerespHeader   X-XSS-Protection: 1; mode=block
--  BerespHeader   X-Frame-Options: SAMEORIGIN
--  BerespHeader   Access-Control-Allow-Origin: *
--  BerespHeader   Content-Encoding: gzip
--  TTL            RFC 1209600 10 0 1621444234 1621444234 1621444234 1622653831 1209600 cacheable
--  VCL_call       BACKEND_RESPONSE
--  TTL            VCL 1209600 259200 0 1621444234 cacheable
--  BerespUnset    Set-Cookie: PHPSESSID=nif7ckijs0hadsu747olm449bh; expires=Wed, 19-May-2021 19:10:31 GMT; Max-Age=7200; path=/; domain=example.com; secure; HttpOnly
--  BerespUnset    Set-Cookie: form_key=nKb2t0FfKDugritm; expires=Wed, 19-May-2021 19:10:31 GMT; Max-Age=7200; path=/; domain=example.com; secure
--  VCL_return     deliver
--  Filters        gunzip esi_gzip
--  BerespUnset    Content-Encoding: gzip
--  BerespHeader   Content-Encoding: gzip
--  Storage        malloc s0
--  Fetch_Body     2 chunked -
--  Gzip           G F E 477855 35904 80 287152 287162
--  Gzip           U F - 35893 477855 80 216112 287074
--  BackendReuse   33 default
--  Timestamp      BerespBody: 1621444234.406546 3.358582 0.011149
--  Length         35904
--  BereqAcct      2339 0 2339 1052 0 1052
--  End            

这两个页面都在同一产品页面上。首页的加载时间其实没什么大问题,产品页面才是最难处理的。缓存本可以提前过期,我只是在刷新之间等待了 10 分钟。缓存时,服务器响应时间为 115 毫秒,未缓存时为 3.5 秒。

更新2:

以下是清除请求的 varnishlog:

*   << Request  >> 1451815   
-   Begin          req 1451814 rxreq
-   Timestamp      Start: 1621546852.088978 0.000000 0.000000
-   Timestamp      Req: 1621546852.088978 0.000000 0.000000
-   VCL_use        boot
-   ReqStart       172.26.12.6 44816 a0
-   ReqMethod      PURGE
-   ReqURL         /
-   ReqProtocol    HTTP/1.1
-   ReqHeader      X-Magento-Tags-Pattern: ((^|,)amasty_checkbox(,|$))
-   ReqHeader      Host: 172.26.14.47
-   ReqHeader      X-Forwarded-For: 172.26.12.6
-   VCL_call       RECV
-   VCL_acl        MATCH purge "172.26.12.6"
-   VCL_return     synth
-   VCL_call       HASH
-   VCL_return     lookup
-   Timestamp      Process: 1621546852.089046 0.000068 0.000068
-   RespHeader     Date: Thu, 20 May 2021 21:40:52 GMT
-   RespHeader     Server: Varnish
-   RespHeader     X-Varnish: 1451815
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespReason     Purged
-   VCL_call       SYNTH
-   RespHeader     Content-Type: text/html; charset=utf-8
-   RespHeader     Retry-After: 5
-   VCL_return     deliver
-   RespHeader     Content-Length: 242
-   Storage        malloc Transient
-   Filters        
-   RespHeader     Accept-Ranges: bytes
-   RespHeader     Connection: keep-alive
-   Timestamp      Resp: 1621546852.089096 0.000118 0.000050
-   ReqAcct        93 0 93 220 242 462
-   End 

回答:

在 Danila Vershinin 的帮助下,我发现 Amasty 的一个插件每次有人通过其扩展接受 TOS 时都会发出清除命令。我不知道他们为什么这样做,但我更新了我的 default.vcl,如下所示:

原来的:

if (req.http.X-Magento-Tags-Pattern) {
          ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
        }

更新:

if (req.http.X-Magento-Tags-Pattern && req.http.X-Magento-Tags-Pattern !~ "amasty_checkbox") {
          ban("obj.http.X-Magento-Tags ~ " + req.http.X-Magento-Tags-Pattern);
        }

我基本上告诉 varnish 忽略来自“amasty_checkbox”的清除请求。Varnish 现在按预期保存页面缓存,并且截至撰写本文时已积极提供缓存内容 16 小时。感谢大家帮助我解决这个问题。

答案1

如果您使用的是 Magento 的默认 VCL,并在 Magento 管理中将 TTL 更改为最高,那么页面提前过期的唯一合理解释是:

  1. 分配给 Varnish 的存储空间不足。也就是说,由于缓存存储空间不足,新缓存的页面将驱逐另一个页面的缓存。要检查是否发生了这种情况,请运行varnishstat并查找MAIN.n_lru_nuked。如果它存在并且具有非零的正值,则意味着您为 Varnish 设置的缓存存储大小很可能不足以容纳缓存中的所有页面。

  2. 某物清除缓存。这可能是由开发人员、有错误的第三方模块、自定义代码等设置的用于清除缓存的自定义 cron。例如,SwissUpLabs SEO 模块在某个时候做到了

要排除故障并找到“清除缓存的东西”,您可能不得不求助于……毫不奇怪,第三方插件。Magento 2 的全页面缓存预热器在其功能中,有一个很棒的功能,您可以在这里查看 Magento 代码(包括插件)清除缓存的日志。通过调查它,您可以发现是否有错误的模块不必要地清除缓存。

但是在使用该模块之前,您可以运行以下操作一段时间,例如 10 分钟:

varnishlog -g request -q 'ReqMethod eq "PURGE" or ReqMethod eq "BAN"'

如果运行此命令时出现任何内容,您可能需要继续使用 warmer 插件来调查究竟是什么发出了导致清除缓存的那些请求。

答案2

我想查看一些日志并尝试确定为什么内容只缓存这么短的时间。

请在缓存为空时运行以下命令:

varnishlog -g request -q "ReqUrl eq '/'"

我假设主页正在经历这种行为,所以我过滤了 URL 的日志/

日志输出将解释发生了什么以及特定响应在缓存中存储了多长时间。

从 VCL 文件中删除一些代码

但是,我已经发现你的 VCL 中存在一些奇怪的行为。例如以下代码片段:

    if (beresp.status != 200 && beresp.status != 404) {
        set beresp.ttl = 0s;
        set beresp.uncacheable = true;
        return (deliver);
    } elsif (beresp.http.Cache-Control ~ "private") {
        set beresp.uncacheable = true;
        set beresp.ttl = 604800s;
        return (deliver);
    }

请从您的 VCL 中删除此代码块,因为它对您没有任何好处。

设置beresp.ttl = 0s;是绝对不行的。通过将 TTL te 设置为零,你基本上就剥夺了利用所谓的请求合并功能。此功能可确保对同一未缓存资源的多个同时请求由单个后端响应满足。

通过将 TTL 设置为零,会导致序列化。这意味着每个针对未缓存资源的排队请求都被放入等待列表中并按顺序处理。

此外,标记私人的内容为命中或未命中或者击球传球为了604800 秒也没什么用。

标准逻辑在最后if 语句vcl_backend_response子程序中这样就可以了。

相关内容