Symfony2反向代理-根据cookie或其他设置分隔相同URL的缓存 [英] Symfony2 reverse proxy - separating the caching of the same URL based on a cookie or other setting

查看:110
本文介绍了Symfony2反向代理-根据cookie或其他设置分隔相同URL的缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是默认的Symfony2反向代理,因此我需要根据cookie设置对同一URL进行缓存。

I'm using the default Symfony2 reverse proxy, and I need to separate caching of the same URL based on a cookie setting.

该网站允许使用缩小图片并删除JavaScript,从而实现基本网站视图。由于内容相同,因此我使用了相同的URL,但是缓存当然是个问题。

The site allows for a 'basic' site view by scaling down images and removing JavaScript. As the content is the same I have used the same URL, but of course caching is an issue.

我需要能够分别缓存它们(或只是确保

I need to be able to cache them separately (or just ensure the cache is cleared).

我尝试更改通常设置为的Vary标头:

I have tried changing the Vary header which normally I have set to:

Vary: Accept-Encoding

..并将其设置为:

Vary: Accept-Encoding, basic

.. or:

Vary: Accept-Encoding, normal

在Mac上的Chrome浏览器中,这确实很不错,但Safari却忽略了它。我此时已停止检查其他浏览器。

That actually works brilliant in Chrome on my Mac, but Safari ignores it. I stopped checking other browsers at this point.

最好的方法是什么?

推荐答案

不同:Accept-Encoding告诉客户端或您的反向代理为不同的编码分别缓存URL。 (即具有/不具有gzip)。

Vary: Accept-Encoding tells the client or your reverse proxy to seperate caching of the url for different encodings. ( i.e. with/without gzip).

如果您有较旧的浏览器不支持不带gzip的gzip的页面,而对于带gzip的较新的浏览器,这特别有用,那么您的反向代理将缓存这两个变体相同的网址。没有此设置,您的反向代理可能最终会向不支持它的浏览器提供压缩后的内容,从而导致不必要的结果。

This is especially useful if you have older browsers not supporting gzip being served the page without gzip and for newer browsers with gzip ... so your reverse proxy will cache both variants of the same url. Without this setting your reverse proxy might end up serving gzipped content to browsers not supporting it... giving unwanted results.

您正在寻找的可能是 ETag 标头,有点像用于缓存的 cookie。

What you are looking for is probably the ETag header which is kind of like a "cookie" for caching.

客户端将发送其缓存版本的etag,然后您可以从应用程序中选择客户端的缓存版本是否有效。

The client will send the etag of his cached version and you can then choose from your application wether the client's cached version is valid or not.

$response = new Response();
$response->setETag(md5('some_identifier'));
if( $response->isNotModified($this->get('request')) )
{
    // automatically returns null content response with http 304 ( not modified ) header
    return $response; 
}
else
{
    // .. otherwise return a new response, possibly with a different ETag

    // $reponse->setEtag(md5('another_identifier'));
    return  $this->renderView('MyBundle:Main:index.html.twig', array(), $response);
}

灵感来自博客文章。

这篇关于Symfony2反向代理-根据cookie或其他设置分隔相同URL的缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆