Set Up Nginx FastCGI Cache to Reduce WordPress Server Response Time
As Nginx is unable to process PHP code, it makes use of the PHP-FPM module. However, this can result in a relative negative performance impact as the page request to PHP-FPM requires that the HTML page is generated every time. To address this, FastCGI Cache can be enabled to cache the already generated HTML pages so that they can be instantly served upon subsequent requests.
Configure Nginx FastCGI Cache using Bunnyshell
-
Edit the Nginx configuration file:
Go to the virtual machine on which your WordPress application is installed -> OPS -> Packages -> Edit Nginx -> Advanced Settings -> Custom Settings Configuration
Note: If the command line box is empty, make sure to change the last character of the page URL from 0 to 1 and reload the page.
Add the following lines in the http context:
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=phpcache:100m max_size=10g inactive=60m use_temp_path=off; fastcgi_cache_key "$scheme$request_method$host$request_uri";
Note: Find out more information on the various settings here.
It is recommended that you copy the content of the file to your local computer as a backup before attempting any modifications.
2. Edit the Nginx vhosts pertaining to your WordPress application:
Go to the virtual machine on which your WordPress application is installed -> OPS -> Packages -> Edit Nginx -> Basic Settings
Add the following lines in the two entries for the http and https vhosts of your WordPress application in the location ~ \.php$ section:
fastcgi_cache phpcache;
fastcgi_cache_valid 200 301 302 60m;
fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
fastcgi_cache_min_uses 1;
fastcgi_cache_lock on;
add_header X-FastCGI-Cache $upstream_cache_status;
Note: It is recommended that you copy the content of both vhost files to your local computer as a backup before attempting any modifications.
3. Click on Update Package
4. Mark Nginx for deployment (activate the green switch)
5. Click on Deploy Changes
6. To test if FastCGI is working a SSH connection to the virtual machine is necessary. Reload your site’s home page for a few times then type-in the following command on the VM's command-line interface:
curl -I http://www.your-domain.com
The output should be similar to this:
HTTP/1.1 200 OK
Server: nginx/1.19.3
Date: Tue, 20 Oct 2020 08:15:45 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/7.4.11
Link: <http://wordpress5f8d917e5d7ea.cloud.bunnyroute.com/wp-json/>; rel="https://api.w.org/"
X-FastCGI-Cache: HIT
Notice the X-FastCGI-Cache header, where HIT indicates the page was served from cache.