FastCGI cache in Nginx - Commands & Configuration
http {
fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2 keys_zone=MYCACHE:10m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
server {
listen 80;
server_name example.com;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_cache MYCACHE;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 404 1m;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_min_uses 1;
add_header X-Cache $upstream_cache_status;
}
}
}
fastcgi_cache_path sets where cache files are stored and how much memory is used for keys.
fastcgi_cache_key defines how requests are identified uniquely.
Inside server, the location ~ \.php$ block configures FastCGI caching for PHP files.
fastcgi_cache enables caching using the defined zone.
fastcgi_cache_valid sets how long responses with certain status codes are cached.
fastcgi_cache_use_stale allows serving stale cache if backend errors occur.
add_header X-Cache adds a header to show cache status in responses.
sudo nginx -t
sudo systemctl reload nginxcurl -I http://example.com/index.phpcurl -I http://example.com/index.phpIf you remember nothing else from this pattern, remember: FastCGI cache stores dynamic responses on disk so nginx can serve them quickly without running the backend app again.
Practice
Solution
Step 1: Understand FastCGI cache function
FastCGI cache stores dynamic content generated by backend servers temporarily.Step 2: Identify the benefit
Serving cached content reduces backend load and speeds up response time.Final Answer:
To store dynamic page responses and serve them faster next time -> Option AQuick Check:
FastCGI cache purpose = store and speed up [OK]
- Thinking it blocks dynamic content
- Confusing cache with logging
- Assuming it disables backend servers
Solution
Step 1: Recall fastcgi_cache_path syntax
The correct syntax includes path, levels, keys_zone with size, and inactive time.Step 2: Match options with syntax
fastcgi_cache_path /path/to/cache levels=1:2 keys_zone=mycache:10m inactive=60m; matches the correct order and parameters exactly.Final Answer:
fastcgi_cache_path /path/to/cache levels=1:2 keys_zone=mycache:10m inactive=60m; -> Option DQuick Check:
Correct fastcgi_cache_path syntax = fastcgi_cache_path /path/to/cache levels=1:2 keys_zone=mycache:10m inactive=60m; [OK]
- Mixing keys_zone and path order
- Omitting levels parameter
- Using fastcgi_cache instead of fastcgi_cache_path
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=cache_zone:10m inactive=30m;
server {
location ~ \.php$ {
fastcgi_cache cache_zone;
fastcgi_cache_valid 200 10m;
fastcgi_pass backend;
}
}
What happens when a PHP page is requested twice within 10 minutes?
Solution
Step 1: Understand fastcgi_cache_valid 200 10m
This means HTTP 200 responses are cached for 10 minutes.Step 2: Analyze repeated request behavior
The first request caches the response; the second request within 10 minutes uses cache.Final Answer:
The second request is served from cache, speeding up response -> Option CQuick Check:
Cache valid time means reuse cached response [OK]
- Assuming backend always handles requests
- Thinking cache clears after first request
- Confusing cache duration with cache clearing
Solution
Step 1: Identify required directives for caching
fastcgi_cache_path defines cache storage; fastcgi_cache_valid sets cache time; fastcgi_pass sends requests to backend.Step 2: Understand fastcgi_cache_key role
fastcgi_cache_key defines how requests are identified for caching; missing it disables caching.Final Answer:
fastcgi_cache_key -> Option BQuick Check:
Missing cache key disables caching [OK]
- Forgetting to set fastcgi_cache_key
- Confusing fastcgi_cache_valid with cache enabling
- Assuming fastcgi_pass controls caching
user_session. Which configuration snippet correctly disables cache for logged-in users?Solution
Step 1: Understand directives controlling cache bypass
fastcgi_no_cache disables caching when its variable is true; fastcgi_cache_bypass skips cache lookup but may still cache response.Step 2: Identify correct directive for excluding logged-in users
fastcgi_no_cache $cookie_user_session; disables caching for requests with that cookie, preventing cached content for logged-in users.Final Answer:
fastcgi_no_cache $cookie_user_session; -> Option AQuick Check:
fastcgi_no_cache disables caching for matching requests [OK]
- Using fastcgi_cache_bypass alone to prevent caching
- Trying to conditionally set fastcgi_cache_valid
- Misusing fastcgi_cache_key for cache control
Ready to prove it?
Pass the test and this pattern moves unknown
