FastCGI is deadly fast and more than 60% of the websites use this to sky rocket the performance of their websites. With this tutorial you will learn how you can Enable and Configure FastCGI Cache in Nginx for WordPress.
Nginx comes preinstalled with FastCGI caching. FastCGI caching is used to cache the dynamic content that is produced by PHP. FastCGI cache fulfil the need of page caching too. That means you don’t have to configure page cache if you are using FastCGI cache. FastCGI is highly customizable and thus you can choose what to include and what to exclude from caching.
Mistakes to avoid
People usually makes a mistake that they install PHP only and try to configure FastCGI but since FastCGI works with PHP-FPM, you must install PHP-FPM as well on your server. If you haven’t installed PHP-FPM, you can do so easily with following commands
# sudo apt install php8.1-fpm -y
NOTE
Change the version number of PHP as per the version you have installed on your server
So, let’s get going.

Prerequisite
To implement FastCGI cache we need a user with sudo privilege LEMP, and WordPress configured. If not, then here is the initial Ubuntu server configuration that you can follow.
Table of Contents
How to Configure fastcgi in nginx with php fpm
Step 1 – Edit nginx.conf
The first step is to setup our nginx.conf file which is located at /etc/nginx/nginx.conf location.
# sudo nano /etc/nginx/nginx.conf
Copy and paste the below code under http { block
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache FASTCGICACHE;
fastcgi_cache_valid 60m;
add_header X-FastCGI-Cache $upstream_cache_status;
Save by pressing “Ctrl+x” and then “y” and press enter
Step 2 – Configure your Nginx and enable FastCGI cache
Second step is to configure the Nginx configuration file which is located at /etc/nginx/sites-available/yourdomain that you are using for your website
# sudo nano /etc/nginx/sites-available/yourdomain
Copy and paste the below code before server { block
fastcgi_cache_path /var/run/nginx-fastcgi-cache levels=1:2 keys_zone=FASTCGICACHE:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
Save by pressing “Ctrl+x” and then “y” and press enter
Step 3 – Exclude some content from caching
There is some content that you never want to be cached just because it is dynamic for example WordPress admin page, sitemap, and feeds. To exclude them from caching, follow the steps given below
# sudo nano /etc/nginx/sites-available/yourdomain
Copy and paste the below code inside the location ~ .php$ block
set $skip_cache 0;
#POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
#Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
#Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
Save by pressing “Ctrl+x” and then “y” and press enter
Step 4 – Restart Nginx
Before restarting Nginx, always make sure to compile it for errors by the following command
# sudo nginx -t
If it is successful you will get the following message
"nginx: the configuration file /etc/nginx/nginx.conf syntax is ok"
"nginx: configuration file /etc/nginx/nginx.conf test is successful"
Then run the following command to restart Nginx
# sudo service nginx restart
Step 5 – Install and configure WordPress plugin
The last step to complete the configuration is to install the plugin in WordPress and configure it.
- search for “Nginx Cache” in WordPress and click on “Install“. Then click on “Activate“.
- Now go to “Tools” and click on “Nginx Cache“.
- Under “Cache Zone Path” copy and paste “/var/run/nginx-fastcgi-cache“
- Click on “Save Changes“
Thats it, you have successfully configured and enabled the FastCGI cache.
Conclusion
FastCGI is very fast if you have configured it properly. Make sure to install PHP-FPM before performing these steps. If you want you can use page caching as well along with FastCGI cache, but everything depends upon your setup.
Leave a Reply