Complete the code to enable micro-caching for 1 second in nginx.
proxy_cache_path /tmp/cache keys_zone=microcache:10m max_size=100m inactive=60m; server { location / { proxy_cache microcache; proxy_cache_valid 200 [1]s; proxy_pass http://backend; } }
The proxy_cache_valid directive sets how long responses are cached. For micro-caching, 1 second is typical to reduce load but keep content fresh.
Complete the code to bypass cache for logged-in users based on a cookie.
server {
location / {
proxy_cache microcache;
proxy_cache_bypass [1];
proxy_pass http://backend;
}
}The proxy_cache_bypass directive can use a condition. Checking if the logged_in cookie exists in $http_cookie disables cache for logged-in users.
Fix the error in the micro-cache key definition to include the request URI.
proxy_cache_key [1];The cache key should uniquely identify requests. Combining $scheme, $host, and $request_uri ensures different URLs and protocols are cached separately.
Fill both blanks to set cache control headers and enable micro-caching for 2 seconds.
server {
location / {
add_header Cache-Control "public, max-age=[1]";
proxy_cache_valid 200 [2]s;
proxy_cache microcache;
proxy_pass http://backend;
}
}Setting Cache-Control max-age to 2 seconds matches the micro-cache duration set by proxy_cache_valid for 200 responses.
Fill all three blanks to create a micro-cache zone, set cache key, and bypass cache for POST requests.
proxy_cache_path /tmp/microcache keys_zone=[1]:10m max_size=50m inactive=1h; server { location /api/ { proxy_cache [2]; proxy_cache_key [3]; proxy_cache_bypass $request_method = POST; proxy_pass http://backend_api; } }
The cache zone name must be consistent in proxy_cache_path and proxy_cache. The cache key should uniquely identify requests using $scheme$host$request_uri. POST requests bypass cache.