Symfony2、Varnish 和 ESI 导致奇怪的行为

标签 symfony varnish esi

我有以下配置:

varnish (80) <-> nginx (8080) <-> php-fpm (9000)

(使用 Apache 和 mod_php 的行为相同) 我的 Varnish 配置:

backend default {
    .host = "127.0.0.1";
    .port = "8080";
    .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
}

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


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

sub vcl_deliver {
    if (obj.hits > 0) {
        set resp.http.X-Varnish-Cache = "HIT Varnish (" +obj.hits+ ")";
    } else {
        set resp.http.X-Varnish-Cache = "MISS";
    }
}

ESI 在 app/config/config.yml 中打开。我在 symfony 中配置了以下路由:

  • /esiouter使用 s-maxage 60 并具有 /esiinner 的 esi-include (使用“普通”esi-tag 或 twig-render 函数和 {'standalone': true} ): <esi:include src="/esiinner" />
  • /esiinner s-maxage 10(由 esi-include 获取)

现在,当我在 web/app.php 中启用 AppCache 时symfony 评估 ESI 标签,因此 varnish 无法获取它们,我们有一个 Content-Length标题和内容未分块。如果我禁用 AppCache,varnish 会评估 ESI 标记并发送分块的内容,并且没有 Content-Length header 。

为什么 Varnish 发送分块响应而不缓冲 esi block 并发送整个页面? 如果我在带有 ESI 的 Symfony 应用程序前面使用 Varnish,我是否必须使用 Symfonys AppCache?

最佳答案

如果您有软件网关缓存/反向代理(如 Varnish),则无需启用 AppCache(这是用 PHP 编写的 Symfony2 反向代理)。

启用 AppCache 可能会导致行为不一致,因为您将拥有 2 个反向代理。

关于Symfony2、Varnish 和 ESI 导致奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8472979/

相关文章:

php - 将字符串日期转换为数据库的对象日期

php - 使用 twig 渲染图像的 VichUploaderBundle 问题

varnish - 用于更改 request.url 的 VCL 片段

varnish - 如何在 Varnish 中做https?

symfony - 父请求和 ESI 子请求之间有任何通信( Varnish 、 session 需求)吗?

akamai - esi :include or esi:eval statement be stored in an esi variable? 的结果可以吗

symfony - MonologBu​​ndle FingerCrossedHandler : how to configure excluded_404s

mysql - Doctrine 交响曲

php - 如何从 ESI :include script? 中设置 cookie

php - Symfony2 选择性 ESI 缓存