Nginx FastCGI caching

Need to know how to configure Nginx FastCGI caching? This guide will help you.

With Nginx web server, you can easily configure Caching with its FastCGI module to cache dynamic content served from the PHP background.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to solve Nginx related tasks.

This context will guide you on the steps to follow to configure FastCGI caching with Nginx.

How to set up FastCGI Caching on your VPS?

Nginx on its own cannot process PHP code as it passes PHP request to PHP-FPM.
Instead of passing the dynamic page request to PHP-FPM to generate an HTML page every time, Nginx can cache the generated HTML page.

This allows it to send cached pages to web browsers, eliminating PHP and database requests.

To enable FastCGI caching on a VPS, we need to modify the Virtual Host configuration file.
You need to open the "/etc/nginx/sites-enabled/vhost" file with a suitable text editor and add the following line to the top of the file outside the server { } directive:

fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

The "fastcgicachepath" directive specifies the location of the cache, its size, memory zone name, the subdirectory levels, and the inactive timer.

Similarly, the "fastcgicachekey" directive specifies the way to hash the cache filenames.

Next, move the location directive that passes PHP requests to php5-fpm. Inside "location ~ .php$ { }" add the following lines.

fastcgi_cache MYAPP;
fastcgi_cache_valid 200 60m;

The "fastcgicache" directive references to the memory zone name which we specified in the "fastcgicache_path" directive and stores the cache in this area.

Additionally, the "fastcgicachevalid" directive specifies the default cache lifetime. Once we have done the changes to the file, perform a configuration test with the command below and reload Nginx if everything is OK:

service nginx configtest
service nginx reload

How to test FastCGI Caching on your VPS

To test the caching create a PHP file "/usr/share/nginx/html/time.php" which outputs a UNIX timestamp.

echo time();

Now, request this file multiple times using curl or your web browser.

root@server:~# curl http://localhost/time.php;echo

If caching works properly, we will see the same timestamp on all requests.

Setting Cache Exceptions

Dynamic contents such as authentication require pages not to be cached. We can exclude such contents from being cached based on server variables like "requesturi," "requestmethod," and "http_cookie."

For instance, to not cache POST request, we can use these lines in the configuration:

#Don't cache POST requests
if ($request_method = POST)
set $no_cache 1;

To apply the "$no_cache" variable to the appropriate directives, place the following lines inside location ~ .php$ { }

fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;

The "fasctcgicachebypass" directive ignores the existing cache for requests related to our previously set conditions. The "fastcginocache" directive does not cache the request at all if the conditions are met.

[Need support in configuring FastCGI caching with Nginx? We are available to help you today.]


This article will show you how to configure FastCGI cache with Nginx since it can cache the HTML pages generated from a PHP code thereby eliminating the PHP/database requests.