• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Swift Guides

Howtos, Tutorials & Guides

  • Home
  • Servers
  • Applications
  • Opinions
  • Search
You are here: Home / Servers / Enable and Configure FastCGI Cache in Nginx for WordPress

Enable and Configure FastCGI Cache in Nginx for WordPress

April 18, 2024 by Sourabh Verma

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.

Enable and Configure FastCGI Cache in Nginx for WordPress
Enable-and-Configure-FastCGI-Cache-in-Nginx-for-WordPress

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
    • Step 2 – Configure your Nginx and enable FastCGI cache
    • Step 3 – Exclude some content from caching
    • Step 4 – Restart Nginx
    • Step 5 – Install and configure WordPress plugin

How to Configure fastcgi in nginx with php fpm

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.

Category: Servers Tagged In: Nginx, Server, Ubuntu

Like my work? Don't Forget to Share

Share on Facebook Share on Twitter Share on LinkedIn Share on Reddit Share on WhatsApp Share via Email

Hi, welcome to SwiftGuides! With over 12 years of experience in Linux, SysAdmin, databases, cloud computing, and other IT-related areas, I make sure publish easy-to-follow, 100% working tutorials. I hope you like my work. Thanks!

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Stay With Us

Follow on Facebook Follow on Twitter Follow on LinkedIn Follow on Reddit Subscribe on YouTube Follow on Instagram

SwiftGuides: Howtos, Tutorials & Guides © 2025 ยท All Rights Reserved

  • Contact Us
  • Privacy Policy
  • Disclaimer