FastCGI cache is a feature in nginx that stores dynamic content generated by FastCGI servers (like PHP-FPM) on disk. This helps serve repeated requests faster by avoiding running the backend script every time.
Click to reveal answer
beginner
Which directive in nginx config enables FastCGI caching?
The directive fastcgi_cache enables FastCGI caching by specifying the cache zone name to use for storing cached responses.
Click to reveal answer
intermediate
What does the fastcgi_cache_path directive do?
It defines the location on disk where cached files are stored, the cache size, and cache key zone name. It sets up the cache storage parameters.
Click to reveal answer
intermediate
How does nginx decide if a cached FastCGI response is valid?
Nginx uses cache control headers like Cache-Control and Expires from the backend response, and directives like fastcgi_cache_valid to determine cache validity duration.
Click to reveal answer
intermediate
What is the purpose of fastcgi_cache_key?
It defines the unique key used to store and retrieve cached content. Usually, it includes the request URI and other variables to differentiate cache entries.
Click to reveal answer
Which directive sets the disk location for FastCGI cache in nginx?
Afastcgi_cache_path
Bfastcgi_cache_key
Cfastcgi_cache_valid
Dfastcgi_cache_use_stale
✗ Incorrect
The fastcgi_cache_path directive defines where cached files are stored on disk.
What does fastcgi_cache_valid 200 10m; mean?
ACache all responses for 200 minutes
BCache 200 OK responses for 10 minutes
CCache only 10 responses with status 200
DDisable caching for 200 status
✗ Incorrect
It tells nginx to cache responses with HTTP status 200 for 10 minutes.
How does nginx serve a cached FastCGI response?
ABy reading the cached file from disk and sending it directly
BBy running the backend script again
CBy redirecting to the backend server
DBy clearing the cache first
✗ Incorrect
Nginx serves cached content directly from disk without contacting the backend.
Which directive defines the cache zone name used in fastcgi_cache?
Afastcgi_cache_valid
Bfastcgi_cache_key
Cfastcgi_cache_path
Dfastcgi_cache_methods
✗ Incorrect
The fastcgi_cache_path directive defines the cache zone name used later by fastcgi_cache.
What is the effect of fastcgi_cache_use_stale error timeout?
ACache only error responses
BAlways clear cache on error
CDisable cache on timeout
DServe stale cache if backend returns error or times out
✗ Incorrect
This directive allows nginx to serve stale cached content if the backend is down or slow.
Explain how to configure FastCGI cache in nginx from scratch.
Think about where to store cache, how to name it, and how to use it in your server block.
You got /5 concepts.
Describe how nginx decides when to serve cached FastCGI content versus fetching fresh content.
Consider cache expiration and backend response status.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of enabling FastCGI cache in nginx?
easy
A. To store dynamic page responses and serve them faster next time
B. To block all dynamic content from being served
C. To increase the size of the nginx log files
D. To disable backend server processing completely
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 A
Quick Check:
FastCGI cache purpose = store and speed up [OK]
Hint: FastCGI cache speeds up dynamic pages by storing them [OK]
Common Mistakes:
Thinking it blocks dynamic content
Confusing cache with logging
Assuming it disables backend servers
2. Which of the following is the correct syntax to define a FastCGI cache zone in nginx?
easy
A. fastcgi_cache /path/to/cache keys_zone=mycache inactive=60m;
B. fastcgi_cache_zone /path/to/cache keys_zone=mycache:10m inactive=60m;
C. fastcgi_cache_path keys_zone=mycache:10m /path/to/cache inactive=60m;
D. fastcgi_cache_path /path/to/cache levels=1:2 keys_zone=mycache:10m inactive=60m;
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 D
fastcgi_cache_key defines how requests are identified for caching; missing it disables caching.
Final Answer:
fastcgi_cache_key -> Option B
Quick Check:
Missing cache key disables caching [OK]
Hint: Without fastcgi_cache_key, nginx can't store cache properly [OK]
Common Mistakes:
Forgetting to set fastcgi_cache_key
Confusing fastcgi_cache_valid with cache enabling
Assuming fastcgi_pass controls caching
5. You want to cache PHP responses but exclude logged-in users identified by a cookie user_session. Which configuration snippet correctly disables cache for logged-in users?
hard
A. fastcgi_no_cache $cookie_user_session;
B. fastcgi_cache_valid 200 10m if_not $cookie_user_session;