nginx prevent cache for proxy/fast_cgi on response 5xx
Solution 1:
Well, it is strange enough, that your backend sends Cache-Control
header for 4xx/5xx errors.
Nginx can be configured via proxy_cache_valid
directive OR it can take cache-settings from Cache-Control
/Expires
/... headers. This options can be mixed, but not in your situation.
So, you have two options:
- Ask your developers not to send
Cache-Control
headers when it is not needed (i.e. for 4xx/5xx codes) - Configure Nginx to disable processing of
Expires
/Cache-Control
backend response headers with proxy_ignore_headers.
Solution 2:
By default, nginx will honor the Cache-Control
received from the backend for its own proxy cache, see e.g. https://forum.nginx.org/read.php?2,268813,268814
So it seems that the best solution would be to change your backend to stop emitting these headers in case of errors.
If you really want to override those 500s in nginx, maybe use proxy_cache_valid with 500 and 0 as the parameters? It definitely means second-guessing the backend, so it could have unintended consequences either way.