Chapter 5
[ 107 ]
The following configuration is designed to cache all responses for six hours, up to
a total cache size of 1 GB. Any items that stay fresh, that is, are called within the six
hour timeout, are valid for up to one day. After this time, the upstream server will
be called again to provide the response. If the upstream isn't able to respond due to
an error, timeout, invalid header, or if the cached item is being updated, a stale cache
element may be used. The shared memory zone, CACHE, is defined to be 10 MB
large and is referenced within the location where the cache keys need to be set
and looked-up.
http {
# we set this to be on the same filesystem as proxy_cache_path
proxy_temp_path /var/spool/nginx;
# good security practice dictates that this directory is owned by
the
# same user as the user directive (under which the workers run)
proxy_cache_path /var/spool/nginx keys_zone=CACHE:10m levels=1:2
inactive=6h max_size=1g;
server {
location / {
# using include to bring in a file with commonly-used
settings
include proxy.conf;
# referencing the shared memory zone defined above
proxy_cache CACHE;
proxy_cache_valid any 1d;
proxy_cache_use_stale error timeout invalid_header
updating http_500 http_502 http_503 http_504;
proxy_pass http://upstream;
}
}
}