可以说,我的后端需要2秒的可怕反应(我们的控制权说),我永远不会想要访问者体验这种延迟,我们不受cookie或用户会话的影响。
我想在nginx中做的是这样的:
server { location / { # server requests from cache unless for some reason its not in cache proxy_pass http://backend/ proxy_cache mycache proxy_cache_is_valid 2m } location / where request IP = 127.0.0.1 { proxy_pass http://backend/ do_not_use_cache but_store response in cache } }
这样我就可以从本地主机运行一个简单的curl任务,每隔30秒运行一次,以保持caching新鲜/热与我需要的几个页面,我不希望访问者是一个温暖的caching。 我读过文档,但不知道如何做到这一点。
试试这个configuration。
http { # http section directives proxy_cache_path /path/to/cache levels=1:2 keys_zone=mycache; geo $bypass_by_ip { default 0; 127.0.0.1 1; } server { # other server directives location / { proxy_cache mycache; proxy_cache_valid any 2m; proxy_cache_use_stale timeout updating; proxy_cache_bypass $bypass_by_ip; proxy_pass ...; } } }
proxy_cache_bypass发出直接请求,绕过caching。 这由它的参数控制 – 如果它们中的任何一个不是一个空string或0.我使用geo来提供这样一个值(默认为0,当远程IP为127.0.0.1时为1)
注意你至less需要Nginx版本1.0.4才能工作。 早期版本在proxy_cache_bypass / proxy_cache_nocache逻辑中有一个错误。