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.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF