Pragma 和 Cache-Control 标头之间的区别?

问题描述 投票:0回答:3

我在 Wikipedia 上读到了关于 Pragma 标题,其中写着:

“Pragma: no-cache 标头字段是一个 HTTP/1.0 标头,旨在用于 在请求中使用。它是浏览器告诉服务器的一种手段 任何需要新版本资源的中间缓存, 服务器不会告诉浏览器不要缓存资源。一些 用户代理确实会注意响应中的此标头,但是 HTTP/1.1 RFC 特别警告不要依赖此行为。”

但我还没明白它的作用是什么?值为

Cache-Control
no-cache
标头和值为
Pragma
no-cache
有什么区别?

http http-headers request protocols httpresponse
3个回答
250
投票

Pragma
是 HTTP/1.0 实现,
cache-control
是同一概念的 HTTP/1.1 实现。它们都是为了防止客户端缓存响应。较旧的客户端可能不支持 HTTP/1.1,这就是该标头仍在使用的原因。


112
投票

没有什么区别,只是

Pragma
仅定义为适用于客户端的请求,而
Cache-Control
可以用于客户端的请求和服务器的回复。

所以,就标准而言,只能从客户端发出请求和服务器接收客户端请求的角度进行比较。 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.32 定义场景如下:

HTTP/1.1 缓存应该将“Pragma: no-cache”视为客户端有 发送“缓存控制:无缓存”。不会有新的 Pragma 指令 在 HTTP 中定义。

  Note: because the meaning of "Pragma: no-cache as a response
  header field is not actually specified, it does not provide a
  reliable replacement for "Cache-Control: no-cache" in a response

我阅读上述内容的方式:

  • 如果您正在编写客户端并且需要

    no-cache

    • 只需在请求中使用
      Pragma: no-cache
      ,因为您可能不知道服务器是否支持
      Cache-Control
    • 但在回复中,要决定是否缓存,请检查
      Cache-Control
  • 如果您正在编写服务器:

    • 在解析来自客户端的请求时,检查
      Cache-Control
      ;如果没有找到,则检查
      Pragma: no-cache
      ,并执行
      Cache-Control: no-cache
      逻辑;
    • 在回复中,提供
      Cache-Control

当然,实际情况可能与 RFC 中所写或暗示的有所不同!


31
投票

tl;dr:

Pragma
是 HTTP/1.0 的遗产,自 Internet Explorer 5 或 Netscape 4.7 以来不再需要。除非您预计某些用户会使用 IE5:停止使用它是安全的。

停止使用(HTTP 1.0) 替换为 (自 1999 年起为 HTTP 1.1)
到期时间:[日期] 缓存控制:max-age=[秒]
编译指示:无缓存 缓存控制:无缓存

如果 1999 年之后,你还在使用 ExpiresPragma,那么你就错了。

我正在看着你 Stackoverflow:

200 OK
Pragma: no-cache
Content-Type: application/json
X-Frame-Options: SAMEORIGIN
X-Request-Guid: a3433194-4a03-4206-91ea-6a40f9bfd824
Strict-Transport-Security: max-age=15552000
Content-Length: 54
Accept-Ranges: bytes
Date: Tue, 03 Apr 2018 19:03:12 GMT
Via: 1.1 varnish
Connection: keep-alive
X-Served-By: cache-yyz8333-YYZ
X-Cache: MISS
X-Cache-Hits: 0
X-Timer: S1522782193.766958,VS0,VE30
Vary: Fastly-SSL
X-DNS-Prefetch-Control: off
Cache-Control: private

  • 过期:
    [date]
    (已弃用 - HTTP 1.0)
  • Pragma: 无缓存 (已弃用 - HTTP 1.0)
  • 缓存控制: max-age=
    [seconds]
  • Cache-Control:无缓存(每次都必须重新验证缓存的副本)

还有条件请求:

  • 基于Etag(实体标签)的条件请求
    • 服务器:
      Etag: W/“1d2e7–1648e509289”
    • 客户:
      If-None-Match: W/“1d2e7–1648e509289”
    • 服务器:
      304 Not Modified
  • 基于修改日期的条件请求
    • 服务器:
      last-modified: Thu, 09 May 2019 19:15:47 GMT
    • 客户:
      If-Modified-Since: Fri, 13 Jul 2018 10:49:23 GMT
    • 服务器:
      304 Not Modified

最后修改时间:2019 年 5 月 9 日星期四 19:15:47 GMT

© www.soinside.com 2019 - 2024. All rights reserved.