How to Set Up FastCGI Cache with Nginx (basic example)

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, you will learn how to set up FastCGI on Nginx which will give you PHP caching capability. Let’s get started.

1. FastCGI Example Nginx Configuration

Assuming you have PHP FPM (FastCGI Process Manager) version 7.0 installed, below you will find a basic Nginx configuration. Obviously, change the PHP FPM version to the version that’s installed on your system.

server {
    listen 80;
    server_name _;
    root   /var/www/html/;
    index  index.php;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        include fastcgi_params;

    }
}

Notice how the value of fastcgi_pass is a Unix socket that points to the default PHP FPM socket. Also take note that we include fastcgi_params and snippets/fastcgi-php.conf.

At this point, this is just a vanilla FastCGI configuration file. No caching has been implemented. We will do that in the next step.

2. Implement FastCGI Cache on Nginx

You can use the following configuration file as an example to implement FastCGI cache with Nginx. Make the following highlighted changes.

fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MY_CACHE:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

server {
    listen 80;
    server_name _;
    root   /var/www/html/;
    index  index.php;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        include fastcgi_params;
        fastcgi_cache MY_CACHE;
        fastcgi_cache_valid 200 60m;
        add_header X-Cache $upstream_cache_status;
    }
}

The fastcgi_cache_path directive defines the location on your server where the cache will reside. Notice how the keys_zone value is MY_CACHE. We associate MY_CACHEĀ  with our fastcgi_cache on line 14. For a full explanation of the arguments, please see the official Nginx docs. You can optionally specify the structure of a key with the fastcgi_cache_key directive on line 2.

In this example, resources with a status code of 200 will be cached for a period of 60 minutes according to the fastcgi_cache_valid directive.

Finally, we add the caching status to the response header with the add_header directive.

3. Apply Your Configuration Changes

Apply your changes by reloading the Nginx web server.

systemctl reload nginx

4. Test Out FastCGI Cache

When you visit a page on your website for the first time, you will see a cache MISS in the response header. This means that the FastCGI server did not find this page in its cache, so it had to dynamically generate the page.

Nginx server-level cache miss

The next time you visit the page, you will see a cache HIT in the response header. This means that the FastCGI server found this page in its cache and served it directly.

Nginx server-level cache hit

I have an entire video tutorial on FastCGI caching with Nginx which will also walk you through these configuration steps in real-time. Please let me know if you have any questions.


Meet Tony

With a strong software engineering background, Tony is determined to help as many people as possible start their online busines. Discover why Tony quit his hedge fund job to pursue this mission. You can send Tony a message here.

Leave a Comment