Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx proxy_cache caches 502 errors

Tags:

caching

nginx

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?

like image 218
Jake Avatar asked Mar 11 '15 18:03

Jake


People also ask

What is Nginx Proxy_cache?

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.

Does nginx support caching?

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.

How do I enable caching in nginx?

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.


1 Answers

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.

like image 58
Jake Avatar answered Nov 15 '22 12:11

Jake