NGINX `location` 指令中的 `expires -1` 是什么意思?

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

鉴于下面的示例

location
示例,
-1
对于
expires
意味着什么?这是否意味着“永不过期”或“永不缓存”?

# cache.appcache, your document html and data
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
  expires -1;
  access_log logs/static.log;
}

https://github.com/h5bp/server-configs-nginx/blob/b935688c2b/h5bp/location/expires.conf

caching nginx cache-control
3个回答
24
投票

根据 nginx 手册,该指令将

Expires
Cache-Control
HTTP 标头添加到响应中。

Value

-1
表示这些标头设置为:

Expires: 
当前时间减1秒

Cache-Control: no-cache

总而言之,它指示浏览器不要缓存文档。


8
投票

如果使用

expires -1
,则意味着这些页面永远不会被缓存。
expire
指令指示浏览器在一定时间(或特定时间)后使文件缓存过期。如果给出负值,则没有缓存。


1
投票

现有的答案都不是严格正确的。设置

expires: -1;
确实会让 nginx 添加
Cache-Control: no-cache
HTTP 标头。但这不会禁用缓存。相反,这会使任何缓存版本立即“过期”,从而要求客户端(浏览器)每次重新验证缓存(如果有)。

例如,如果客户端缓存了资源版本,则有义务通过向服务器发出请求来进行验证,该请求可以包含

If-None-Match
和/或
If-Modified-Since
标头,在这种情况下,服务器可以使用
304 Not Modified
进行回复,如果缓存的资源仍然是最新的。

编辑:MDN 文章中的这一部分强调了

no-cache
的含义及其与
no-store
的区别(后者实际上禁用了缓存)。

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