Is there a way to cleverly cache with Nginx?

Alex asked:

We have an Apache behind Nginx. Nginx is used to load balance between webservers and to cache static content. The only problem is, every time the web application is updated we have to completely clear Nginx cache (static file cache is set to quite a high value, some static files change often others not).

Is there a clever way to let Nginx periodically check that the file has been modified (note: Apache returns “Date” in response headers) since the time file had been cached by Nginx.

My answer:

You’ll have to write a script that periodically crawls your internal content that you want cached, and have the script crawl each URL that you update.

In the script, set an HTTP header (the actual header doesn’t matter) and then use proxy_cache_bypass to force nginx to retrieve it from upstream.

Example: Your cache priming script sets the HTTP header X-Really-Get-It: true. In nginx.conf you will set:

proxy_cache_bypass $http_x_really_get_it;

nginx will fetch the file from upstream instead of from cache, and then cache the result.

If you are willing and able to use third party nginx modules, you can also use the cache_purge module. It seems rather poorly documented, though.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.