#1 Enabling Fastcgi Cache in nginx.conf
Add the Below code in etc/Nginx.conf or
the domain configuration file (server block)
- mkdir /var/www/html/fastcgicache && chown www-data:www-data /var/www/html/fastcgicache
fastcgi_cache_path /var/www/html/fastcgicache levels=1:2
keys_zone=FASTCGICACHE:100m inactive=60m; max_size=1024m; fastcgi_cache_key
“$scheme$request_method$host$request_uri”; fastcgi_cache_use_stale error
timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=FASTCGICACHE:100m inactive=60m;
fastcgi_cache_key “$scheme$request_method$host$request_uri”;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
#2 Step adding Code in Directory block
set $skip_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != “”) {
set $skip_cache 1;
}
# Don’t cache uris 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;
}
#3. Adding Fastcgi Directives in PHP location block:
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache FASTCGICACHE;
fastcgi_cache_valid 60m;
add_header X-FastCGI-Cache $upstream_cache_status;
Configuring values
# 4 CHecking Status & monitoring nginx Fastcgi Cache
verifying
HIT – returned from the FastCGI cache.
•MISS – missed
•BYPASS – cached but bypass ex: admin pages.
using command line
curl -I https://yoursite.com
or chrome tools
right click >> inspect>>network>>select main HTML file >> check headers.
Test results
loader, any load testing tools
best location for FastCGI cache RAM or Disk
Read write speeds disks lower than RAM 16Gbps s and 500Mbps
read more on HDD vs SSD (sata & nvme) vs RAM
1.Check Size of Run Directory (which is tmpfs memory location)
df -h /var/run
20% your server RAM
Defaults to 10% core memory (debian)
1 gb
2.mkdir /var/run/nginx-cache
3. Add Permission to nginx
chown www-data:www-data -R /var/run/nginx-cache
or
DIsk location
Create cache folder if not exist.
fastcgi_cache_path /usr/share/nginx/cache/fcgi levels=1:2 keys_zone=microcache:10m max_size=1024m inactive=1h;
add_header X-Cache $upstream_cache_status;
Enabling Fastcgi Cache in shared Memory
df -h /var/run
Filesystem Size Used Avail Use% Mounted on
tmpfs 370M 892K 369M 1% /run
free -m
total used free shared buff/cache available
Mem: 3693 1415 1485 186 792 1851
Swap: 0 0
my var/run file size 10% of core ram (3.7Gb) so i allocated 200M or 100m for fastcgi
sudo mkdir /var/run/nginx-cache
cd /var/run/
ls
mkdir nginx-cache
mv nginx-cache fastcgicache
granting permission to server user (www-data:www-data or nginx runs as root)
sudo chown www-data:www-data -R /var/run/fastcgicache
How to Purge CLear Fastcgi Cache?
1.Add this Code your nginx /conf.d/ default.conf
location ~ /purge(/.*) {
fastcgi_cache_purge WORDPRESS “$scheme$request_method$host$1”;
}
2. Enter server ip/purge to clear the cache
or
install this to purge content automatically when wp pages updated.
https://wordpress.org/plugins/nginx-cache
in Some cases Fastcgi Caches won’t delete.
Then Delete Fastcgi Cache Directory, or Assign new location reload nginx.
#3 Deleting Fastcgi Cache Directory
cd /ar/run/cache
ls
or
rm -r var/run/fastcgicache/*
Deletes files in cache directory not directory
2nd way to configure FastCGI cache in nginx
nginx.conf file
server block.file
3rd way of Reusing code Nginx.confd folder
b. nginx http block
C. Adding php location Block
explanation of Fastcgi Cache
other resources
redis object caching
Additional fastCGI settings for nginx.conf
APC or Redis for WordPress, Opcode cache, page speed module, improve MySQL performance.
How I configured FastCGI cache for WordPress in 2019 nginx version 15+
This time i want to create a separate file for FastCGI
nano /etc/nginx/conf.d/fastcgi.conf
fastcgi_cache_path /var/run/fastcgicache levels=1:2 keys_zone=FASTCGICACHE:100m inactive=60m;
fastcgi_cache_key “$scheme$request_method$host$request_uri”;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
my default server’s php location block or global config file all domains
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache FASTCGICACHE;
fastcgi_cache_valid 60m;
add_header X-FastCGI-Cache $upstream_cache_status;
finally reloading Server
nginx -t && nginx -s reload
checking
curl -I https://website.com
1st time
x-fastcgi-cache: MISS
2nd time
x-fastcgi-cache: HIT
anyway, its done need to enhance and implement object cache /page cache etc.
I need to check TTFB (time to first byte Aka server response time before fastCGI 800ms
after 1022 ms (you know it depends on many factors just ignoring it right now)
fastcgi_cache_min_uses 3;
fastcgi_cache_revalidate on;
// uses if modified since to add the expired header in nginx.conf
fastcgi_cache_lock on;
testing response time
$ curl -s -w ‘Testing Website Response Time for :%{url_effective}\n\nLookup Time:\t\t%{time_namelookup}\nConnect Time:\t\t%{time_connect}\nAppCon Time:\t\t%{time_appconnect}\nRedirect Time:\t\t%{time_redirect}\nPre-transfer Time:\t%{time_pretransfer}\nStart-transfer Time:\t%{time_starttransfer}\n\nTotal Time:\t\t%{time_total}\n’ -o /dev/null https://www.google.com
$ curl -s -w %{time_total}\\n -o /dev/null http://www.shellhacks.com
nginx http2
sudo apt-get install -y nghttp2
curl –head https://raazkumar.com
Conclusion: this post may confuse you, but over time it will be improved, my case page TTFB was high 800ms (but i implemented, opcode cache, APC /Redis along with fastCGI) now i realized when i migrated I have used default nginx config file not my exist.