I heard recently that Nginx has added caching to its reverse proxy feature. I looked around but couldn't find much info about it.
I want to set up Nginx as a caching reverse proxy in front of Apache/Django: to have Nginx proxy requests for some (but not all) dynamic pages to Apache, then cache the generated pages and serve subsequent requests for those pages from cache.
Ideally I'd want to invalidate cache in 2 ways:
- Set an expiration date on the cached item
- To explicitly invalidate the cached item. E.g. if my Django backend has updated certain data, I'd want to tell Nginx to invalidate the cache of the affected pages
Is it possible to set Nginx to do that? How?
I don't think that there is a way to explicitly invalidate cached items, but here is an example of how to do the rest. Update: As mentioned by Piotr in another answer, there is a cache purge module that you can use. You can also force a refresh of a cached item using nginx's proxy_cache_bypass - see Cherian's answer for more information.
In this configuration, items that aren't cached will be retrieved from example.net and stored. The cached versions will be served up to future clients until they are no longer valid (60 minutes).
Your Cache-Control and Expires HTTP headers will be honored, so if you want to explicitly set an expiration date, you can do that by setting the correct headers in whatever you are proxying to.
There are lots of parameters that you can tune - see the nginx Proxy module documentation for more information about all of this including details on the meaning of the different settings/parameters: http://nginx.org/r/proxy_cache_path
You can specifically invalidate cached pages through
Say you want to cache a page, set cache this way
Now, when you want to invalidate that page and cache again
Do a secret curl call with the header
It will invalidate and cache it.
Works from nginx 0.7.
As an added bonus the
add_header X-Cache-Status
can be used to check if the page is from cache or not.I suggest you give Varnish a try. Varnish is specifically designed as a reverse proxy cache. It will honor all cache control headers you send from the origin server, which satisfies your first request.
For your second request, explicit invalidation. My strong recommendation is to change the name of the url of the resource you want to invalidate, either by renaming the file or using some form of query string cache buster. Varnish does have a
PURGE
operation that will remove the resource from Varnish's cache, but it will not give you control over any other caches between you and the user. As you've said you want to explicitly purge a resource, then standard http control headers won't help you. In that cases the most foolproof way to defeat the caching of a resource is to rename it.Most caching tools (Citrix) allow a force-refresh (Ctrl+r) to repopulate a cached page.
Here's a trick I found to do something similar in nginx.
This assumes that when you do a Ctrl+r in your browser, the Cache-Control header has max-age=0 in its request. I know Chrome does this, but I have not tried in other browsers. Adding more header fields can be easy, by just adding more if statements that set the
$eac
variable to 1.For invalidating selected pages you can use "cache_purge" patch for nginx-0.8.x which does exacly what you want ;)
It's available here.
Caching is pretty new function in nginx (and not so well documented for now), but stable enough to be used in production.
Use English documentation
Ask in Mailing lists
Translate Russian documentation with something like Google Translate
Use varnish
I believe NginxHttpProxyModule is capable of caheing http requests. Look for the directives starting with:
Yes, it is possible to control cache behaviour via directives like:
Based on the fact that you can't find docs on it, I'd be a little bit wary about relying on it in production. Have you considered Varnish? It's my "nginx of reverse proxies", small, lightweight, doing one job and doing it well.
If you use eTags on your application and put nginx in front of it then it will take care of the expiration for you, because if the eTag changes it will invalidate the cache.
You can control Nginx's cache expiration with multiple directives/parameters:
proxy_cache_valid 200 302 10m;
Expires
Cache-Control
X-Accel-Expires
the
inactive
parameter in theproxy_cache_path
directive:proxy_cache_path /data/nginx/cache keys_zone=one:10m inactive=60m;
I recommend my blog post if you want to learn more about Nginx caching.
The purging topic is really interesting since this feature exists only in Nginx Plus (Nginx's commercial edition). I really like @randy-wallace answer. But there are also other possibilities like the ngx_cache_purge module.
The simplest thing you can do is remove the cached file manually:
generate your hash key:
remove the file from the filesystem: