Bobcares

How to setup FastCGI caching with Nginx?

by | Oct 14, 2020

Nginx contains a FastCGI module that can cache dynamic content served from the PHP background.

As a part of our Server Management Services, we help our Customers with Nginx related requests regularly.

Let us today discuss the possible steps to setup FastCGI caching with Nginx.

Enabling FastCGI Caching on a VPS

Nginx passes PHP request to PHP-FPM because Nginx itself is unable to process PHP code. 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 edit the Virtual Host configuration file. 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.

Further, 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.

Also, 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

Testing FastCGI Caching on the VPS

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

<?php
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 any further assistance to setup FastCGI caching with Nginx? – We’re available 24*7]

Conclusion

In short, Nginx can cache the HTML pages generated from a PHP code thereby eliminating the PHP/database requests. Today, we saw how our Support Engineers setup FastCGI cache with Nginx.

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

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

Never again lose customers to poor
server speed! Let us help you.