How to Configure Nginx FastCGI Cache for Better PHP Performance

Published: 2026-07-05
Google AdSense 广告位 (待申请通过后开启)

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

Operating System : AlmaLinux 10
Web Server       : Nginx
PHP Version      : PHP 8.3
PHP-FPM          : Enabled
Architecture     : x86_64

How FastCGI Cache Works

Without FastCGI Cache:

Browser
    │
    ▼
Nginx
    │
    ▼
PHP-FPM
    │
    ▼
PHP Application
    │
    ▼
Response

Every request executes PHP.

With FastCGI Cache:

First Request

Browser
    │
    ▼
Nginx
    │
    ▼
PHP-FPM
    │
    ▼
Cache Saved

Later Requests

Browser
    │
    ▼
Nginx
    │
    ▼
Cached Response

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.

sudo mkdir -p /var/cache/nginx/fastcgi

Set the correct ownership.

sudo chown -R nginx:nginx /var/cache/nginx

Configure the Cache Zone

Edit the main Nginx configuration.

sudo nano /etc/nginx/nginx.conf

Inside the http block, add:

fastcgi_cache_path /var/cache/nginx/fastcgi
levels=1:2
keys_zone=PHPCACHE:100m
inactive=60m
max_size=5g;

fastcgi_cache_key "$scheme$request_method$host$request_uri";

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:

fastcgi_cache PHPCACHE;

fastcgi_cache_valid 200 301 302 30m;

fastcgi_cache_valid 404 1m;

fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503;

fastcgi_cache_lock on;

add_header X-FastCGI-Cache $upstream_cache_status;

Skip Cache for Logged-In Users

Dynamic pages should not be cached.

Example:

set $skip_cache 0;

if ($request_method = POST) {
    set $skip_cache 1;
}

if ($query_string != "") {
    set $skip_cache 1;
}

if ($http_cookie ~* "PHPSESSID|wordpress_logged_in") {
    set $skip_cache 1;
}

fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;

This prevents authenticated users and POST requests from receiving cached content.

Restart Nginx

After saving the configuration:

sudo nginx -t

Expected output:

syntax is ok

test is successful

Restart Nginx.

sudo systemctl 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.

sudo nginx -t

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

Technology Guides →

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

Southeast Asia Insights →