How to Configure FastCGI Cache with Nginx?

Published on April 11th, 2020 by

Nginx has obtained traction from webmasters due to its low-resource and high-performance feature. Although the default configuration of Nginx is already optimized for all the basic workloads; still you can take it to the next level by setting up a server-side cache.

Our Previous Tutorials:

Here whenever I said “Server-Side Cache” means “Nginx FastCGI Cache”.

What is Nginx FastCGI Cache?

In any LEMP stack, we always configure Nginx to pass PHP requests to PHP-FPM because Nginx itself cannot process PHP code directly.

Nginx is blazing fast when it comes to serving static HTML pages. However, PHP is known to be slow, although the latest version PHP7 is much faster than previous versions. On the other side, MySQL/MariaDB database is another performance bottleneck of LEMP stack websites.

What FastCGI cache does is instead of transferring the dynamic page request to PHP-FPM and let it generate the HTML page every time, Nginx can cache the generated HTML page so next time it can send cached pages to web browsers, eliminating PHP and database requests.

  • This will surely improve your response time or TTFB (Time to First Byte).
  • You save a lot of CPU resources wasted on dynamic content processing.
  • Limited interaction of PHP and Database means the low load on the server.
  • It will serve the cached resources when PHP and Database fails or stops.

FastCGI is the medium between Nginx and PHP-FPM so the cache is called FastCGI cache.

Why Server-Side Cache?

Because one less plugin is needed in my site which is built on WordPress. In this article, I’ll show you how to set up FastCGI Cache with the Nginx server.

WordPress on NGINX with FastCGI Cache

This article assumes you have direct access (SSH) to your server and have root privilege.

Since you’ve already installed Nginx, you don’t need to install anything else.

Just start with editing Nginx’s configuration file/s.

First we need to set FastCGI cache directives in order to cache work.

Make sure below directives are placed either in http block if editing nginx.conf or outside server block if editing the site’s configuration file.

fastcgi_cache_path /var/run/webcache levels=1:2 keys_zone=atulhost:100m inactive=30m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

Now you need to edit server block for caching mechanism. To edit your PHP configs you need to edit in the server block, just search for the PHP section there.

location ~ .php$ {
    // PHP CONFIGURATIONS...
    fastcgi_cache atulhost;
    fastcgi_cache_valid 200 10m;
    fastcgi_cache_methods GET HEAD;
    add_header X-AtulHost-Cache $upstream_cache_status;
}

Here you need to just copy four lines inside of PHP configs.

Those four lines are explained below, you can edit them according to your need.

  • First – connect the PHP with FastCGI Cache’s key_zone.
  • Second – cache only 200 responses for 10 minutes.
  • Third – cache either GET or HEAD requests, don’t cache POST requests.
  • Fourth – show cache status in response headers (HIT or MISS).

Now everything is perfect to make a test run. Save the config file/s.

Before restarting Nginx, I’ll recommend you to do a dry test for any errors.

nginx -t

If no error is showing that means you are good to go.

systemctl restart nginx

Now you’ve FastCGI cache enabled, you can handle more traffic form existing server.

To test it on your SSH terminal use the following command.

curl -I http://www.Your-Domain.com

Here is our outcome: look for x-atulhost-status line.

FastCGI Cache Status

Here, HIT means Cached and MISS means Not Cached Yet.

If you have any configuration issues do let us know.

Published in Tech Tutorials
Labeled with , , , ,

About AtulHost

Hi, I'm Atul Kumar Pandey; also known as AtulHost on the web, a blogger by profession with an objective of sharing fresh and real contents in the business and technical topics; from contents to useful resources it's all here.

Leave a Reply

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