“Vary:Accept”的功能是什么? HTTP标头? [英] What is the function of the "Vary: Accept" HTTP header?

查看:260
本文介绍了“Vary:Accept”的功能是什么? HTTP标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP生成动态网页。如以下教程中所述(请参阅下面的链接),当$ _SERVER ['HTTP_ACCEPT']允许时,XHTML文档的MIME类型应为application / xhtml + xml。由于您可以使用2个不同的MIME(application / xhtml + xml和text / html)提供相同的页面,因此您应该将VaryHTTP标头设置为Accept。这将有助于代理上的缓存。

I use PHP to generate dynamic Web pages. As stated on the following tutorial (see link below), the MIME type of XHTML documents should be "application/xhtml+xml" when $_SERVER['HTTP_ACCEPT'] allows it. Since you can serve the same page with 2 different MIMEs ("application/xhtml+xml" and "text/html") you should set the "Vary" HTTP header to "Accept". This will help the cache on proxies.

链接:
http://keystonewebsites.com/articles/mime_type.php

现在我不确定其含义:
标题('变化:接受');
我不确定'Vary:Accept'会做什么......

Now I'm not sure of the implication of: header('Vary: Accept'); I'm not really sure of what 'Vary: Accept' will precisely do...

我找到的唯一解释是:


在Content-Type标头之后,发送一个Vary
标头(如果我理解它正确地为
)告诉中间缓存,
就像代理服务器一样,文档的内容
类型取决于
请求文档的客户端
的功能。
http://www.456bereastreet.com/archive/200408/content_negotiation/

任何人都可以给我这个标题的真实解释(带有该值)。我想我理解的事情如下:
Vary:Accept-Encoding
其中代理上的缓存可以基于所服务页面的编码,但我不明白:
变化:接受

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

推荐答案


  • cache-control header是HTTP服务器的主要机制告诉缓存代理响应的新鲜度。 (即,如何/如果长时间将响应存储在缓存中)

    • The cache-control header is the primary mechanism for an HTTP server to tell a caching proxy the "freshness" of a response. (i.e., how/if long to store the response in the cache)

      在某些情况下,缓存控制指令不足。来自HTTP工作组的讨论存档此处,描述了仅使用语言更改的页面。这是变量标题的正确用例,但上下文对我们的讨论很有价值。 (虽然我相信Vary标题会在这种情况下解决问题,但有更好的方法。)从该页面开始:

      In some situations, cache-control directives are insufficient. A discussion from the HTTP working group is archived here, describing a page that changes only with language. This is not the correct use case for the vary header, but the context is valuable for our discussion. (Although I believe the Vary header would solve the problem in that case, there is a Better Way.) From that page:


      Vary 严格适用于代理复制服务器所做的事情无望或过于复杂的情况。




      • RFC2616标题字段定义描述了从服务器角度来看的标题用法, RFC2616缓存协商响应从缓存代理的角度来看。它旨在指定一组HTTP请求标头,用于确定请求的唯一性。

        • RFC2616 "Header-Field Definitions" describes the header usage from the server perspective, RFC2616 "Caching Negotiated Responses" from a caching proxy perspective. It's intended to specify a set of HTTP request headers that determine uniqueness of a request.
        • 一个人为的例子:

          您的HTTP服务器有一个很大的目标网页。您有两个略有不同的页面具有相同的URL,具体取决于用户以前是否在那里。您可以根据Cookie区分请求和用户的访问次数。但是 - 由于您的服务器的登录页面太大,您希望中​​间代理在可能的情况下缓存响应。

          Your HTTP server has a large landing page. You have two slightly different pages with the same URL, depending if the user has been there before. You distinguish between requests and a user's "visit count" based on Cookies. But -- since your server's landing page is so large, you want intermediary proxies to cache the response if possible.

          URL,Last-Modified和Cache-Control标头是不足以将此洞察力提供给缓存代理,但如果添加 Vary:Cookie ,缓存引擎会将Cookie标头添加到其缓存决策中。

          The URL, Last-Modified and Cache-Control headers are insufficient to give this insight to a caching proxy, but if you add Vary: Cookie, the cache engine will add the Cookie header to its caching decisions.

          最后,对于小流量,动态网站 - 我总是找到简单的 Cache-Control:no-cache,no-store Pragma:no-cache 就足够了。

          Finally, for small traffic, dynamic web sites -- I have always found the simple Cache-Control: no-cache, no-store and Pragma: no-cache sufficient.

          编辑 - 更精确地回答您的问题:HTTP请求标头'Accept'定义客户端可以处理的内容类型。如果您在同一个网址上有两个相同内容的副本,但仅在内容类型上有所不同,那么使用 Vary:Accept 可能是合适的。

          Edit -- to more precisely answer your question: the HTTP request header 'Accept' defines the Content-Types a client can process. If you have two copies of the same content at the same URL, differing only in Content-Type, then using Vary: Accept could be appropriate.

          9月12日更新:

          我包括一些链接自评论最初发布以来,已经出现在评论中。对于Vary:Accept;它们都是真实世界范例(和问题)的优秀资源。如果你正在阅读这个答案,你也需要阅读这些链接。

          I'm including a couple links that have appeared in the comments since this comment was originally posted. They're both excellent resources for real-world examples (and problems) with Vary: Accept; Iif you're reading this answer you need to read those links as well.

          第一个,来自优秀的EricLaw,关于Internet Explorer与Vary标题的行为和一些它给开发人员带来的挑战: Vary Header阻止IE中的缓存。简而言之,IE(IE9之前版本)不会缓存任何使用Vary标头的内容,因为请求缓存不包含HTTP请求标头。 EricLaw(Eric Lawrence在现实世界中)是IE团队的项目经理。

          The first, from the outstanding EricLaw, on Internet Explorer's behavior with the Vary header and some of the challenges 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.

          第二个来自Eran Medan,并且正在讨论Chrome中与Vary相关的意外行为:支持无法正确处理Vary标头。它与IE的行为有关,除了Chrome开发者采取了不同的方法 - 虽然它似乎并不是一个慎重的选择。

          The second is from Eran Medan, and is an on-going discussion of Vary-related unexpected behavior in Chrome: Backing doesn't handle Vary header correctly. It's related to IE's behavior, except the Chrome devs took a different approach -- though it doesn't appear to have been a deliberate choice.

          这篇关于“Vary:Accept”的功能是什么? HTTP标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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