proxy_cache_use_stale
The proxy_cache_use_stale directive controls when to serve stale cached responses to clients. — NGINX HTTP Core
Описание
The proxy_cache_use_stale directive allows NGINX to serve cached responses that may be stale based on specific conditions, instead of fetching fresh content from the backend server. This is particularly useful to improve response times and reduce load on the backend during high latency or downtime scenarios. The directive can be set to multiple parameters, which dictate under what conditions stale cached data should be used. For example, if set to 'error', NGINX will serve stale content if the backend server returns any error status. Similarly, 'timeout', 'updating', and 'http_500' can also be specified as conditions for serving stale data. Multiple conditions can be used in a single directive, separated by spaces. When the directive is enabled with specific parameters, if NGINX encounters a situation that meets one of the criteria while trying to obtain fresh content (e.g., a timeout or server error), it will automatically serve the last cached response that corresponds to that request instead of returning an error to the client. This ensures better resilience and user experience, particularly for frequently accessed resources where the cached data may still be relevant even if it's slightly out of date.
Пример конфига
location /api {
proxy_pass http://backend;
proxy_cache my_cache;
proxy_cache_use_stale error timeout;
}Using this directive may lead to serving outdated content inadvertently if not configured properly accordingly to the application requirements.
Overusing this directive can mask backend issues without resolving underlying problems, leading to a poor user experience.