FastCGI Micro Cache in NGINX

FastCGI Micro Cache in NGINX

Introduction

FastCGI Micro Cache is a caching mechanism that caches dynamic content in front of a web server with NGINX. It is the most efficient way to implement a dynamic cache mechanism. In this article, we will discuss how to configure FastCGI Micro Cache in NGINX.

Configuration

  1. Edit the NGINX main configuration file to add the following lines in the http context:
http {
# Configure microcache (fastcgi)
  fastcgi_cache_path /tmp/nginx_cache levels=1:2 keys_zone=ZONE_1:100m inactive=60m;
  fastcgi_cache_key "$scheme$request_method$host$request_uri";
  add_header X-Cache $upstream_cache_status;
  1. Edit the virtual host configuration file in which caching is to be enabled.
  1. Add the following content to the server context:
server {
    # Cache by default
    set $no_cache 0;
    # Check for cache bypass
    if ($arg_skipcache = 1) {
      set $no_cache 1;
    }

    location / {
      try_files $uri $uri/ =404;
    }

    location ~\.php$ {
      # Pass php requests to the php-fpm service (fastcgi)
      include fastcgi.conf;
      fastcgi_pass unix:/run/php/php7.1-fpm.sock;

      # Enable cache
      fastcgi_cache ZONE_1;
      fastcgi_cache_valid 200 60m;
      fastcgi_cache_bypass $no_cache;
      fastcgi_no_cache $no_cache;
    }
  }

Here’s a brief explanation of the code:

  • The server block is the top-level configuration context for NGINX. It defines the settings for a virtual server.

  • The set directive is used to set a variable to a value. In this case, $no_cache is set to 0 by default.

  • The if directive is used to execute a block of code if a certain condition is met. In this case, it checks if the query parameter skipcache is set to 1. If it is, $no_cache is set to 1.

  • The location directive is used to define how NGINX should handle requests for a specific URL pattern.

  • The first location block defines how NGINX should handle requests for the root URL (/). It uses the try_files directive to check if the requested file exists and serve it if it does. If it doesn’t exist, it returns a 404 error.

  • The second location block defines how NGINX should handle requests for PHP files (files ending in .php). It includes the fastcgi.conf file, which contains common FastCGI configuration settings. It then passes the request to the PHP-FPM service using the fastcgi_pass directive.

  • The fastcgi_cache directive enables caching for FastCGI requests. In this case, it uses the cache zone named ZONE_1.

  • The fastcgi_cache_valid directive sets how long cached content should be considered valid. In this case, it sets it to 60 minutes for responses with a status code of 200.

  • The fastcgi_cache_bypass and fastcgi_no_cache directives are used to control when cached content should not be used. In this case, they use the $no_cache variable that was set earlier.

  1. Apply your configuration changes.

  2. Test out FastCGI Micro Cache using Apache Bench, etc.

Conclusion

FastCGI Micro Cache is an efficient way to implement a dynamic cache mechanism in front of a web server with NGINX. It can help improve website performance and reduce server load. By following the steps outlined in this article, you can easily configure FastCGI Micro Cache in NGINX.

References

How to Setup FastCGI Caching with Nginx on your VPS | DigitalOcean

NGINX Fundamentals: High Performance Servers from Scratch | Udemy

Cache your Web Site with NGINX and FastCGI in CentOS 7 - PHP-FPM (ryadel.com)