Complete the code to enable basic caching in nginx.
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m [1] inactive=60m;
The max_size directive sets the maximum size of the cache. Setting it to 1 gigabyte allows nginx to store cached files up to that size, improving response times by serving cached content.
Complete the code to enable caching for a proxy location in nginx.
location /api/ {
proxy_cache my_cache;
proxy_pass http://backend_server;
proxy_cache_valid 200 [1];
}The proxy_cache_valid directive sets how long a successful response (status 200) is cached. Setting it to 1 hour means nginx will serve cached content for one hour, improving response times.
Fix the error in the caching configuration to properly enable caching.
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m; location /static/ { proxy_pass http://backend; proxy_cache [1]; }
The proxy_cache directive must match the name of the cache zone defined in keys_zone. Here, it is my_cache. Using a different name causes caching not to work.
Fill both blanks to configure nginx to cache only GET requests and bypass caching for POST requests.
proxy_cache_methods [1]; proxy_cache_bypass [2];
proxy_cache_methods GET; tells nginx to cache only GET requests. proxy_cache_bypass POST; tells nginx to bypass cache for POST requests, ensuring dynamic data is not cached.
Fill both blanks to create a cache key that includes the host, URI, and query string.
proxy_cache_key [1]$host:$request_uri[2]$query_string;
The cache key is a string combining host, URI, and query string. Quotes " wrap the key. Colons : and underscores _ separate parts for clarity and uniqueness.