How to Enable FastCGI Caching with Nginx on Ubuntu Linux

Share

This article describes steps one can take 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 greatly 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, when you combine Nginx and FastCGI modules, you will greatly 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 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

On Ubuntu Linux, Nginx configuration files are stored in the /etc/nginx directory. Open 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 and content below and paste it at the end of the file, 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

After updating Nginx main config file with the lines above, open your website server block.

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 be going through the FastCGI module and caching enabled.

If you are running a WordPress website, you may also include the code below in the website 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 FastCGI caching module.

If you find any error above or have something to add, please use the comment form below.