FastCGI Micro Cache in NGINX

I am a DevOps Consultant. 3 years experience with Cloud technologies. 9 years total IT experience in the Linux, Networking and Data Visualization domains. Love listening to music and playing older PC games.
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
- Edit the NGINX main configuration file to add the following lines in the
httpcontext:
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;
- Edit the virtual host configuration file in which caching is to be enabled.
- Add the following content to the
servercontext:
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
serverblock is the top-level configuration context for NGINX. It defines the settings for a virtual server.The
setdirective is used to set a variable to a value. In this case,$no_cacheis set to0by default.The
ifdirective is used to execute a block of code if a certain condition is met. In this case, it checks if the query parameterskipcacheis set to1. If it is,$no_cacheis set to1.The
locationdirective is used to define how NGINX should handle requests for a specific URL pattern.The first
locationblock defines how NGINX should handle requests for the root URL (/). It uses thetry_filesdirective 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
locationblock defines how NGINX should handle requests for PHP files (files ending in.php). It includes thefastcgi.conffile, which contains common FastCGI configuration settings. It then passes the request to the PHP-FPM service using thefastcgi_passdirective.The
fastcgi_cachedirective enables caching for FastCGI requests. In this case, it uses the cache zone namedZONE_1.The
fastcgi_cache_validdirective 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_bypassandfastcgi_no_cachedirectives are used to control when cached content should not be used. In this case, they use the$no_cachevariable that was set earlier.
Apply your configuration changes.
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)




