10

我配置了 Symfony 和 FOSHttpCacheBundle(遵循FOSHttpCache 文档中的 Varnish 配置说明)。

我向控制器添加了一个操作,该操作test在响应 HTTP 标头中添加了一个标记:

<?php

use FOS\HttpCacheBundle\Configuration\Tag;

class MyController extends Controller
{
    /**
     * @Route("/test1", name="acme_my_test1")
     * @Tag("test")
     */
    public function test1Action()
    {
        return new Response(rand(0, 1000));
    }
}

但是,每当我调用/test1URL 时,数字都会改变,显示缓存未激活。请注意,我的应用程序不使用任何 cookie,并且我可以测试标头是否已发送到 Varnish(由于该指令X-Cache-Tags,它会在对浏览器的响应中将其剥离)。vlc_deliver

配置中有什么我会错过的吗?Varnish 和 Nginx 都运行在同一台服务器上。

这是我的 Symfonyconfig.yml文件中与 HTTP 缓存相关的配置:

framework:
    trusted_hosts:   ~
    trusted_proxies:  [127.0.0.1]

fos_http_cache:
    proxy_client:
        varnish:
            servers: 127.0.0.1:80
            base_url: mywebsite.localhost.com
    tags:
        enabled: true

和清漆配置/etc/varnish/default.vcl

vcl 4.0;

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

acl invalidators {
    "localhost";
}

sub vcl_recv {
    if (req.http.X-Forwarded-Proto == "https" ) {
        set req.http.X-Forwarded-Port = "443";
    } else {
        set req.http.X-Forwarded-Port = "80";
    }

    set req.http.Surrogate-Capability = "abc=ESI/1.0";

    if (req.method == "PURGE") {
        if (!client.ip ~ invalidators) {
            return (synth(405, "Not allowed"));
        }
        return (purge);
    }

    if (req.http.Cache-Control ~ "no-cache" && client.ip ~ invalidators) {
        set req.hash_always_miss = true;
    }

    if (req.method == "BAN") {
        if (!client.ip ~ invalidators) {
            return (synth(405, "Not allowed"));
        }

        if (req.http.X-Cache-Tags) {
            ban("obj.http.X-Host ~ " + req.http.X-Host
                + " && obj.http.X-Url ~ " + req.http.X-Url
                + " && obj.http.content-type ~ " + req.http.X-Content-Type
                + " && obj.http.X-Cache-Tags ~ " + req.http.X-Cache-Tags
            );
        } else {
            ban("obj.http.X-Host ~ " + req.http.X-Host
                + " && obj.http.X-Url ~ " + req.http.X-Url
                + " && obj.http.content-type ~ " + req.http.X-Content-Type
            );
        }

        return (synth(200, "Banned"));
    }
}

sub vcl_backend_response {
    set beresp.http.X-Url = bereq.url;
    set beresp.http.X-Host = bereq.http.host;

    if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
        unset beresp.http.Surrogate-Control;
        set beresp.do_esi = true;
    }
}

sub vcl_deliver {
    if (!resp.http.X-Cache-Debug) {
        unset resp.http.X-Url;
        unset resp.http.X-Host;
        unset resp.http.X-Cache-Tags;
    }
}
4

1 回答 1

5

好的,我找到了。除了我必须添加一些过期标头之外,我的配置一切都很好。我认为标签标头就足够了,但也需要一些长期过期标头。

动作必须看起来像这样:

<?php

use FOS\HttpCacheBundle\Configuration\Tag;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;

class MyController extends Controller
{
    /**
     * @Route("/test1", name="acme_my_test1")
     * @Tag("test")
     * @Cache(expires="+1 year")
     */
    public function test1Action()
    {
        return new Response(rand(0, 1000));
    }
}

我仍然对 ESI 标签和标签有一点问题,但这超出了这个问题的范围。

于 2015-12-06T18:08:07.743 回答