Need some collective brainstorming :). There is a heavy PHP application (e.g. Magento) and each time a visitor hits a non-existent page (or there is a problem with a particular part of the application and it returns errors) the load on the application server is snowballing:
in case of non-existent pages they are not cached and the application spends quite a lot of resources to check everything and generate a cached copy, which will be a waste of time;
in case of a problem with a part of the application, the application will spend valuable time to generate these error responses in expense of the visitors of healthy parts of the site.
The idea here is to cache all negative responses like 404 and 5xx coming from the FastCGI backend on the nginx frontend for, say, 5 minutes - this will significantly lower the performance impact on the application servers.
The caching can be quite easily implemented using fastcgi_cache_* directives. However, once you define fastcgi_cache_* directives in the location block where fastcgi_pass is located it will try to cache everything going through that location block.
Therefore, the question is how to limit fastcgi_cache to, say, 404 responses from the backend scripts only?
Just to illustrate the scenario a bit more. Imagine that a request goes to /app/missing, the application has it's only entry point in /index.php, so nginx will pass a request to the backend FastCGI server calling /index.php/app/missing. Now, since /app/missing does not exist index.php will return HTTP 404. This route will crunch CPU as many times as you request /app/missing. What is desired is that once HTTP 404 was returned back from index.php the /app/missing is cached with the resulting 404 page from index.php on nginx, so if somebody requests /app/missing right away no call is performed to the PHP backend but a cached page is returned.