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 rel="noreferrer">cache purge module that you can use. You can also force a
refresh of a cached item using nginx's proxy_cache_bypass - see href="https://serverfault.com/a/334924/15314">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:
href="http://nginx.org/r/proxy_cache_path"
rel="noreferrer">http://nginx.org/r/proxy_cache_path
http
{
proxy_cache_path /var/www/cache levels=1:2 keys_zone=my-cache:8m
max_size=1000m inactive=600m;
proxy_temp_path /var/www/cache/tmp;
server {
location / {
proxy_pass
http://example.net;
proxy_cache my-cache;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;
}
}
}
Comments
Post a Comment