• Skip to main content
  • Skip to primary sidebar
  • Home
  • WordPress
  • web Hosting
  • linux
  • mysql
  • nginx
  • apache2
  • devops

Raju Ginni

wordpress tutorials seo hosting etc

You are here: Home / nginx server tutorials (installation, configuration, performance tuning, security) / nginx fastcgi cache wordpress how to configure

nginx fastcgi cache wordpress how to configure

 

 

Table of Contents

Toggle
  • #1 Enabling Fastcgi Cache in nginx.conf
  • #2 Step adding Code in Directory block
  • #3. Adding Fastcgi Directives in PHP location block:
  • # 4 CHecking Status & monitoring nginx Fastcgi Cache
  • best location for FastCGI cache RAM or Disk
  • Enabling  Fastcgi Cache in shared Memory
    • How to Purge CLear Fastcgi Cache?
    • #3 Deleting Fastcgi Cache 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
    • Additional fastCGI settings for nginx.conf

#1 Enabling Fastcgi Cache in nginx.conf

 

Add the Below code in etc/Nginx.conf  or

the domain configuration file (server block)

  1. 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;

 

fastcgi configuration nginx

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

##
# Fastcgi cache
##
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:400m max_size=812m 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;
add_header Fastcgi-Cache $upstream_cache_status;

server block.file

fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 60m;
fastcgi_temp_file_write_size 10m;
fastcgi_busy_buffers_size 512k;
fastcgi_buffer_size 512k;
fastcgi_buffers 16 512k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_intercept_errors on;
fastcgi_next_upstream error invalid_header timeout http_500;

3rd way of Reusing code Nginx.confd folder

/etc/nginx/conf.d/fastcgi.conf
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS: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;

b. nginx http block

top of the file outside the server { } directive: http block
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key “$scheme$request_method$host$request_uri”;

C. Adding php location Block

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_cache MYAPP;
fastcgi_cache_valid 200 60m;

explanation of Fastcgi Cache

fastcgi_cache fideloper;
fastcgi_cache_valid 200 60m; # Only cache 200 responses, cache for 60 minutes
fastcgi_cache_methods GET HEAD; # Only GET and HEAD methods apply
add_header X-Fastcgi-Cache $upstream_cache_status;
fastcgi_cache_bypass $no_cache; # Don’t pull from cache based on $no_cache fastcgi_no_cache $no_cache; # Don’t save to cache based on $no_cache
fastcgi_cache_path /tmp/cache levels=1:2 keys_zone=fideloper:100m inactive=60m; fastcgi_cache_key “$scheme$request_method$host$request_uri”;

other resources

redis object caching

Additional fastCGI settings for nginx.conf

 

fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:50m inactive=60m;
fastcgi_cache_key “$scheme$request_method$host$request_uri”;
fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503;
fastcgi_cache_valid any 1h;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SERVER_NAME $http_host;
fastcgi_ignore_headers  Cache-Control Expires Set-Cookie;
fastcgi_temp_file_write_size 10m;
fastcgi_busy_buffers_size 512k;
fastcgi_buffer_size 512k;
fastcgi_buffers 16 512k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_intercept_errors on;
fastcgi_next_upstream error invalid_header timeout http_500;

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://rajuginni.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.

 

 

 

Primary Sidebar

nginx server tutorials (installation, configuration, performance tuning, security)

  • Letsencrypt SSL Installation on apache/Nginx ubuntu / debian wordpress
  • fix error 520 522 524 on cloudflare wordpress godaddy nginx etc
  • nginx fastcgi cache wordpress how to configure
  • install LEMP Stack on 22.04 LTS Nginx MySQL PHP fpm #wordpress #digital ocean
  • Apache vs nginx (connection handling, modules, memory usage)
  • Pagespeed module install, configure, monitor, errors ft nginx &apache
  • nginx errors (504,502, 413, unable to start, syntax errors)
  • nginx conf explained best config file performance tuning tips nginx.conf location errors tutorial
  • use nginx as reverse proxy and load balancer for apache wordpress
  • nginx rewrite rules with examples 301 redirection
  • nginx modules list (enable, disable, upgrade, install dynamic module)
  • php fpm pool manager configuration settings based on server spike high cpu wordpress
  • php fpm restart nginx ubuntu enable status page, monitor etc
  • what is TTFB & how to Reduce it (server response time) Google pagespeed
  • letsencrypt install configure on ubuntu / debian nginx
  • Top 10 tips to improve nginx server security
  • nginx performance tuning connections, buffers file descriptors
  • enable brotli compression nginx brotli vs gzip
  • nginx installation on ubuntu 20.04 LTS
  • monitor nginx request with nginx status amplify datadog new relic
  • SSL faster reduce TLS hand shake improve https performance
  • nginx rate limiting explained by location time specific url
  • datadog nginx integration installation process
  • newrelic nginx integration process and errors fix and metrics
  • php fpm seems busy fixed warning and max children reached to handle max connections / requests
  • Php fpm configuration for 1000 concurrent connections server busy max children reached
  • php fpm ondemand vs dynamic vs Static (the dynamic pool problem)
  • nginx upstream response is buffered to a temporary file
  • php fpm install ubuntu 20.04 nginx
  • install phpmyadmin ubuntu nginx 22.04
  • upgrade php fpm ubuntu nginx 7.4 to 8.2
  • nginx add last modified header (remove, php wordpress)
  • php fpm dynamic pool manager settings
  • nginx fastcgi cache purge
  • nginx open file limit connections ulimits sysctl
  • php fpm high cpu usage WordPress 4 solutions nay work for you
  • nginx buffer size for wordpress
  • Cloudflare error code 524 nginx a timeout error occured
  • server configuration for 1000 concurrent users

hi i am raju ginni, primalry i manage wordpress websites on GCP cloud platform as a cloud engineer, and create content on passionate things.
you can follow me on youtbe

© 2025 - All Rights Reserved Disclaimer & Privacy Policy