Chapter 5
[ 105 ]
NGINX is also capable of caching the response from the upstream server, so that
the same request asked again doesn't have to go back to the upstream server to
be served. The preceding figure illustrates this as follows:
- 1a: A client makes a request
- 1b: The request's cache key is not currently found in the cache, so NGINX
requests it from the upstream server - 1c: The upstream responds and NGINX places the response corresponding
to that request's cache key into the cache - 1d: The response is delivered to the client
- 2a: Another client makes a request that has a matching cache key
- 2b: NGINX is able to serve the response directly from the cache without
needing to first get the response from the upstream server
Table: Proxy module caching directives
Directive Explanation
proxy_cache Defines a shared memory zone to be used
for caching.
proxy_cache_bypass One or more string variables, which when
non-empty or non-zero, will cause the
response to be taken from the upstream
server instead of the cache.
proxy_cache_key A string used as the key for storing and
retrieving cache values. Variables may be
used, but care should be taken to avoid
caching multiple copies of the same content.
proxy_cache_lock Enabling this directive will prevent multiple
requests to the upstream server(s) during
a cache miss. The requests will wait for the
first to return and make an entry into the
cache key. This lock is per worker.
proxy_cache_lock_timeout The length of time a request will wait for
an entry to appear in the cache or for the
proxy_cache_lock to be released.
proxy_cache_min_uses The number of requests for a certain key
needed before a response is cached.