简体   繁体   English

nginx使用过时的缓存不起作用

[英]nginx use stale cache not working

I am trying to use proxy_cache_use_stale error; 我正在尝试使用proxy_cache_use_stale错误; to let the nginx serve a cached page when a target returns http status 500 internal error. 当目标返回http status 500内部错误时,让nginx为缓存的页面提供服务。 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. 测试将返回当前时间和Http状态200或当前时间和http状态500。如果我调用/ toggle,则/ test返回的值将从200切换到500。

My expectation was that I should be able to send a call to /test and get the current time. 我的期望是,我应该能够向/ test发送呼叫并获取当前时间。 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. 然后,我应该能够发送对/ toggle的调用,对/ test的调用将返回第一次调用该函数的时间。 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. 发生的事情是它将最后一次缓存保持10秒钟,然后将其返回给当前时间,并且根本不使用缓存。

I understand that setting proxy_cache_valid 200 10s; 我知道将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. 当返回500以外的值时,它将阻止刷新缓存,并在10秒钟后返回空错误消息时将新内容存储在缓存中。

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. 我尚未为缓存设置非活动标志,因此我希望“ proxy_cache_use_stale错误”将阻止缓存刷新,直到经过10分钟(未定义非活动状态时为默认值),或者不再返回错误。 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. 我要参考的Nginx文档是在这里找到的。 http://nginx.org/en/docs/http/ngx_http_proxy_module.html?&_ga=1.112574977.446076600.1424025436#proxy_cache 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相同的参数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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