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

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

问题描述

我使用 PHP 生成动态网页.如以下教程所述(请参阅下面的链接),当 $_SERVER['HTTP_ACCEPT'] 允许时,XHTML 文档的 MIME 类型应为application/xhtml+xml".由于您可以使用 2 个不同的 MIME(application/xhtml+xml"和text/html")为同一页面提供服务,因此您应该将Vary"HTTP 标头设置为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

现在我不确定以下情况的含义:header('变化:接受');我不太确定变化:接受"会做什么......

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/>

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

任何人都可以给我这个标题的真实"解释(具有该值).我想我明白这样的事情:变化:接受编码代理上的缓存可能基于所提供页面的编码,但我不明白:变化:接受

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

推荐答案

  • 缓存控制 标头是 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)

      在某些情况下,cache-control 指令是不够的.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:

      变化 严格用于代理复制服务器将执行的操作无望或过于复杂的情况.

      • 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-storePragma: 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 请求标头接受"定义了客户端可以处理的内容类型.如果您在同一个 URL 上有两个相同内容的副本,仅在 Content-Type 上有所不同,那么使用 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.

          12 年 9 月 11 日更新:

          我在评论中添加了一些链接,这些链接自此评论最初发布以来就出现在评论中.它们都是 Vary 的真实示例(和问题)的极好资源:接受;如果您正在阅读此答案,则还需要阅读这些链接.

          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 相关的意外行为的持续讨论:Backing 无法正确处理 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.

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

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