Sub topic 13
Caching upstream responses
Assume you know Nginx server and location blocks, HTTP methods and status codes, and how to validate and reload an Nginx service. This chapter adds caching to an existing reverse proxy that uses an upstream group such as app_backend.
How Nginx proxy caching works
A proxy cache stores selected upstream responses on disk. When a later request has the same cache key, Nginx can return the stored response without contacting the application.
The normal flow is:
Nginx uses two related settings:
proxy_cache_pathcreates a cache zone and describes where response files are stored. It belongs in thehttpcontext.proxy_cacheenables a named zone inside aserverorlocationblock.
The keys_zone size describes in-memory metadata, not the maximum response size. max_size limits disk usage. inactive removes entries that have not been requested during the specified period.
Configure a basic cache
This example caches successful responses for ten minutes and adds a response header that exposes the cache result.
# Place this directive inside the http { } context.
proxy_cache_path /var/cache/nginx/app
levels=1:2
keys_zone=app_cache:10m
max_size=1g
inactive=60m
use_temp_path=off;
server {
listen 80;
server_name example.com;
location / {
proxy_cache app_cache;
proxy_cache_methods GET HEAD;
proxy_cache_valid 200 301 10m;
add_header X-Cache-Status $upstream_cache_status always;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://app_backend;
}
}The X-Cache-Status value usually reports MISS, HIT, BYPASS, EXPIRED, or STALE. A first request normally produces MISS. A repeated request can produce HIT until the entry expires or is removed.
The cache key determines which requests share a response. By default, Nginx includes the scheme, request method, host and URI. Query strings are part of $request_uri, so /items?page=1 and /items?page=2 normally have different entries.
Avoid caching private responses
Caching is safe only when the response is suitable for multiple clients. Public documentation and static API responses are common candidates. Account pages, shopping carts and responses containing personal data usually are not.
Use both bypass and no-cache rules when authentication or a session cookie is present:
location / {
proxy_cache app_cache;
proxy_cache_methods GET HEAD;
proxy_cache_valid 200 301 10m;
proxy_cache_bypass $http_authorization $cookie_sessionid;
proxy_no_cache $http_authorization $cookie_sessionid;
add_header X-Cache-Status $upstream_cache_status always;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://app_backend;
}proxy_cache_bypass prevents Nginx from serving an existing cached response for a matching request. proxy_no_cache prevents Nginx from storing the response. Using only one can leave a gap in the protection.
Validate and observe the cache
Ensure the cache directory exists and is writable by the Nginx worker user. Then test and reload safely:
sudo nginx -t && sudo systemctl reload nginx
curl -i http://example.com/public-data
curl -i http://example.com/public-dataCompare X-Cache-Status in both responses. If both requests are MISS, inspect the response headers, cache rules and Nginx error log. An upstream Set-Cookie, an authorization header or a Cache-Control: private response can prevent storage.
Exercise
Why should a site use both proxycachebypass and proxynocache for requests with a session cookie?
Show answer
proxycachebypass prevents serving an existing cached response, while proxynocache prevents storing the new private response.