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.
So, let’s get going.
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.
Step 1 – Edit nginx.conf
# 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
# sudo nano /etc/nginx/conf.d/yourdomain.conf
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/conf.d/yourdomain.conf
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.