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.
Search for a command to run...

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.
No comments yet. Be the first to comment.
In this series, I will explore ways to optmize and secure NGINX using best practices.
The Cache-Control header is a powerful tool that can be used to control how a browser caches a resource. Here are some real-world examples of how to use the Cache-Control header in a location block: To set a maximum age for a resource, you can use t...
Hi readers, There are times when we make huge decisions in our lives. These may not be easy to make due to several reasons. Like the decision I made to quit DevOps completely. You may feel that I quit suddenly, but I took this decision years ago. It ...

Objectives Target the code at: https://github.com/melvincv/java-basic-practice-app Switch to main-v2 branch. Build the code using GitHub Actions. Deploy the code on two AWS EC2 servers. (independently) Implement approval process for deployment o...

with Sonarqube, Ansible and Blue / Green deployment

Jenkins Install Controller and Agent The controller is the main Jenkins instance that will co-ordinate the connections and tooling for the agents. Agents are the workhorse instances used to run jobs and pipelines. It can have various tools and unique...

Docker, Kubernetes and GitOps

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.
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;
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.
Apply your configuration changes.
Test out FastCGI Micro Cache using Apache Bench, etc.
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.
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)