整理对“Cache-control”理解

本文详细解析了HTTP缓存控制机制中的Cache-Control、Expires及Last-Modified等关键头部字段的作用和使用方法,并提供了不同场景下缓存行为的具体案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概念

Cache-control 用于控制HTTP缓存(在HTTP/1.0中可能部分没实现,仅仅实现了Pragma: no-cache)

数据包中的格式:

Cache-Control:  cache-directive

cache-directive可以为以下:

request时用到:

| "no-cache"
| "no-store"
| "max-age" "=" delta-seconds
| "max-stale" [ "=" delta-seconds ]
| "min-fresh" "=" delta-seconds
| "no-transform"
| "only-if-cached"
| "cache-extension"

response时用到:

| "public"
| "private" [ "=" <"> field-name <"> ]
| "no-cache" [ "=" <"> field-name <"> ]
| "no-store"
| "no-transform"
| "must-revalidate"
| "proxy-revalidate"
| "max-age" "=" delta-seconds
| "s-maxage" "=" delta-seconds
| "cache-extension"

部分说明:
根据是否可缓存分为
Public  指示响应可被任何缓存区缓存。
Private  指示对于单个用户的整个或部分响应消息,不能被共享缓存处理。这允许服务器仅仅描述当用户的
部分响应消息,此响应消息对于其他用户的请求无效。
no-cache  指示请求或响应消息不能缓存(HTTP/1.0用Pragma的no-cache替换)
根据什么能被缓存
no-store  用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用缓存。
根据缓存超时
max-age  指示客户机可以接收生存期不大于指定时间(以秒为单位)的响应。
min-fresh  指示客户机可以接收响应时间小于当前时间加上指定时间的响应。
max-stale  指示客户机可以接收超出超时期间的响应消息。如果指定max-stale消息的值,那么客户机可以
接收超出超时期指定值之内的响应消息。
Expires

表示存在时间,允许客户端在这个时间之前不去检查(发请求),等同max-age的
效果。但是如果同时存在,则被Cache-Control的max-age覆盖。
格式:
Expires = "Expires" ":" HTTP-date
例如
Expires: Thu, 01 Dec 1994 16:00:00 GMT (必须是GMT格式


Cache-Control: no-cache:这个很容易让人产生误解,使人误以为是响应不被缓存。实际上Cache-Control: no-cache是会被缓存的,只不过每次在向客户端(浏览器)提供响应数据时,缓存都要向服务器评估缓存响应的有效性。

Cache-Control: no-store:这个才是响应不被缓存的意思。

Pragma: no-cache:跟Cache-Control: no-cache相同,Pragma: no-cache兼容http 1.0 ,Cache-Control: no-cache是http 1.1提供的。因此,Pragma: no-cache可以应用到http 1.0 和http 1.1,而Cache-Control: no-cache只能应用于http 1.1.


网页的缓存是由HTTP消息头中的“Cache-control”来控制的,常见的取值有private、no-cache、max-age、must-revalidate等,默认为private。其作用根据不同的重新浏览方式分为以下几种情况:

(1) 打开新窗口
如果指定cache-control的值为private、no-cache、must-revalidate,那么打开新窗口访问时都会重新访问服务器。而如果指定了max-age值,那么在此值内的时间里就不会重新访问服务器,例如:
Cache-control: max-age=5
表示当访问此网页后的5秒内再次访问不会去服务器
(2) 在地址栏回车
如果值为private或must-revalidate(和网上说的不一样),则只有第一次访问时会访问服务器,以后就不再访问。如果值为no-cache,那么每次都会访问。如果值为max-age,则在过期之前不会重复访问。
(3) 按后退按扭
如果值为private、must-revalidate、max-age,则不会重访问,而如果为no-cache,则每次都重复访问
(4) 按刷新按扭
无论为何值,都会重复访问

当指定Cache-control值为“no-cache”时,访问此页面不会在Internet临时文章夹留下页面备份。
另外,通过指定“Expires”值也会影响到缓存。例如,指定Expires值为一个早已过去的时间,那么访问此网时若重复在地址栏按回车,那么每次都会重复访问:Expires: Fri, 31 Dec 1999 16:00:00 GMT 。

Cache-Control: max-age=31536000

此种情况的话,打开新窗口或者在地址栏回车,的时候如果在这个时间内就不会重新访问服务器,上面表示缓存时间为1年。若按后退按扭则不会重新访问服务器,无论过没过期,按刷新按钮的话,无论过没过期都会重新访问。

Cache-Control:private

此种情况在打开新窗口以及刷新的时候会访问服务器,在地址栏输入的时候则只有第一次访问时会访问服务器,以后就不再访问,按后退按钮也不会访问服务器,但是有个很重要的就是不会会在中间的cache server中缓存,如squid就不会缓存。

Cache-Control:no-cache

此种情况的话每次都会访问服务器

Cache-Control:must-revalidate

此种情况与Cache-Control:private一样

Expires: Sat, 04 Dec 2010 01:00:43 GMT

Expires用于控制请求文件的有效时间,当请求数据在有效期内时客户端浏览器从缓存请求数据而不是服务器端. 当缓存中数据失效或过期,才决定从服务器更新数据。
可以使用Apache的mod_expires 模块来设置,这包括控制应答时的Expires头内容和Cache-Control头的max-age指令

当设置了expires后,会自动输出Cache-Control 的max-age 信息,这个数值是expires有效期内的秒数

Last-Modified: Wed, 22 Apr 2009 05:48:04 GMT

什么是”Last-Modified”?
在浏览器第一次请求某一个URL时,服务器端的返回状态会是200,内容是你请求的资源,同时有一个Last-Modified的属性标记此文件在服务期端最后被修改的时间

客户端第二次请求此URL时,根据 HTTP 协议的规定,浏览器会向服务器传送 If-Modified-Since 报头,询问该时间之后文件是否有被修改过。如果服务器端的资源没有变化,则自动返回HTTP/1.x 304 Not Modified状态码,内容为空,这样就节省了传输数据量。


server { listen 9007; server_name localhost; client_max_body_size 600M; proxy_connect_timeout 60s; # 代理连接超时时间 proxy_read_timeout 60s; # 代理读取超时时间 proxy_send_timeout 60s; # 代理发送超时时间 fastcgi_read_timeout 60s; # FastCGI 读取超时时间 send_timeout 60s; # 发送数据超时时间 #前端打的dist资源存放目录 root D:/ZrWrjServer/nginx-1.18.0/html; # 静态资源的缓存和压缩设置 location ~* ^/a-map/(.*)$ { alias /home/map/AMap/AMap/$1; etag on; expires 24h; gzip on; gzip_types image/jpeg image/png image/gif image/jpg; } location ~* ^/map-image/(.*)$ { alias /home/map/AMap/AMap/$1; etag on; expires 24h; gzip on; gzip_types image/jpeg image/png image/gif image/jpg; } location / { # 用于配合 browserHistory使用 try_files $uri $uri/ /index.html; } location /power-system/ { proxy_pass http://192.168.127.16:9006/power-system/; proxy_redirect off; #真实IP获取 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; set $my_proxy_add_x_forwarded_for $proxy_add_x_forwarded_for; if ($proxy_add_x_forwarded_for ~* "127.0.0.1"){ set $my_proxy_add_x_forwarded_for $remote_addr; } proxy_set_header X-Forwarded-For $my_proxy_add_x_forwarded_for; } location /api/ { proxy_pass http://192.168.127.16:9006/power-system/; proxy_redirect off; #真实IP获取 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; set $my_proxy_add_x_forwarded_for $proxy_add_x_forwarded_for; if ($proxy_add_x_forwarded_for ~* "127.0.0.1"){ set $my_proxy_add_x_forwarded_for $remote_addr; } proxy_set_header X-Forwarded-For $my_proxy_add_x_forwarded_for; } location /static/defect { # 设置缓存过期时间,例如1天 expires 1d; # 添加Cache-Control头,控制缓存行为 add_header Cache-Control "public"; root D:/ZrWrjServer/zrgn_wrj/defect; } location /static/video { # 设置缓存过期时间,例如1天 expires 1d; # 添加Cache-Control头,控制缓存行为 add_header Cache-Control "public"; root D:/ZrWrjServer/zrgn_wrj/video; } location /static/uav { # 设置缓存过期时间,例如1天 expires 1d; # 添加Cache-Control头,控制缓存行为 add_header Cache-Control "public"; root D:/ZrWrjServer/zrgn_wrj/uav; } location /static/imageManage { # 设置缓存过期时间,例如1天 expires 1d; # 添加Cache-Control头,控制缓存行为 add_header Cache-Control "public"; root D:/ZrWrjServer/zrgn_wrj/uav; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } 我在windows上使用这个nginx配置但是报错2025/07/14 16:17:59 [emerg] 7236#7276: "server" directive is not allowed here in D:\ZrWrjServer\nginx-1.18.0/conf/nginx.conf:1
最新发布
07-16
response返回的是res :>> {"header": {"Access-Control-Allow-Headers": "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,key,x-biz,x-info,platinfo,encr,enginever,gzipped,poiid", "Access-Control-Allow-Methods": "*", "Access-Control-Allow-Origin": "*", "Connection": "close", "Content-Encoding": "gzip", "Content-Type": "application/json", "Date": "Wed, 12 Mar 2025 06:20:18 GMT", "Server": "Tengine", "Transfer-Encoding": "chunked", "Vary": "Accept-Encoding", "gsid": "033017013222174176041837700030450875888", "protocol": "http/1.1", "sc": "0.004"}, "data": {"info": "USERKEY_PLAT_NOMATCH", "infocode": "10009", "status": "0", "sec_code_debug": "d41d8cd98f00b204e9800998ecf8427e", "key": "b96e4ccbfc8a4cc4ec27dc27b0ce2060", "sec_code": "d41d8cd98f00b204e9800998ecf8427e"}, "statusCode": 200, "profile": {"redirectStart": 0, "redirectEnd": 0, "fetchStart": 1741760418186, "domainLookUpStart": 1741760418189, "domainLookUpEnd": 1741760418189, "connectStart": 1741760418189, "connectEnd": 1741760418265, "SSLconnectionStart": 1741760418202, "SSLconnectionEnd": 1741760418265, "requestStart": 1741760418186, "requestEnd": 1741760418291, "responseStart": 1741760418290, "responseEnd": 1741760418291, "rtt": 24, "estimate_nettype": 5, "httpRttEstimate": 24, "transportRttEstimate": 11, "downstreamThroughputKbpsEstimate": 1600, "throughputKbps": 0, "peerIP": "203.209.230.17", "port": 443, "socketReused": false, "sendBytesCount": 678, "receivedBytedCount": 679, "protocol": "http/1.1", "usingHighPerformanceMode": true}, "cookies": [], "accelerateType": "none", "errMsg": "request:ok"}
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值