如何使用FOSHttpCacheBundle和Varnish进行缓存标记?

时间:2015-12-01 13:44:05

标签: symfony varnish foshttpcachebundle

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

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

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

但是,每当我调用/test1 URL时,数字都会更改,表示缓存未处于活动状态。 请注意,我的应用程序没有使用任何cookie,我可以测试X-Cache-Tags标头是否被发送到Varnish(由于vlc_deliver指令,它在浏览器的响应中将其删除)。

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

以下是我的Symfony config.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中的Varnish配置:

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

1 个答案:

答案 0 :(得分: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标签和标签仍然存在一些问题,但这不属于这个问题的范围。