简体   繁体   中英

nginx use stale cache not working

I am trying to use proxy_cache_use_stale error; to let the nginx serve a cached page when a target returns http status 500 internal error. I have the following setup:

location /test {
                proxy_cache     maincache;
                proxy_cache_valid 200 10s;
                proxy_cache_use_stale error;
                proxy_pass      http://127.0.0.1:3000/test;
        }
 location /toggle {
                proxy_pass      http://127.0.0.1:3000/toggle;
}

Test will return either the current time and Http status 200 or the current time and http status 500. If i call /toggle the value returned from /test will switch from 200 to 500.

My expectation was that I should be able to send a call to /test and get the current time. I should then be able to send a call to /toggle and calls to /test would return the time when the function was first called. What is happening is that it keeps its last cache for 10 seconds and then sending back the current time and not using cache at all.

I understand that setting proxy_cache_valid 200 10s; will keep it from refreshing the cache when something other than 500 is returned and store new content in the cache when 10 seconds has passed and a none error message is returned.

What i assumed after reading the documentation, old cache would not be automatically cleared until time passed equal to the inactive flag set for a cache. I have not set the inactive flag for cache so i expected the "proxy_cache_use_stale error" would prevent the cache from refreshing until either 10 minutes passed (default value when inactive is not defined), or errors are no longer returned. What part of the documentation have i misunderstood? How should this be done correctly?

Nginx documentation that i am refering to is the one found here. http://nginx.org/en/docs/http/ngx_http_proxy_module.html?&_ga=1.112574977.446076600.1424025436#proxy_cache

您应该使用“ http_500”而不是“错误”,请参阅http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream(proxy_cache_use_stale使用与proxy_next_upstream相同的参数)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM