我怎样才能确保Nginx永远不会caching某些错误状态?

我一直在注意到一些406状态被caching在我的Nginx代理实例中,我想确定这不会再发生。

我发现一个可能的原因是有人已经设置了一个proxy_cache_valid any几分钟,必须被删除。 我目前的理解是,默认情况下,什么都不会被caching,然后如果有一个proxy_cache_valid ,那么使用,如果有任何caching控制或过期头,他们将覆盖该设置。

因此,如果我正确地理解它,我预计错误默认情况下不会被caching,但是如果一个HTTP头覆盖它,那么它可以被caching。 一种解决scheme是通常忽略这些HTTP标头。 所以,第一个问题是我在这个理解中是否正确。

假设所有这一切都是真实的,在我的情况下,我想保持这个服务器上的caching控制标题,所以我不想忽略它们。 但是,我担心上游可能会设置错误状态。 我正在寻找一个解决scheme,让我可以肯定,这些状态,如500,503等,从未被caching。

以下是我正在考虑的当前解决scheme:

 http { map $upstream_status $never_cache { 200 0; # Want to receive from cache as in proxy_cache_valid 301 0; # Want to receive from cache as in proxy_cache_valid 404 0; # Want to receive from cache as in proxy_cache_valid 500 0; # Want stale content from proxy_cache_use_stale 502 0; # Want stale content from proxy_cache_use_stale 503 0; # Want stale content from proxy_cache_use_stale 504 0; # Want stale content from proxy_cache_use_stale default 1; # Everything else should never be cached } proxy_cache_valid 200 301 404 10m; #proxy_cache_valid any 0s # No active line for this as it's default proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; proxy_no_cache $never_cache; ... } 

这些合理的设置,它会做我所期望的吗? 有没有更好的方法来完成这个? 我无法find很多关于这样做的文档,这让我觉得我错过了很大的东西。