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"头。

相关内容

    暂无相关文章