What is the function of the "Vary: Accept" HTTP header?

I use PHP to create dynamic web pages. As indicated in the next tutorial (see Link below), the XHTML MIME type of document should be "application / xhtml + xml" when $ _SERVER ['HTTP_ACCEPT'] permits it. Since you can serve the same page with two different MIMEs ("application / xhtml + xml" and "text / html"), you must set the "Vary" HTTP header to "Accept". This will help proxy cache.

Link: http://keystonewebsites.com/articles/mime_type.php

Now I'm not sure about the following: header ('Vary: Accept'); I'm not quite sure that “Vari: Accept” will be for sure ...

The only explanation I found is:

After the Content-Type header, the Vary header is sent (if I understand it correctly) to tell intermediate caches, like proxies, that the content type of the document changes depending on the capabilities of the client who requests the document. http://www.456bereastreet.com/archive/200408/content_negotiation/

Anyone can give me a "real" explanation of this header ( with this value ). I think I understand things like: Vary: Accept-Encoding where the cache on proxies can be based on the encoding of the served page, but I don’t understand: Vary: Accept

+81
caching proxy
Dec 29 '09 at 16:15
source share
4 answers
  • The cache-control header is the main mechanism for the HTTP server, which tells the caching proxy the "freshness" of the response, (i.e. how / if the response is stored in the cache for a long time)

  • In some situations, cache-control directives are insufficient. A discussion of the HTTP working group is archived here , describing a page that only changes in the language. This is not a valid use case for a variable header, but this context is valuable for our discussion. (Although I believe that the Vary header will solve the problem in this case, there is a better way.) From this page:

Vary strictly for those cases where it is hopelessly or overly complicated for a proxy server to replicate what the server will do.

A contrived example:

Your HTTP server has a large landing page. You have two slightly different pages with the same URL, depending on whether the user has been before. You distinguish between requests and the user "visits the account" based on cookies. But, since your server landing page is so large, you want mediation proxies to cache the response, if possible.

The URL, Last-Modified, and Cache-Control headers are not enough to give this view to the caching proxy, but if you add Vary: Cookie , the caching mechanism will add a Cookie header to its caching solutions.

Finally, for small traffic, dynamic websites - I always considered simple Cache-Control: no-cache, no-store and Pragma: no-cache sufficient.

Edit - to more accurately answer your question: the header of the Accept HTTP request defines the types of content that the client can process. If you have two copies of the same content at the same URL that differ only in the content type, then using Vary: Accept may be appropriate.

Update September 11th:

I include a couple of links that appeared in the comments, as this comment was originally posted. They are both excellent resources for real-world examples (and problems) using Vary: Accept; If you are reading this answer, you also need to read these links.

The first, from the outstanding EricLaw, from the behavior of Internet Explorer with the Vary header and some of the problems it presents to developers: Vary Header prevents caching in IE . In short, IE (pre IE9) does not cache any content that uses the Vary header because the request cache does not include HTTP request headers. EricLaw (Eric Lawrence in the real world) is a program manager on the IE team.

The second is from Eran Medan, and this is a constant discussion of the unexpected behavior related to Vary in Chrome: Backup does not handle the Vary header correctly . This is due to the behavior of IE, except that the Chrome developers took a different approach, although it did not seem to be a deliberate choice.

+85
Dec 29 '09 at 16:59
source

Vary: Accept simply says that the response was generated based on the Accept header in the request. A request with a different Accept header may receive a different response.

(You can see that the associated PHP code looks like $HTTP_ACCEPT . This is the value of the Accept request header.)

In HTTP caches, this means that the response must be cached with extreme care. It will only be valid for subsequent requests with exactly the same Accept header.

Now it matters only if the page is cached in the first place. By default, PHP pages are not. A PHP page can mark the output as cached by sending certain headers (e.g. Expires ). But whether and how to do this is another question.

+52
Dec 29 '09 at 17:53
source

In fact, at the moment there are a significant number of new features (and already in Chrome) that make the Vary header extremely useful. For example, consider a customer prompt . For example, when used in conjunction with images, a client prompt allows the server to optimize resources, such as images, depending on:

  • Image width
  • Viewport width
  • Type of encoding supported by browser (think WebP)
  • Down (essentially network speed)

Thus, a server that supports these features will set a Vary header to indicate this.

Chrome advertises WebP support by setting "image / webp" as part of the Vary header for each request. Thus, the server can rewrite the image as WebP if the browser supports it, so the proxy server will need to check the header so that it does not cache the WebP image and then serve it in a browser that does not support WebP. Obviously, if your server does not, it does not matter. Since the server response depends on the Accept request header, the response should include this so as not to confuse the proxy:

 Vary: Accept 

Another example would be the width of an image. In a mobile browser, the Width header can be quite small for a responsive image, compared to what it will be when viewed from the desktop browser. Therefore, in this case, Width will be added to the Vary header so that the proxy server does not cache the small mobile version and does not serve it on desktop browsers, or vice versa. In this case, the title may include:

 Vary: Accept, Width 

Or in the event that the server supports all client hint specifications, the header will look something like this:

 Vary: Accept, DPR, Width, Save-Data, Downlink 
+2
Sep 09 '15 at 20:59
source

This Google Webmaster video contains a very good explanation of the Vary HTTP header.

+1
Jan 31 '15 at 7:30
source



All Articles