Set Up Nginx FastCGI Cache on Ubuntu 20.04 LTS
Want a snappy site with fewer hits to your PHP backend?
This guide will walk you through turning Nginx’s FastCGI cache into a lean, mean speed machine on Ubuntu 20.04.
1. Why the Cache Matters
FastCGI cache stores rendered PHP pages so that subsequent requests hit the disk instead of re‑running scripts. If your site is mostly static or has heavy database queries, it can cut response times by half—no more waiting for `php-fpm` to crunch every request.
2. Prepare the Environment
sudo apt update && sudo apt upgrade -y sudo apt install nginx php-fpm
The upgrade ensures you’re on Nginx 1.18 and PHP‑7.4, both stable for FastCGI.
3. Create a Cache Directory
sudo mkdir /var/cache/nginx/fastcgi sudo chown -R www-data:www-data /var/cache/nginx/fastcgi
Setting the owner to `www-data` lets Nginx write there without root, keeping permissions tidy.
4. Tell PHP‑FPM Where to Store Its Temporary Files
Open `/etc/php/7.4/fpm/pool.d/www.conf` and add:
; FastCGI cache path for php-fpm fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=mycache:10m inactive=60m use_temp_path=off;
If you skip `use_temp_path=off`, PHP will write to a temp folder first, which adds latency.
5. Configure Nginx Server Block
Edit `/etc/nginx/sites-available/default` (or your own block) and add within the `server {}`:
# Enable FastCGI cache for this server
fastcgi_cache mycache;
fastcgi_cache_valid 200 1h; # Cache successful responses for an hour
fastcgi_cache_use_stale error timeout invalid_header http_500; # Serve stale if backend fails
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# Keys based on the full request URI
fastcgi_cache_key "$scheme$request_method$host$request_uri";
# Invalidate cache when a PHP file changes (optional)
try_files $uri =404;
}
The `fastcgi_cache_use_stale` line keeps your users happy even if the backend hiccups.
6. Prevent Cache for Logged‑In Users
If you run WordPress or another CMS, you don’t want logged‑in folks seeing stale content. Add inside the PHP block:
# Skip cache for admin sessions
set $skip_cache 0;
if ($http_cookie ~* "wordpress_logged_in") {
set $skip_cache 1;
}
fastcgi_no_cache $skip_cache;
fastcgi_cache_bypass $skip_cache;
Without this, an admin could see outdated pages until the stale period expires.
7. Test the Configuration
sudo nginx -t
If it says “syntax is ok”, reload:
sudo systemctl reload nginx
8. Verify It’s Working
1. Cache Miss – Open a browser tab to your PHP page, then run:
sudo tail -f /var/log/nginx/access.log | grep "200"
2. Reload the same page. You should see the same `200` line without an extra request to PHP‑FPM in `/var/log/php7.4-fpm.log`.
If you still spot a hit on PHP‑FPM after a reload, double‑check your cache key or that the file permissions allow Nginx to write into `/var/cache/nginx/fastcgi`.
9. Common Pitfalls
- Wrong Cache Path – If you misspell the directory in `fastcgi_cache_path`, Nginx silently ignores it and behaves as if caching is off.
- Too Small Keys Zone – A 10 MiB zone can fill up quickly on a busy site; bump to 50 MiB if needed.
- Not Using `use_temp_path=off` – Creates an extra write round‑trip that negates the speed benefit.
10. Clean Up Old Cache Files
Periodically prune stale cache entries:
sudo find /var/cache/nginx/fastcgi -type f -mtime +7 -delete
Keep the cache lean and avoid disk bloat.
That’s it—FastCGI caching is up, running, and ready to shave milliseconds off each request. Feel free to tweak `fastcgi_cache_valid` or add more sophisticated cache‑bypass logic as your traffic grows.