Symfony2 Reverse Proxy - split caching of the same URL based on cookie or other setting

I use Symfony2's reverse proxy by default, and I need to separate the caching of the same URL based on cookie settings.

The site allows the "basic" look of the site, reducing the number of images and removing JavaScript. Since the content is the same, I used the same URL, but of course the caching problem is the problem.

I need to be able to cache them separately (or just provide a clear cache).

I tried to change the Vary header, which I usually set:

Vary: Accept-Encoding 

.. and installed it either:

 Vary: Accept-Encoding, basic 

.. or:

 Vary: Accept-Encoding, normal 

It really works brilliantly in Chrome on my Mac, but Safari ignores it. I stopped checking other browsers at this point.

What is the best way to do this?

+2
source share
1 answer

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

This is especially useful if you have older browsers that do not support gzip, which serve the page without gzip, and for newer browsers with gzip ... so your reverse proxy will cache both versions of the same URL. Without this setting, your reverse proxy might end up serving gzipped content for browsers that don't support it ... providing unwanted results.

What you're looking for is probably an ETag header that looks like a cookie for caching.

The client will send etag its cached version, and you can choose from your application that the caching 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); } 

inspired by this blog post.

+1
source

Source: https://habr.com/ru/post/1482752/


All Articles