Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure NginX to serve Cached Content only when Backend is down (5xx Resp. Codes)?

I've configured my system with NginX listening on port 80, serving static content and proxying dynamic requests to a backend server.

I can configure NginX to cache content generated by the backend, but I want this cached content be served only when the Backend responds with an error http 5xx, or when it's totally down.

We tried the proxy_cache_use_stale option with max-age of 1 second, it worked but it has one negative side.. which is simply dozens of requests being served from cache during this 1 second cache-aged-content. These requests served from cache will miss further Backend processing (Stats for example).

We can only afford to live with this negativity IF the backend was down, Thus, the cache will act as a backup or a failover solution. But as long as the backend is up and responding, no requests should be served from cache.

I would appreciate any hints

like image 789
Hasan Alsawadi Avatar asked May 26 '13 04:05

Hasan Alsawadi


People also ask

How do I set up Nginx cache?

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.

Can Nginx cache dynamic content?

Cache both static and dynamic content from your proxied web and application servers, to speed delivery to clients and reduce the load on the servers.

Does Nginx proxy cache?

Nginx caches the responses in the disk, proxy_cache_path specifies the path where the responses are to be stored. proxy_cache defines the shared memory zone used for storing the cache keys and other metadata. proxy_cache_key defines the caching key.


1 Answers

Take a look at proxy_cache_use_stale

like image 165
user1600649 Avatar answered Oct 24 '22 22:10

user1600649