1

I have been noticing some 406 statuses being cached in my Nginx proxy instance, and I want to be sure that this does not happen again.

I discovered one possible cause is that somebody had set a proxy_cache_valid any for several minutes, which had to be removed. My current understanding is that by default, nothing would be cached, and then if there is a proxy_cache_valid, that is used, and then if there is any cache-control or expire headers, they will override that setting.

Therefore, if I am understanding it correctly, I would expect that errors by default wouldn't be cached, but that if an HTTP header overrides it, then it can be cached. One solution would be to generally ignore those HTTP headers. So, the first question is whether I am correct in this understanding.

Assuming that all of this is true, in my situation, I want to keep the cache-control headers on this server, so I don't want to just ignore them. However, I am concerned that the upstream might set them even on error statuses. I am looking for a solution that will let me be certain that those statuses, like 500, 503, etc., are never cached.

Here's what I am thinking for the current solution:

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;

    ...
}

Are these reasonable settings, and will it do what I expect? Is there a better way to accomplish this? I was not able to find much documentation on doing this, which makes me think that I'm missing something pretty big.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.