Mic*_*rin 10 varnish symfony foshttpcachebundle
我配置了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));
}
}
Run Code Online (Sandbox Code Playgroud)
但是,每当我调用/test1URL时,数字都会更改,表示缓存未处于活动状态.请注意,我的应用程序不使用任何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
Run Code Online (Sandbox Code Playgroud)
和Varnish配置/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;
}
}
Run Code Online (Sandbox Code Playgroud)
好的,我找到了.我的配置一切都很好,除了我必须添加一些过期标头.我认为标签标题足够了,但也需要一些长时间的过期标头.
该动作必须如下所示:
<?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));
}
}
Run Code Online (Sandbox Code Playgroud)
ESI标签和标签仍然存在一些问题,但这超出了这个问题的范围.