我们使用Symfony(Sulu)和Varish作为反向代理,使用基于标签的无效(Xkey),其工作方式与预期一致.现在,我们希望在我们的网站上对动态块使用ESI,与"主"缓存生存期分开.

但每次完全缓存网页时,即使在硬刷新/清除浏览器缓存或使用新浏览器之后,页面也总是显示第一个缓存条目.

使用带有PHP(端口:9000)、Nginx(端口:8000)和Varish(端口:8081)的Docker设置

我们已在Symfony中启用了ESI:

framework:
...
    esi: true
    fragments: { path: /_fragment }

并在我们的Twig模板中提供render_esi:

navbar.html.twig

...
            {{ render_esi(controller('App\\Controller\\Website\\LoginButtonController::renderEsiLoginButton')) }}
            <div class="navbar-main__toggler"></div>
        </div>

LoginButtonController

class LoginButtonController extends AbstractController
{
    public function renderEsiLoginButton(): Response
    {
        $response = $this->render('esi/_login_button.html.twig', ['random_nr' => rand()]);

        $response->setMaxAge(10);
        $response->setSharedMaxAge(10);
        $response->setPublic();

        return $response;
    }
}

_login_button.html.twig

<div class="navbar-main__action-account">
    <div class="account__dropdown">
        <a href="#" class="account__user">{{ random_nr }}</a>
        ...
    </div>
</div>

我们的Varnish default.vcl:

# /etc/varnish/default.vcl
vcl 4.0;

include "/etc/varnish/sulu.vcl";
include "/etc/varnish/fos_user_context.vcl";
include "/etc/varnish/fos_user_context_url.vcl";
include "/etc/varnish/fos_tags_xkey.vcl";

acl invalidators {
    "localhost";
    "$VARNISH_INVALIDATOR_HOST";
}

backend default {
    .host = "$VARNISH_BACKEND_HOST";
    .port = "$VARNISH_BACKEND_PORT";
}

sub vcl_recv {
    call fos_tags_xkey_recv;
    call fos_user_context_recv;
    call sulu_recv;

    // # Add a Surrogate-Capability header to announce ESI
    set req.http.Surrogate-Capability = "abc=ESI/1.0";

    # Remove all cookies except the session ID (SULUSESSID)
    # Check configured session ID name in your config/packages/framework.yaml
    if (req.http.Cookie) {
        set req.http.Cookie = ";" + req.http.Cookie;
        set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
        set req.http.Cookie = regsuball(req.http.Cookie, ";(SULUSESSID)=", "; \1=");
        set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
        set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");

        if (req.http.Cookie == "") {
            # If there are no more cookies, remove the header to get page cached.
            unset req.http.Cookie;
        }
    }

    # No cache besides GET and HEAD
    if (req.method != "GET" && req.method != "HEAD") {
      return (pass);
    }

    # if Authorization or no-cache header we skip the cache
    if (req.http.Authorization || req.http.Cache-Control ~ "no-cache") {
        return (pass);
    }

    # Force the lookup, the backend must tell not to cache or vary on all
    # headers that are used to build the hash.
    return (hash);
}

sub vcl_hash {
    call fos_user_context_hash;
}

sub vcl_backend_response {
    set beresp.grace = 2m;

    call fos_user_context_backend_response;
    call sulu_backend_response;

    // Check for ESI acknowledgement and remove Surrogate-Control header
    if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
        unset beresp.http.Surrogate-Control;
        set beresp.do_esi = true;
    }
}

sub vcl_deliver {
    call fos_tags_xkey_deliver;
    call fos_user_context_deliver;
    call sulu_deliver;

    # Include debugging info
    set resp.http.Obj-Hits = obj.hits;
    set resp.http.Seen-Cookie = req.http.Cookie;

    # set resp.http.Vary = "X-User-Context-Hash";
}

我们的fos_http_cache配置:

fos_http_cache:
    tags:
        enabled: true
        response_header: xkey
        max_header_value_length: 1024
    proxy_client:
        symfony:
            use_kernel_dispatcher: true

    user_context:
        enabled: true
        role_provider: true
#        user_hash_header: 'X-User-Context-Hash'
        hash_cache_ttl: 900

和Sulu http缓存:

when@prod: &prod
    sulu_http_cache:
        debug:
            enabled: false
        tags:
            enabled: true
        cache:
            max_age: 240
            shared_max_age: 480
        proxy_client:
            symfony:
                enabled: false
            varnish:
                enabled: true
                servers: [ '%env(VARNISH_SERVER)%' ]
                tag_mode: purgekeys

在使用cURL时,我们还可以正确地看到esi:include标记

curl -i -X GET 'http://localhost:8000/en' -H 'Surrogate-Capability: ESI/1.0:

<span class="navbar-main__action-search-button"></span>

<esi:include src="/_fragment?_hash=HAz3Ym5wXab4GXTRKnq8oUR3WwnGqUpprL0niBc%2BEwg%3D&_path=_format%3Dhtml%26_locale%3Den%26_controller%3DApp%255CController%255CWebsite%255CLoginButtonController%253A%253ArenderEsiLoginButton" onerror="continue" />

<div class="navbar-main__toggler"></div>

但网页仍被完全缓存,即使在硬刷新/清除浏览器缓存或使用新浏览器后,页面始终显示第一个随机数.直到我们完全清除瓦尼什的藏身之处.

我们遗漏了什么?


更新1:按要求输出varnishlog:

$ varnishlog -g request -q "ReqUrl eq '/en'"
*   << Request  >> 65566
-   Begin          req 65565 rxreq
-   Timestamp      Start: 1700910701.474503 0.000000 0.000000
-   Timestamp      Req: 1700910701.474503 0.000000 0.000000
-   VCL_use        boot
-   ReqStart       192.168.207.1 60914 a0
-   ReqMethod      GET
-   ReqURL         /en
-   ReqProtocol    HTTP/1.1
-   ReqHeader      Host: localhost:8081
-   ReqHeader      Connection: keep-alive
-   ReqHeader      sec-ch-ua: "Not A(Brand";v="99", "Google Chrome";v="121", "Chromium";v="121"
-   ReqHeader      sec-ch-ua-mobile: ?0
-   ReqHeader      sec-ch-ua-platform: "macOS"
-   ReqHeader      Upgrade-Insecure-Requests: 1
-   ReqHeader      User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 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.7
-   ReqHeader      Sec-Fetch-Site: none
-   ReqHeader      Sec-Fetch-Mode: navigate
-   ReqHeader      Sec-Fetch-User: ?1
-   ReqHeader      Sec-Fetch-Dest: document
-   ReqHeader      Accept-Encoding: gzip, deflate, br, zstd
-   ReqHeader      Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
-   ReqHeader      X-Forwarded-For: 192.168.207.1
-   VCL_call       RECV
-   ReqHeader      X-Fos-Original-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.7
-   ReqUnset       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.7
-   ReqHeader      accept: application/vnd.fos.user-context-hash
-   ReqHeader      X-Fos-Original-Url: /en
-   ReqURL         /_fos_user_context_hash
-   VCL_return     hash
-   ReqUnset       Accept-Encoding: gzip, deflate, br, zstd
-   ReqHeader      Accept-Encoding: gzip
-   VCL_call       HASH
-   VCL_return     lookup
-   ExpBan         393224 banned lookup
-   VCL_call       MISS
-   VCL_return     fetch
-   Link           bereq 65567 fetch
-   Timestamp      Fetch: 1700910703.172124 1.697620 1.697620
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Server: nginx
-   RespHeader     Content-Type: application/vnd.fos.user-context-hash
-   RespHeader     X-Powered-By: PHP/8.2.5
-   RespHeader     X-User-Context-Hash: 5224d8f5b85429624e2160e538a3376a479ec87b89251b295c44ecbf7498ea3c
-   RespHeader     Cache-Control: max-age=900, public
-   RespHeader     Date: Sat, 25 Nov 2023 11:11:43 GMT
-   RespHeader     Vary: cookie, authorization
-   RespHeader     xkey: fos_http_cache_hashlookup- webspace-amfori
-   RespHeader     X-Generator: Sulu/2.5.10
-   RespHeader     x-url: /_fos_user_context_hash
-   RespHeader     x-host: localhost:8081
-   RespHeader     X-Varnish: 65566
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish (Varnish/6.5)
-   VCL_call       DELIVER
-   RespUnset      xkey: fos_http_cache_hashlookup- webspace-amfori
-   ReqHeader      X-User-Context-Hash: 5224d8f5b85429624e2160e538a3376a479ec87b89251b295c44ecbf7498ea3c
-   VCL_return     restart
-   Timestamp      Process: 1700910703.172133 1.697630 0.000009
-   Timestamp      Restart: 1700910703.172134 1.697631 0.000000
-   Link           req 65568 restart
-   End
...

更新2:修改后的VCL将Surrogate-Capability人移至榜首:

sub vcl_recv {
    // # Add a Surrogate-Capability header to announce ESI
    set req.http.Surrogate-Capability = "abc=ESI/1.0";

    call fos_tags_xkey_recv;
    call fos_user_context_recv;
    call sulu_recv;
...

在Varish varnishlog中执行ESI_Fragment调用的结果:

$ varnishlog -g request -q "ReqUrl eq '/en'"
*   << Request  >> 295260
-   Begin          req 295259 rxreq
-   Timestamp      Start: 1701104453.954200 0.000000 0.000000
-   Timestamp      Req: 1701104453.954200 0.000000 0.000000
-   VCL_use        boot
-   ReqStart       192.168.207.1 45193 a0
-   ReqMethod      GET
-   ReqURL         /en
-   ReqProtocol    HTTP/1.1
-   ReqHeader      Host: localhost:8081
-   ReqHeader      User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0
-   ReqHeader      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
-   ReqHeader      Accept-Language: en-US,en;q=0.5
-   ReqHeader      Accept-Encoding: gzip, deflate, br
-   ReqHeader      Connection: keep-alive
-   ReqHeader      Cookie: trusted_device=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MDA4MzEwNzYuMjIxNzQ1LCJleHAiOjE3MDYwMTUwNzYuMjE5NDQ2LCJ1c3IiOiJzYW5kZXIuaG9mbWFuQGljYXBwcy5jb20iLCJmd2wiOiJhZG1pbiIsInZzbiI6MH0.haaj4WNSp4inor3rWD1FlUm52qXwmWEJ3mkmgTEtX4Q; userTimez
-   ReqHeader      Upgrade-Insecure-Requests: 1
-   ReqHeader      Sec-Fetch-Dest: document
-   ReqHeader      Sec-Fetch-Mode: navigate
-   ReqHeader      Sec-Fetch-Site: none
-   ReqHeader      Sec-Fetch-User: ?1
-   ReqHeader      Pragma: no-cache
-   ReqHeader      Cache-Control: no-cache
-   ReqHeader      X-Forwarded-For: 192.168.207.1
-   VCL_call       RECV
-   ReqHeader      Surrogate-Capability: abc=ESI/1.0
-   ReqHeader      X-Fos-Original-Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
-   ReqUnset       Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
-   ReqHeader      accept: application/vnd.fos.user-context-hash
-   ReqHeader      X-Fos-Original-Url: /en
-   ReqURL         /_fos_user_context_hash
-   VCL_return     hash
-   ReqUnset       Accept-Encoding: gzip, deflate, br
-   ReqHeader      Accept-Encoding: gzip
-   VCL_call       HASH
-   VCL_return     lookup
-   ExpBan         32789 banned lookup
-   VCL_call       MISS
-   VCL_return     fetch
-   Link           bereq 295261 fetch
-   Timestamp      Fetch: 1701104455.867260 1.913060 1.913060
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Server: nginx
-   RespHeader     Content-Type: application/vnd.fos.user-context-hash
-   RespHeader     X-Powered-By: PHP/8.2.5
-   RespHeader     X-User-Context-Hash: 5224d8f5b85429624e2160e538a3376a479ec87b89251b295c44ecbf7498ea3c
-   RespHeader     Cache-Control: max-age=900, public
-   RespHeader     Date: Mon, 27 Nov 2023 17:00:55 GMT
-   RespHeader     Vary: cookie, authorization
-   RespHeader     xkey: fos_http_cache_hashlookup- webspace-amfori
-   RespHeader     X-Generator: Sulu/2.5.10
-   RespHeader     x-url: /_fos_user_context_hash
-   RespHeader     x-host: localhost:8081
-   RespHeader     X-Varnish: 295260
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish (Varnish/6.5)
-   VCL_call       DELIVER
-   RespUnset      xkey: fos_http_cache_hashlookup- webspace-amfori
-   ReqHeader      X-User-Context-Hash: 5224d8f5b85429624e2160e538a3376a479ec87b89251b295c44ecbf7498ea3c
-   VCL_return     restart
-   Timestamp      Process: 1701104455.867277 1.913076 0.000016
-   Timestamp      Restart: 1701104455.867278 1.913077 0.000000
-   Link           req 295262 restart
-   End
**  << BeReq    >> 295261
--  Begin          bereq 295260 fetch
--  VCL_use        boot
--  Timestamp      Start: 1701104453.954671 0.000000 0.000000
--  BereqMethod    GET
--  BereqURL       /_fos_user_context_hash
--  BereqProtocol  HTTP/1.1
--  BereqHeader    Host: localhost:8081
--  BereqHeader    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0
--  BereqHeader    Accept-Language: en-US,en;q=0.5
--  BereqHeader    Cookie: trusted_device=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MDA4MzEwNzYuMjIxNzQ1LCJleHAiOjE3MDYwMTUwNzYuMjE5NDQ2LCJ1c3IiOiJzYW5kZXIuaG9mbWFuQGljYXBwcy5jb20iLCJmd2wiOiJhZG1pbiIsInZzbiI6MH0.haaj4WNSp4inor3rWD1FlUm52qXwmWEJ3mkmgTEtX4Q; userTimez
--  BereqHeader    Upgrade-Insecure-Requests: 1
--  BereqHeader    Sec-Fetch-Dest: document
--  BereqHeader    Sec-Fetch-Mode: navigate
--  BereqHeader    Sec-Fetch-Site: none
--  BereqHeader    Sec-Fetch-User: ?1
--  BereqHeader    Pragma: no-cache
--  BereqHeader    X-Forwarded-For: 192.168.207.1
--  BereqHeader    Surrogate-Capability: abc=ESI/1.0
--  BereqHeader    X-Fos-Original-Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
--  BereqHeader    accept: application/vnd.fos.user-context-hash
--  BereqHeader    X-Fos-Original-Url: /en
--  BereqHeader    Accept-Encoding: gzip
--  BereqHeader    X-Varnish: 295261
--  VCL_call       BACKEND_FETCH
--  VCL_return     fetch
--  BackendOpen    25 default 192.168.207.8 8000 192.168.207.10 37256 connect
--  Timestamp      Bereq: 1701104453.955471 0.000800 0.000800
--  Timestamp      Beresp: 1701104455.866936 1.912265 1.911465
--  BerespProtocol HTTP/1.1
--  BerespStatus   200
--  BerespReason   OK
--  BerespHeader   Server: nginx
--  BerespHeader   Content-Type: application/vnd.fos.user-context-hash
--  BerespHeader   Transfer-Encoding: chunked
--  BerespHeader   Connection: keep-alive
--  BerespHeader   X-Powered-By: PHP/8.2.5
--  BerespHeader   X-User-Context-Hash: 5224d8f5b85429624e2160e538a3376a479ec87b89251b295c44ecbf7498ea3c
--  BerespHeader   Cache-Control: max-age=900, public
--  BerespHeader   Date: Mon, 27 Nov 2023 17:00:55 GMT
--  BerespHeader   Vary: cookie
--  BerespHeader   Vary: authorization
--  BerespHeader   xkey: fos_http_cache_hashlookup- webspace-amfori
--  BerespHeader   X-Generator: Sulu/2.5.10
--  TTL            RFC 900 10 0 1701104456 1701104456 1701104455 0 900 cacheable
--  VCL_call       BACKEND_RESPONSE
--  TTL            VCL 900 120 0 1701104456 cacheable
--  BerespHeader   x-url: /_fos_user_context_hash
--  BerespHeader   x-host: localhost:8081
--  VCL_return     deliver
--  Filters         esi
--  Storage        malloc s0
--  Fetch_Body     2 chunked -
--  BackendClose   25 default recycle
--  Timestamp      BerespBody: 1701104455.867224 1.912553 0.000287
--  Length         0
--  BereqAcct      901 0 901 435 0 435
--  End
*   << Request  >> 295262
-   Begin          req 295260 restart
-   Timestamp      Start: 1701104455.867278 1.913077 0.000000
-   ReqStart       192.168.207.1 45193 a0
-   ReqMethod      GET
-   ReqURL         /_fos_user_context_hash
-   ReqProtocol    HTTP/1.1
-   ReqHeader      Host: localhost:8081
-   ReqHeader      User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0
-   ReqHeader      Accept-Language: en-US,en;q=0.5
-   ReqHeader      Connection: keep-alive
-   ReqHeader      Cookie: trusted_device=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MDA4MzEwNzYuMjIxNzQ1LCJleHAiOjE3MDYwMTUwNzYuMjE5NDQ2LCJ1c3IiOiJzYW5kZXIuaG9mbWFuQGljYXBwcy5jb20iLCJmd2wiOiJhZG1pbiIsInZzbiI6MH0.haaj4WNSp4inor3rWD1FlUm52qXwmWEJ3mkmgTEtX4Q; userTimez
-   ReqHeader      Upgrade-Insecure-Requests: 1
-   ReqHeader      Sec-Fetch-Dest: document
-   ReqHeader      Sec-Fetch-Mode: navigate
-   ReqHeader      Sec-Fetch-Site: none
-   ReqHeader      Sec-Fetch-User: ?1
-   ReqHeader      Pragma: no-cache
-   ReqHeader      Cache-Control: no-cache
-   ReqHeader      X-Forwarded-For: 192.168.207.1
-   ReqHeader      Surrogate-Capability: abc=ESI/1.0
-   ReqHeader      X-Fos-Original-Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
-   ReqHeader      accept: application/vnd.fos.user-context-hash
-   ReqHeader      X-Fos-Original-Url: /en
-   ReqHeader      Accept-Encoding: gzip
-   ReqHeader      X-User-Context-Hash: 5224d8f5b85429624e2160e538a3376a479ec87b89251b295c44ecbf7498ea3c
-   VCL_call       RECV
-   ReqUnset       Surrogate-Capability: abc=ESI/1.0
-   ReqHeader      Surrogate-Capability: abc=ESI/1.0
-   ReqURL         /en
-   ReqUnset       X-Fos-Original-Url: /en
-   ReqUnset       accept: application/vnd.fos.user-context-hash
-   ReqHeader      accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
-   ReqUnset       X-Fos-Original-Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
-   VCL_return     hash
-   VCL_call       HASH
-   VCL_return     lookup
-   ExpBan         32783 banned lookup
-   VCL_call       MISS
-   VCL_return     fetch
-   Link           bereq 295263 fetch
-   Timestamp      Fetch: 1701104456.583858 2.629658 0.716580
-   RespProtocol   HTTP/1.1
-   RespStatus     200
-   RespReason     OK
-   RespHeader     Server: nginx
-   RespHeader     Content-Type: text/html; charset=UTF-8
-   RespHeader     X-Powered-By: PHP/8.2.5
-   RespHeader     Cache-Control: max-age=240, public, s-maxage=480
-   RespHeader     Date: Mon, 27 Nov 2023 17:00:56 GMT
-   RespHeader     Link: </build/website/runtime.67e3af91.js>; rel="preload"; as="script",</build/website/main.c77e8851.js>; rel="preload"; as="script",</build/website/app.56c210d4.css>; rel="preload"; as="style"
-   RespHeader     xkey: account-1 media-9750 media-9268 media-9269 media-9270 media-9586 media-9585 media-9276 media-9582 media-9583 media-9274 media-9610 media-9658 category-2 category-4 category-5 category-6 category-7 a280a8cf-ef54-41af-a576-e2ee364cde7c f76cf5c1-0dd5-
-   RespHeader     X-Generator: Sulu/2.5.10
-   RespHeader     x-url: /en
-   RespHeader     x-host: localhost:8081
-   RespHeader     Content-Encoding: gzip
-   RespHeader     Vary: X-User-Context-Hash, Accept-Encoding
-   RespHeader     X-Varnish: 295262
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish (Varnish/6.5)
-   VCL_call       DELIVER
-   RespUnset      xkey: account-1 media-9750 media-9268 media-9269 media-9270 media-9586 media-9585 media-9276 media-9582 media-9583 media-9274 media-9610 media-9658 category-2 category-4 category-5 category-6 category-7 a280a8cf-ef54-41af-a576-e2ee364cde7c f76cf5c1-0dd5-
-   RespUnset      Vary: X-User-Context-Hash, Accept-Encoding
-   RespHeader     Vary: , Accept-Encoding
-   RespUnset      Vary: , Accept-Encoding
-   RespHeader     Vary: Accept-Encoding
-   RespUnset      x-url: /en
-   RespUnset      x-host: localhost:8081
-   RespHeader     Obj-Hits: 0
-   RespHeader     Seen-Cookie: trusted_device=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MDA4MzEwNzYuMjIxNzQ1LCJleHAiOjE3MDYwMTUwNzYuMjE5NDQ2LCJ1c3IiOiJzYW5kZXIuaG9mbWFuQGljYXBwcy5jb20iLCJmd2wiOiJhZG1pbiIsInZzbiI6MH0.haaj4WNSp4inor3rWD1FlUm52qXwmWEJ3mkmgTEtX4Q; user
-   VCL_return     deliver
-   Timestamp      Process: 1701104456.583879 2.629679 0.000020
-   Filters         esi
-   RespHeader     Accept-Ranges: bytes
-   RespHeader     Connection: keep-alive
-   RespHeader     Transfer-Encoding: chunked
-   Link           req 295264 esi
-   Timestamp      Resp: 1701104456.805554 2.851354 0.221675
-   ReqAcct        770 0 770 880 3654 4534
-   End
**  << BeReq    >> 295263
--  Begin          bereq 295262 fetch
--  VCL_use        boot
--  Timestamp      Start: 1701104455.867403 0.000000 0.000000
--  BereqMethod    GET
--  BereqURL       /en
--  BereqProtocol  HTTP/1.1
--  BereqHeader    Host: localhost:8081
--  BereqHeader    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0
--  BereqHeader    Accept-Language: en-US,en;q=0.5
--  BereqHeader    Cookie: trusted_device=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MDA4MzEwNzYuMjIxNzQ1LCJleHAiOjE3MDYwMTUwNzYuMjE5NDQ2LCJ1c3IiOiJzYW5kZXIuaG9mbWFuQGljYXBwcy5jb20iLCJmd2wiOiJhZG1pbiIsInZzbiI6MH0.haaj4WNSp4inor3rWD1FlUm52qXwmWEJ3mkmgTEtX4Q; userTimez
--  BereqHeader    Upgrade-Insecure-Requests: 1
--  BereqHeader    Sec-Fetch-Dest: document
--  BereqHeader    Sec-Fetch-Mode: navigate
--  BereqHeader    Sec-Fetch-Site: none
--  BereqHeader    Sec-Fetch-User: ?1
--  BereqHeader    Pragma: no-cache
--  BereqHeader    X-Forwarded-For: 192.168.207.1
--  BereqHeader    Accept-Encoding: gzip
--  BereqHeader    X-User-Context-Hash: 5224d8f5b85429624e2160e538a3376a479ec87b89251b295c44ecbf7498ea3c
--  BereqHeader    Surrogate-Capability: abc=ESI/1.0
--  BereqHeader    accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
--  BereqHeader    X-Varnish: 295263
--  VCL_call       BACKEND_FETCH
--  VCL_return     fetch
--  BackendOpen    25 default 192.168.207.8 8000 192.168.207.10 37256 reuse
--  Timestamp      Bereq: 1701104455.867506 0.000103 0.000103
--  Timestamp      Beresp: 1701104456.582952 0.715549 0.715446
--  BerespProtocol HTTP/1.1
--  BerespStatus   200
--  BerespReason   OK
--  BerespHeader   Server: nginx
--  BerespHeader   Content-Type: text/html; charset=UTF-8
--  BerespHeader   Transfer-Encoding: chunked
--  BerespHeader   Connection: keep-alive
--  BerespHeader   X-Powered-By: PHP/8.2.5
--  BerespHeader   Cache-Control: max-age=240, public, s-maxage=480
--  BerespHeader   Date: Mon, 27 Nov 2023 17:00:56 GMT
--  BerespHeader   X-Reverse-Proxy-TTL: 86400
--  BerespHeader   Surrogate-Control: content="ESI/1.0"
--  BerespHeader   Link: </build/website/runtime.67e3af91.js>; rel="preload"; as="script",</build/website/main.c77e8851.js>; rel="preload"; as="script",</build/website/app.56c210d4.css>; rel="preload"; as="style"
--  BerespHeader   xkey: account-1 media-9750 media-9268 media-9269 media-9270 media-9586 media-9585 media-9276 media-9582 media-9583 media-9274 media-9610 media-9658 category-2 category-4 category-5 category-6 category-7 a280a8cf-ef54-41af-a576-e2ee364cde7c f76cf5c1-0dd5-
--  BerespHeader   Vary: X-User-Context-Hash
--  BerespHeader   X-Generator: Sulu/2.5.10
--  BerespHeader   Content-Encoding: gzip
--  TTL            RFC 480 10 0 1701104457 1701104457 1701104456 0 480 cacheable
--  VCL_call       BACKEND_RESPONSE
--  TTL            VCL 480 120 0 1701104457 cacheable
--  BerespHeader   x-url: /en
--  BerespHeader   x-host: localhost:8081
--  TTL            VCL 86400 120 0 1701104457 cacheable
--  BerespUnset    X-Reverse-Proxy-TTL: 86400
--  BerespUnset    Surrogate-Control: content="ESI/1.0"
--  VCL_return     deliver
--  Filters         gunzip esi_gzip
--  BerespUnset    Content-Encoding: gzip
--  BerespHeader   Content-Encoding: gzip
--  BerespUnset    Vary: X-User-Context-Hash
--  BerespHeader   Vary: X-User-Context-Hash, Accept-Encoding
--  Storage        malloc s0
--  Fetch_Body     2 chunked -
--  Gzip           G F E 19864 3649 80 29112 29122
--  Gzip           U F - 3997 19864 80 80 31905
--  BackendClose   25 default recycle
--  Timestamp      BerespBody: 1701104456.583813 0.716410 0.000860
--  Length         3649
--  BereqAcct      881 0 881 1087 0 1087
--  End
**  << Request  >> 295264
--  Begin          req 295262 esi
--  Timestamp      Start: 1701104456.583969 0.000000 0.000000
--  ReqURL         /_fragment?_hash=8bu9984nZ8dzK0ti7IeJWjYw%2BVIgXHNOzgK9K%2FxsZak%3D&_path=absolute_uri%3D0%26_format%3Dhtml%26_locale%3Den%26_controller%3DApp%255CController%255CWebsite%255CLoginButtonController%253A%253ArenderEsiLoginButton
--  ReqUnset       Accept-Encoding: gzip, deflate, br
--  ReqHeader      Accept-Encoding: gzip
--  ReqStart       192.168.207.1 45193 a0
--  ReqMethod      GET
--  ReqURL         /_fragment?_hash=8bu9984nZ8dzK0ti7IeJWjYw%2BVIgXHNOzgK9K%2FxsZak%3D&_path=absolute_uri%3D0%26_format%3Dhtml%26_locale%3Den%26_controller%3DApp%255CController%255CWebsite%255CLoginButtonController%253A%253ArenderEsiLoginButton
--  ReqProtocol    HTTP/1.1
--  ReqHeader      Host: localhost:8081
--  ReqHeader      User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0
--  ReqHeader      Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
--  ReqHeader      Accept-Language: en-US,en;q=0.5
--  ReqHeader      Connection: keep-alive
--  ReqHeader      Cookie: trusted_device=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MDA4MzEwNzYuMjIxNzQ1LCJleHAiOjE3MDYwMTUwNzYuMjE5NDQ2LCJ1c3IiOiJzYW5kZXIuaG9mbWFuQGljYXBwcy5jb20iLCJmd2wiOiJhZG1pbiIsInZzbiI6MH0.haaj4WNSp4inor3rWD1FlUm52qXwmWEJ3mkmgTEtX4Q; userTimez
--  ReqHeader      Upgrade-Insecure-Requests: 1
--  ReqHeader      Sec-Fetch-Dest: document
--  ReqHeader      Sec-Fetch-Mode: navigate
--  ReqHeader      Sec-Fetch-Site: none
--  ReqHeader      Sec-Fetch-User: ?1
--  ReqHeader      Pragma: no-cache
--  ReqHeader      Cache-Control: no-cache
--  ReqHeader      Accept-Encoding: gzip
--  ReqHeader      X-Forwarded-For: 192.168.207.1
--  VCL_call       RECV
--  ReqHeader      Surrogate-Capability: abc=ESI/1.0
--  ReqHeader      X-Fos-Original-Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
--  ReqUnset       Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
--  ReqHeader      accept: application/vnd.fos.user-context-hash
--  ReqHeader      X-Fos-Original-Url: /_fragment?_hash=8bu9984nZ8dzK0ti7IeJWjYw%2BVIgXHNOzgK9K%2FxsZak%3D&_path=absolute_uri%3D0%26_format%3Dhtml%26_locale%3Den%26_controller%3DApp%255CController%255CWebsite%255CLoginButtonController%253A%253ArenderEsiLoginButton
--  ReqURL         /_fos_user_context_hash
...

推荐答案

从表面上看,你做的一切都是对的.但是,一些有针对性的varnishlog个命令将让我们更好地理解Varish是如何缓存各种片段的.

我需要您将以下命令的输出发送给我:

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

我的假设是这个问题发生在主页上.如果不是这样,请更改varnishlog命令中的筛选器.

It's important that you run this command on an empty cache!

当Varish执行各种获取操作时,我们可以查看日志(log)中的TTL个标记.这将告诉我们Varish如何决定每个片段的TTL(通过在VCL代码中设置的TTL,或者通过Cache-Control头).

日志(log)还将各种ESI子请求作为单独的日志(log)事务返回,从而更容易理解在什么情况下发生了什么.

请把日志(log)输出放在你的原始问题中,我会帮你弄清楚发生了什么.

更新

我没有在您的(后端)请求中看到Surrogate-Capability头.如果没有该标头,symfony将不会触发Surrogate-Control标头,从而启用Varish中的ESI处理.尽管在您的VCL代码中看到了Surrogate-Capability.

然而,就在您设置标题之前,我在VCL代码中看到了call fos_user_context_backend_response;call sulu_backend_response;call sulu_recv;.很可能会在那里执行一条返回语句,绕过您的头.

我的建议是将set req.http.Surrogate-Capability = "abc=ESI/1.0";移到vcl_recv子 routine 的开头.

只需确保设置了头并且逻辑不会被其他调用绕过即可.

解决此问题的一种简单方法是将此逻辑移到vcl_backend_fetch子 routine ,因为设置标头与获取相关,而不是与接收请求相关.以下是VCL代码:

sub vcl_backend_fetch {
    set bereq.http.Surrogate-Capability = "abc=ESI/1.0";
}

Php相关问答推荐

p a/a/p的x路径 Select ,但不是p somethext a链接/a或某些文本/p>

Woocommerce自定义变体html表仅显示带值的列

FatFreeFramework上的不同路由

Symfony/Panther Web抓取不适用于登录后的内容(云功能)

WooCommerce购物车中仅允许一个可下载产品

在特定订单状态更改时自定义WooCommerce订单发货项目

使用PHP curl的Amazon SP API批处理调用

针对给定产品类别的WooCommerce计数审核

未收到Strava WebHook事件数据

Laravel 10中的Validator类

将一个表中的行连接为不同表MYSQL中的一列

Symfony:从控制器内部调用自定义命令

将图像作为 PHP post 响应发送到浏览器

Woocommerce单个添加到购物车页面中的产品动态定价

如何设置Mailersend PHP API 模板变量?

为什么foreach循环会输出每个结果?

保留 .htaccess 中的符号登录 URL

如何使用在产品快速编辑菜单前端的自定义框中 Select 的值更新 woocommerce 产品属性值?

从 CSV 文件 seeder 数据库时的额外行

如何将在同一台服务器上运行的 2 个 PHP 应用程序与使用公共会话分开