How to Install FastCGI Cache in Nginx for WordPress

WordPress needs no introduction; as it is one of the most versatile and yet powerful CMS in the world which powers more than 25% of sites in the world. There are many ways you can setup WordPress. Most popular method is using cPanel provided one click install.

WordPress on NGINX with FastCGI Cache

However professionals are not using it, as cPanel is buggy and it acts as resource hungry tool. Professionals utilises managed or unmanaged services which uses 100% resource of the servers. In our recent article we shown how to…

Install WordPress with NGINX PHP7 PHPMyAdmin on Ubuntu

PHP7 makes WordPress buttery smooth as it uses JIT (Just in Time) technology similar like HHVM, and even beats HHVM in performance. Still we have to consider a robust setup so we need to ensure that there are cache mechanism to low down the burden of hardware resources. Thus, after many test and trials I am presenting you how to enable FastCGI cache in WordPress which is also known as MicroCache or Server-Side Cache.

Step 1: Create a Cache Folder

If you have followed our previous tutorials you may have seen that we store our WordPress setup to /var/www/html, so we need to create a folder for cache also. We can use WordPress’s default cache directory but we are not using it, why want to know?

Because while backing up WordPress directory the cache folder also get backed up and we don’t want to happen this. So we can create one directory at the top of WordPress at /var/www/cache/site. You can change this to any location but do not keep it inside WordPress directory.

Step 2: Edit NGINX’s Server Block

sudo nano /etc/nginx/sites-available/default

Just add below blue lines in your current configuration. I have copied our previous code and added cache code in blue color.

fastcgi_cache_path /var/www/cache/site levels=1:2 keys_zone=WORDPRESS: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;
server {
    listen 80 default_server;
    root /var/www/html;
    index index.php index.html index.htm;
    server_name atulhost.com www.atulhost.com;
    charset UTF-8;
    set $skip_cache 0;
    if ($request_method = POST) {set $skip_cache 1;}
    if ($query_string != "") {set $skip_cache 1;}
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|/wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {set $skip_cache 1;}
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {set $skip_cache 1;}
location / {
    try_files $uri/ /index.php?$args;
    }
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    fastcgi_cache WORDPRESS;
    fastcgi_cache_valid 200 60m;
    }
location ~* \.(js|css|png|jpg|jpeg|gif|ico|eot|otf|ttf|woff)$ {
    add_header Access-Control-Allow-Origin *;
    access_log off; log_not_found off; expires 30d;
    }
location = /robots.txt { access_log off; log_not_found off; }
location ~ /\. { deny all; access_log off; log_not_found off; }
}

Now save the file.

Step 3: Check and Restart Services

Now we need to restart NGINX and check weather it is working or not.

sudo chown -R www-data:www-data /var/www
sudo service nginx -t

Of you see following message like this,

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Then restart NGINX.

sudo service nginx restart

Now login to WordPress and add one plugin called Nginx Cache and add the same cache location (i.e., /var/www/cache/site) inside that plugin. With the help of this you will able to purge cache inside WordPress itself. Since it is very basic plugin which is made to delete a directory, it won’t hurt the performance anyhow.

If you have any suggestions or feedback regarding the above setup let us know using the comment section below, and don’t forget to share your experience after using this setup.

Share This

Author: AtulHost

Hi, I'm Atul Kumar Pandey, a full time blogger by passion and profession. Since I started working on the web industries many years ago, with my all expertise I blog on atulhost.com, where my main objective is to build and market quality contents and reviews.