我有以下配置:
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/