nginx缓存过期管理小结

nginx缓存过期管理小结

先看一个经典的配置:

proxy_cache_path /cache levels=1:2 keys_zone=cache_pool:512minactive=1m max_size=10g;

location ~ .*\.(jpg|gif|png)$

{

    proxy_next_upstream   http_502 http_504 error timeout invalid_header;

    proxy_cache           cache_pool;

    proxy_cache_valid     200 304 2m;

    proxy_cache_key       $host$uri$is_args$args;

    proxy_set_header      Host $host;

    proxy_set_header      X-Forwarded-For  $remote_addr;

    proxy_pass            http://storage1;

    expires               10m;

}

我在论坛上看到有人问过,说这三个时间到底是什么意思,他们有什么用处?先看官网的解释:

Cached data that are not accessed during the time specified by the inactive parameter get removed from the cache regardless of their freshness. By default,inactive is set to 10 minutes.

(被缓存的数据如果在inactive参数指定的时间内未被访问,就会被从缓存中移除,不论它是否是刚产生的。inactive的默认值是10分钟)

Sets caching time for different response codes. For example, the following directives

proxy_cache_valid 200 302 10m;

proxy_cache_valid 404      1m;

set 10 minutes of caching for responses with codes 200 and 302, and 1 minute for responses with code 404.

expires: Controls whether the response should be marked with an expiry time, and if so, what time that is.

这里总结下:

        inactive的时间表示一个文件在指定时间内没有被访问过,就从存储系统中移除,不管你proxy_cache_valid里设置的时间是多少。而proxy_cache_valid在保证inactive时间内被访问过的前提下,最长的可用时间。proxy_cache_valid定义的其实是一个绝对过期时间(第一次缓存的时间+配置的缓存时间),到了这个点,对象就被认为是过期,然后去后端重取数据,尽管它被访问的很频繁(即所谓的inactive时间内)。expires呢,它不在这个过期控制体系内,它用在发给客户端的响应中,添加"Expires"头。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值