NGINX代理服务器与caching作为备份

我们想要build立一个NGINX服务器,它将所有接收到的请求代理到上游服务器,但是当上游服务器失败/变得不可用时,它应该回退到最近接收文件的本地caching。 什么是最好的方式来设置?

我认为这样做的最好方法是将5xx错误文档设置为类似的东西

error_document 500 502 503 504 =200 /cache/; location /cache/ { #Send cached files } 

但我不确定如何有效地让NGINXcaching所有文件,同时仍然代理上游,然后如何通过一个位置从caching中取回文件。

*没有A)用相同的caching文件的多个版本快速填满磁盘,B)不会太慢地减缓请求

如果这些文件在远程服务器上没有改变, proxy_store可以为你工作。 例如

  upstream big { server serverfault.com:80; } root /somewhere; location / { try_files $uri @big; } location @big { proxy_pass http://big; proxy_set_header Host serverfault.com:80; proxy_store on; }