How to enable FastCGI caching with Nginx on Ubuntu Linux

|

,

|

The article provides a detailed guide on enabling FastCGI caching with Nginx on Ubuntu Linux to significantly improve the performance of PHP-based applications. It involves installing the PHP FastCGI module, updating the Nginx main config file and website server block, and optionally integrating specific code for WordPress websites to exclude backend portal, sitemap, and other…

This article describes steps to enable FastCGI caching with Nginx on Ubuntu Linux.

If you are running a WordPress website, using FastCGI caching to improve performance should be considered.

When you combine Nginx and FastCGI modules, you will significantly improve your PHP-based applications. FastCGI module caches dynamic PHP content that is served through the Nginx backend.

When dynamic PHP content is cached, repeated requests for the same content are quickly returned from the cache store instead of compiling all the dynamic data that make up the page each time a request is made.

How to enable FastCGI caching with Nginx on Ubuntu Linux

As described above, combining Nginx and FastCGI modules will significantly improve your PHP-based applications. FastCGI module caches dynamic PHP content that is served through the Nginx backend.

Below is how to enable FastCGI caching with Nginx on Ubuntu Linux.

First, install the PHP FastCGI module. You can do that by running the commands below.

sudo apt update
sudo apt install php-fpm

This article assumes that you have Nginx installed and that it’s functioning. If you haven’t installed Nginx, read the post below to do so.

How to install Nginx on Ubuntu Linux

Set up FastCGI Nginx directive.

Nginx configuration files are stored in the /etc/nginx directory on Ubuntu Linux. Open the Nginx main configuration file (nginx.conf).

Run the commands below to open the Nginx configuration file.

sudo nane /etc/nginx/nginx.conf

Once the file is opened, copy the content below, paste it at the end, then save.

## Nginx FastCGI Cache
    fastcgi_cache_path /var/cache/nginx/fastcgi_temp/cache levels=1:2 keys_zone=czone:100m inactive=60m;
    fastcgi_cache_key $scheme$request_method$host$request_uri;
    fastcgi_cache_lock on;
    fastcgi_cache_use_stale error timeout invalid_header updating http_500;
    fastcgi_cache_valid 200 301 302 60m;
    fastcgi_pass_header Set-Cookie:Set-Cookie;
    fastcgi_pass_header Cookie;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

Configure website server block

Open your website server block after updating the Nginx main config file with the lines above.

On Ubuntu Linux, website server blocks are typically stored in /etc/nginx/sites-available/

Run the commands below to open the default server block file.

sudo nano /etc/nginx/sites-available/default

Then, copy and paste the content below into the file. Usually, under the PHP section of the file, then save.

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    root /var/www/html/example.com;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    try_files $uri =404;
    fastcgi_index index.php;
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    fastcgi_cache czone;
    include fastcgi_params;
    }

If everything is set up correctly, PHP should go through the FastCGI module and enable caching.

If running a WordPress website, you may include the code below in the server block file. This will help you skip caching of the backend portal, sitemap, and other files you don’t want to cache.

set $skip_cache 0;
   # POST requests and url's with a query string should always skip cache
    if ($request_method = POST) {
        set $skip_cache 1;
    }
    if ($query_string != "") {
        set $skip_cache 1;
    }
    # Don't cache url's containing the following segments
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
        set $skip_cache 1;
    }
    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $skip_cache 1;
    }

Save your settings, and you are done.

Conclusion:

This post showed you how to run the Nginx HTTP server with the FastCGI caching module.

Please use the comment form below if you find any errors above or have something to add.

Like this:



Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.