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 directivesproxy_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"頭。