I'm using Nginx proxy_cache to cache responses from a Node server. When that server is offline Nginx returns a custom 502 page. All going well so far...
When the Node server comes back online Nginx keeps returning the 502 page for 5 mins (my cache time). If I delete all the files in the cache dir it makes no difference. If I restart Nginx it busts the cache and starts serving real content again.
Is it normal for Nginx to cache 502s? Note that it's not caching a 502 response from the backend server it's caching the fact that the server isn't accessible.
What can I do to stop this?
NGINX Processes Involved in Caching The cache manager is activated periodically to check the state of the cache. If the cache size exceeds the limit set by the max_size parameter to the proxy_cache_path directive, the cache manager removes the data that was accessed least recently.
By default, NGINX Plus and NGINX serve cached content for as long as it is valid. Validity is configurable or can be controlled by the Cache-Control header set by the origin server.
Go to the “Web Server” tab. In the “nginx settings” section, select the “Enable nginx caching” checkbox. (Optional) You can customize nginx caching settings. If you are not familiar with nginx caching, we recommend that you keep the default settings.
I managed to figure this out myself. Nginx really does cache the knowledge that the upstream server is inaccessible.
To fix this I changed my cache config from proxy_cache_valid any 5m;
to proxy_cache_valid 5m;
Removing any
implies you only want to cache 200, 301, and 302 responses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With