How to Configure Nginx FastCGI Cache for Better PHP Performance
Introduction
Nginx FastCGI Cache is one of the most effective ways to improve the performance of PHP websites. Instead of sending every request to PHP-FPM, Nginx can store generated pages in memory or on disk and serve them directly to future visitors.
For content that does not change frequently, FastCGI Cache can dramatically reduce PHP execution time, lower CPU usage, and improve response times.
This guide demonstrates how to configure FastCGI Cache on AlmaLinux 10 using Nginx and PHP-FPM.
Test Environment
How FastCGI Cache Works
Without FastCGI Cache:
Every request executes PHP.
With FastCGI Cache:
First Request
Later Requests
Subsequent requests are served directly by Nginx, eliminating the need to execute PHP for every page view.
Create the Cache Directory
Create a directory to store cached files.
Set the correct ownership.
Configure the Cache Zone
Edit the main Nginx configuration.
Inside the http block, add:
Configuration Explained
keys_zone
keys_zone=PHPCACHE:100m
Stores cache metadata in shared memory.
A 100 MB zone is sufficient for many websites.
inactive
inactive=60m
Cached files that are not accessed for 60 minutes are automatically removed.
max_size
max_size=5g
Limits the total disk space used by cached files.
fastcgi_cache_key
fastcgi_cache_key "$scheme$request_method$host$request_uri";
Defines how Nginx uniquely identifies cached pages.
Enable FastCGI Cache
Inside your PHP location block, add:
Skip Cache for Logged-In Users
Dynamic pages should not be cached.
Example:
This prevents authenticated users and POST requests from receiving cached content.
Restart Nginx
After saving the configuration:
Expected output:
syntax is ok
test is successful
Restart Nginx.
Verify the Cache
Request the same page twice.
curl -I https://example.com/
Check:
X-FastCGI-Cache: MISS
The second request should display:
X-FastCGI-Cache: HIT
This confirms that the page is being served from the cache.
Common Issues
Cache Always Shows MISS
Possible causes include:
Cache directory permissions are incorrect.
fastcgi_cache is not enabled inside the PHP location block.
Every request is being bypassed by cache rules.
Logged-In Users Receive Cached Pages
Review the cache bypass conditions and ensure session cookies are excluded.
Configuration Test Fails
Always validate the configuration before restarting Nginx.
The error message usually identifies the exact configuration problem.
Best Practices
Cache anonymous visitors only.
Exclude login, checkout, and admin pages.
Use FastCGI Cache together with PHP OPcache.
Tune PHP-FPM to handle cache misses efficiently.
Monitor cache hit rates after deployment.
Conclusion
Nginx FastCGI Cache is one of the most effective optimizations for PHP applications. By serving cached responses directly from Nginx, you can significantly reduce PHP execution, lower CPU usage, and improve page load times.
When combined with PHP OPcache and a properly tuned PHP-FPM configuration, FastCGI Cache forms a powerful performance optimization stack for modern PHP websites.
Explore More
› PHP opcache.max_accelerated_files Explained: Configuration, Performance, and Best Practices
› PHP opcache.validate_timestamps Explained: Configuration, Code Updates, and Best Practices
› PHP opcache.revalidate_freq Explained: Configuration, Performance, and Best Practices
› PHP opcache.enable Explained: Configuration, Performance, and Best Practices