目录 搜索
Guides Access control CORS Authentication Browser detection using the user agent Caching Caching FAQ Compression Conditional requests Connection management in HTTP 1.x Content negotiation Content negotiation: List of default Accept values Cookies CSP Messages Overview Protocol upgrade mechanism Proxy servers and tunneling Proxy servers and tunneling: Proxy Auto-Configuration (PAC) file Public Key Pinning Range requests Redirections Resources and specifications Resources and URIs Response codes Server-Side Access Control Session Guides: Basics Basics of HTTP Choosing between www and non-www URLs Data URIs Evolution of HTTP Identifying resources on the Web MIME Types MIME types: Complete list of MIME types CSP Content-Security-Policy Content-Security-Policy-Report-Only CSP: base-uri CSP: block-all-mixed-content CSP: child-src CSP: connect-src CSP: default-src CSP: font-src CSP: form-action CSP: frame-ancestors CSP: frame-src CSP: img-src CSP: manifest-src CSP: media-src CSP: object-src CSP: plugin-types CSP: referrer CSP: report-uri CSP: require-sri-for CSP: sandbox CSP: script-src CSP: style-src CSP: upgrade-insecure-requests CSP: worker-src Headers Accept Accept-Charset Accept-Encoding Accept-Language Accept-Ranges Access-Control-Allow-Credentials Access-Control-Allow-Headers Access-Control-Allow-Methods Access-Control-Allow-Origin Access-Control-Expose-Headers Access-Control-Max-Age Access-Control-Request-Headers Access-Control-Request-Method Age Allow Authorization Cache-Control Connection Content-Disposition Content-Encoding Content-Language Content-Length Content-Location Content-Range Content-Type Cookie Cookie2 Date DNT ETag Expect Expires Forwarded From Headers Host If-Match If-Modified-Since If-None-Match If-Range If-Unmodified-Since Keep-Alive Large-Allocation Last-Modified Location Origin Pragma Proxy-Authenticate Proxy-Authorization Public-Key-Pins Public-Key-Pins-Report-Only Range Referer Referrer-Policy Retry-After Server Set-Cookie Set-Cookie2 SourceMap Strict-Transport-Security TE Tk Trailer Transfer-Encoding Upgrade-Insecure-Requests User-Agent User-Agent: Firefox Vary Via Warning WWW-Authenticate X-Content-Type-Options X-DNS-Prefetch-Control X-Forwarded-For X-Forwarded-Host X-Forwarded-Proto X-Frame-Options X-XSS-Protection Methods CONNECT DELETE GET HEAD Methods OPTIONS PATCH POST PUT Status 100 Continue 101 Switching Protocols 200 OK 201 Created 202 Accepted 203 Non-Authoritative Information 204 No Content 205 Reset Content 206 Partial Content 300 Multiple Choices 301 Moved Permanently 302 Found 303 See Other 304 Not Modified 307 Temporary Redirect 308 Permanent Redirect 400 Bad Request 401 Unauthorized 403 Forbidden 404 Not Found 405 Method Not Allowed 406 Not Acceptable 407 Proxy Authentication Required 408 Request Timeout 409 Conflict 410 Gone 411 Length Required 412 Precondition Failed 413 Payload Too Large 414 URI Too Long 415 Unsupported Media Type 416 Range Not Satisfiable 417 Expectation Failed 426 Upgrade Required 428 Precondition Required 429 Too Many Requests 431 Request Header Fields Too Large 451 Unavailable For Legal Reasons 500 Internal Server Error 501 Not Implemented 502 Bad Gateway 503 Service Unavailable 504 Gateway Timeout 505 HTTP Version Not Supported 511 Network Authentication Required Status
文字

Cache-Control通用头字段用于指定在两个,请求和响应的缓存机制的指令。缓存指令是单向的,这意味着请求中的给定指令并不意味着将在响应中给出相同的指令。

Header type

General header

Forbidden header name

no

CORS-safelisted response-header

yes

语法

这些指令不区分大小写,并且有一个可选参数,可以同时使用令牌和带引号的字符串语法。多个指令用逗号分隔。

缓存请求指令

Cache-Control客户端可以在 HTTP 请求中使用的标准指令。

Cache-Control: max-age=<seconds>Cache-Control: max-stale[=<seconds>]Cache-Control: min-fresh=<seconds>Cache-Control: no-cache 
Cache-Control: no-store
Cache-Control: no-transform
Cache-Control: only-if-cached

缓存响应指令

Cache-Control服务器可以在 HTTP 响应中使用的标准指令。

Cache-Control: must-revalidate
Cache-Control: no-cache
Cache-Control: no-store
Cache-Control: no-transform
Cache-Control: publicCache-Control: privateCache-Control: proxy-revalidate
Cache-Control: max-age=<seconds>Cache-Control: s-maxage=<seconds>

扩展Cache-Control指令

扩展Cache-Control指令不是核心 HTTP 缓存标准文档的一部分。请务必检查兼容性表格以获取支持。

Cache-Control: immutable 
Cache-Control: stale-while-revalidate=<seconds>Cache-Control: stale-if-error=<seconds>

指令

可缓存

public指示响应可能被任何缓存缓存。

private指示该响应是针对单个用户的,并且不能由共享缓存存储。私有缓存可以存储该响应。

no-cache在释放缓存副本之前,强制高速缓存将请求提交给原始服务器进行验证。

only-if-cached表示不检索新数据。客户端只希望获得缓存的响应,并且不应该联系原始服务器以查看是否存在新的副本。

Expiration

max-age=<seconds>指定资源被视为新鲜的最长时间。相反Expires,这个指令是相对于请求的时间而言的。

s-maxage=<seconds>覆盖max-ageExpires标题,但它只适用于共享缓存(例如,代理)并被私有缓存忽略。

max-stale[=<seconds>]表示客户愿意接受超过其到期时间的响应。或者,您可以以秒为单位指定一个值,指示响应不能过期的时间。

min-fresh=<seconds>表示客户想要的响应至少在指定的秒数内仍然是新鲜的。

stale-while-revalidate=<seconds>指示客户端愿意接受陈旧的响应,同时在后台异步检查新的响应。秒值指示客户愿意接受陈旧响应的时间。

stale-if-error=<seconds>

重新验证和重新加载

must-revalidate缓存必须在使用前验证陈旧资源的状态,不应使用过期资源。与之proxy-revalidate相同must-revalidate,但它仅适用于共享缓存(例如,代理)并被私有缓存忽略。

immutable表示响应主体不会随着时间而改变。资源如果未到期,则在服务器上保持不变,因此,即使用户明确刷新页面,客户端也不应为其发送条件重新验证(以检查更新。根据 HTTP 规范,不知道这个扩展的客户端必须忽略它们。在 Firefox 中,immutable只有在交易中才有荣誉。

其他

no-store缓存不应该存储有关客户端请求或服务器响应的任何内容。

no-transform不应该对资源进行转换或转换。Content-Encoding ,Content-Range ,Content-Type 标头不得被代理修改。例如,一个不透明的代理可能会在图像格式之间进行转换,以节省缓存空间或减少慢速链接上的流量。该no-transform指令不允许这样做。

例子

防止缓存

要关闭缓存,您可以发送以下响应标题。另外,请参阅ExpiresPragma标题。

Cache-Control: no-cache, no-store, must-revalidate

缓存静态资产

对于应用程序中不会更改的文件,通常可以通过在下面发送响应标题来添加主动缓存。这包括由应用程序提供的静态文件,例如图像,CSS 文件和 JavaScript 文件。另外,请参阅Expires标题。

Cache-Control: public, max-age=31536000

规范

Specification

Title

RFC 7234

Hypertext Transfer Protocol (HTTP/1.1): Caching

RFC 5861

HTTP Cache-Control Extensions for Stale Content

draft-mcmanus-immutable-00

HTTP Immutable Responses

浏览器兼容性

Feature

Chrome

Edge

Firefox

Internet Explorer

Opera

Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

immutable

No

15

49.0

No

No

No1

stale-while-revalidate

No2

No

No3

No

No

No

stale-if-error

No2

No

No3

No

No

No

Feature

Android

Chrome for Android

Edge mobile

Firefox for Android

IE mobile

Opera Android

iOS Safari

Basic Support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

immutable

No

No

No

No

No

No

No

stale-while-revalidate

No

No

No

No

No

No

No

stale-if-error

No

No

No

No

No

No

No

上一篇: 下一篇: