
正常情況下,使用proxy_cache_bypass
nginx 時會從上游取得一份新副本,並用新副本覆蓋快取的回應。
但是,如果 URL 從可快取回應變更為不可快取回應(例如,使用 的 4xx 回應Cache-Control: no-cache
),則 usingproxy_cache_bypass
確實會提供來自上游的新副本,但會將舊副本保留在快取中。
這意味著每次在沒有觸發的情況下請求 URL 時proxy_cache_bypass
,它將繼續為舊快取提供服務。
我想這是一個有意的行為,因為proxy_cache_bypass
只有覆蓋透過保存新的回應來快取回應,而no-cache
回應意味著沒有什麼可保存的?這是正在發生的事情嗎?
我該如何解決這個問題?我不想為 4xx 響應啟用緩存...
我正在運行 nginx/1.14.2
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my-cache:70m max_size=28g inactive=1d;
proxy_temp_path /var/cache/nginx/tmp;
proxy_cache my-cache;
proxy_cache_key $remote_user$scheme$host$request_uri;
proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504 http_429;
proxy_cache_bypass $http_cache_control;
proxy_read_timeout 90;
add_header X-Cache-Status $upstream_cache_status;
etag off;
答案1
您可以透過指定告訴 nginx 僅快取 200、301 和 302 回應
proxy_cache_valid 10m;
這告訴 nginx 將有效回應快取十分鐘。
答案2
這是預期的行為。這就是為什麼他們希望您使用付費版本來獲得代理快取清除指示