我配置了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 { …Run Code Online (Sandbox Code Playgroud) 我尝试标注
* @Cache(expires="+10 hours", public=false)
Run Code Online (Sandbox Code Playgroud)
或在控制器中
$maxAge = 60*60;
$response->setExpires(Carbon::create()->addHour());
$response->setSharedMaxAge($maxAge);
$response->setPublic();
$response->setMaxAge($maxAge);
Run Code Online (Sandbox Code Playgroud)
而且还有 Cache-Control: max-age=0, must-revalidate, private
应用使用会话,用户正在登录-我想要-缓存私有,但没有任何效果-我总是能得到这个。
我已经添加 FOS\HttpCacheBundle\FOSHttpCacheBundle()
(只需添加)希望它能覆盖symfony缓存并允许将发送缓存设为私有-但什么都不会改变。
我在Symfony Web应用程序前面使用CloudFront作为缓存.要根据用户的角色(admin,customer,...)获取缓存,我会在Lambda @ Edge Viewer请求触发器中生成基于用户角色的哈希.我将该哈希作为请求标头传递给我的原点X-User-Context-Hash.
我现在的问题是我需要将PHPSESSIDcookie 传递给我的源,以获得正确的缓存响应,但我不想将缓存基于值的PHPSESSID.我只需要根据X-User-Context-Hash我的会话cookie 的值而不是我的缓存响应.
下面的图片应该详细解释我的问题
有没有可能实现这一目标?
非常感谢任何帮助.
这是我的Lambda @ Edge Viewer请求触发器:
'use strict';
function parseCookies(headers) {
const parsedCookie = {};
if (headers.cookie) {
console.log(`${headers.cookie[0].value}`);
headers.cookie[0].value.split(';').forEach((cookie) => {
if (cookie) {
const parts = cookie.split('=');
parsedCookie[parts[0].trim()] = parts[1].trim();
}
});
}
return parsedCookie;
}
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const https = require('https');
// Read session cookie
const parsedCookies …Run Code Online (Sandbox Code Playgroud) amazon-web-services symfony amazon-cloudfront aws-lambda foshttpcachebundle
问题:即使在注销后,点击浏览器中的后退按钮也会呈现安全页面的缓存版本。
我正在尝试使用FOSHttpCacheBundle. 这是我的配置:
fos_http_cache:
cache_control:
defaults:
overwrite: true
rules:
-
match:
path: ^/
headers:
cache_control: { no_cache: true }
Run Code Online (Sandbox Code Playgroud)
虽然可以设置Cache-Control:no-cache, private但没有Pragma: no-cache和Expires: -1头文件中定义了根据文档应当被定义。
这是我的响应标头:
Cache-Control:no-cache, private
Connection:Keep-Alive
Content-Type:text/html; charset=UTF-8
Date:Tue, 03 May 2016 09:03:49 GMT
Keep-Alive:timeout=5, max=95
Server:Apache/2.4.9 (Win64) PHP/5.5.12
Transfer-Encoding:chunked
X-Cache-Debug:1
X-Debug-Token:050e09
X-Debug-Token-Link:/app_dev.php/_profiler/050e09
X-Powered-By:PHP/5.5.12
Run Code Online (Sandbox Code Playgroud)
有人请解释我错过了什么!!
谢谢
caching cache-control browser-cache symfony foshttpcachebundle