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

时间:2012-04-25 10:53:11

标签: http http-headers request protocols httpresponse

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

  

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

但我还不明白它的作用?值为Cache-Control的{​​{1}}标头和值no-cache的{​​{1}}之间有什么区别?

3 个答案:

答案 0 :(得分:162)

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

答案 1 :(得分:89)

没有区别,除了Pragma仅定义为适用于客户端的请求,而Cache-Control可以由客户端的请求和服务器的回复使用。

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

  

HTTP / 1.1缓存应该像客户端一样对待“Pragma:no-cache”   发送“Cache-Control: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中的书面或暗示不同!

答案 2 :(得分:6)

| Stop using          | Replace with                     |
| (HTTP 1.0)          | (HTTP 1.1 - 1999)                |
|---------------------|----------------------------------|
| Expires: [date]     | Cache-Control: max-age=[seconds] |
| Pragma: no-cache    | Cache-Control: nocache           |

如果是在1999年之后,你还在使用过期 Pragma ,那你就错了。

我正在看着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

tl; dr:Pragma是HTTP / 1.0的遗产,自Internet Explorer 5或Netscape 4.7以来一直不需要。