• Home
  • WordPress
  • web Hosting
    • Free hosting
    • Cheap Hosting
    • comparison cloud , vps, shared, wordpress
    • managed wordpress hosting
    • managed cloud hosting
  • page Speed
  • Deals
  • Services
  • About

RAaz Kumar .com

wordpress tutorials seo hosting etc


nginx fastcgi cache wordpress how to configure

 

 

#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.

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

 

 

 

Related topics:

  1. enable caching in apache server (mod cache disk cache) vs fastcgi cache
  2. enable browser caching in wordpress apache nginx caching headers explained
  3. use nginx as reverse proxy and load balancer for apache wordpress
  4. letsencrypt install configure on ubuntu / debian nginx
  5. Pagespeed module install, configure, monitor, errors ft nginx &apache

tutorials

  • Vivek Bindra Videos Transcription (business strategy)
  • git commands
  • new relic php agent install in 3 steps
  • new relic mysql install integration - 2 ways fix problems
  • new relic installation linux (infrastructure agent , php, mysql , nginx)
  • xampp tutorials 2021 installation errors fix wordpress phpmyadmin mysql apache
  • Redis performance metrics & tuning for nginx apache ubuntu & debian
  • Devops course Syllabus topics PDF AWS, Azure, cisco, redhat
  • CCNA Syllabus pdf (CCNA / CCNP vs devops vs mcsa /MCSE)
  • how to create a website free of cost on google
  • what is vpn vs proxy vs tor, http vs https, http2, tcp vs udp, kali linux sql source code injection
  • nginx server tutorials (installation, configuration, performance tuning, security)
    • Top 10 tips to improve nginx server security
    • install phpmyadmin ubuntu nginx 22.04
    • php fpm install ubuntu 20.04 nginx
    • nginx upstream response is buffered to a temporary file
    • php fpm ondemand vs dynamic vs Static (the dynamic pool problem)
    • Php fpm configuration for 1000 concurrent connections server busy max children reached
    • php fpm seems busy fixed warning and max children reached to handle max connections / requests
    • newrelic nginx integration process and errors fix and metrics
    • datadog nginx integration installation process
    • nginx rate limiting explained by location time specific url
    • SSL faster reduce TLS hand shake improve https performance
    • monitor nginx request with nginx status amplify datadog new relic
    • nginx installation on ubuntu 20.04 LTS
    • enable brotli compression nginx brotli vs gzip
    • nginx performance tuning
    • Letsencrypt SSL Installation on apache/Nginx ubuntu / debian wordpress
    • letsencrypt install configure on ubuntu / debian nginx
    • what is TTFB & how to Reduce it (server response time) Google pagespeed
    • php fpm restart nginx ubuntu enable status page, monitor etc
    • php fpm pool manager configuration settings based on server spike high cpu wordpress
    • nginx modules list (enable, disable, upgrade, install dynamic module)
    • nginx rewrite rules with examples 301 redirection
    • use nginx as reverse proxy and load balancer for apache wordpress
    • nginx conf explained best config file performance tuning tips nginx.conf location errors tutorial
    • nginx errors (504,502, 413, unable to start, syntax errors)
    • Pagespeed module install, configure, monitor, errors ft nginx &apache
    • Apache vs nginx (connection handling, modules, memory usage)
    • install LEMP Stack on 22.04 LTS Nginx MySQL PHP fpm #wordpress #digital ocean
    • nginx fastcgi cache wordpress how to configure
    • fix error 520 522 524 on cloudflare wordpress godaddy nginx etc
  • MySQL Tutorial (create,connect database, update tables, export etc) Documentation & TIPS
  • AUdio Editing Background Noise removal (Audacity, Adobe Premiere Addition, Camtasia Filmora Windows Obs)
  • Android Studio tutorials syllabus Topics Course details #AndroidApplicationDevelopment
  • [INTRO] Ethical hacking / cyber Security / Penetration testing Tutorial -{updates frequently}
  • redis install ubuntu 20.04 with wordpress php redis mysql configuration
  • ubuntu tutorials installation download issues etc
  • Php tutorials
  • HTML & CSS Tutorials
  • Core Java Tutorial Free online
  • Linux sysadmin tutorials linux system administrator
  • apache server tutorial (install,config, performance tuning, security, error handling)

 

 

wordpress Pagespeed optimization

Digitalocean free $100 Credit

Cloudways Review pricing promo codes

Siteground cpu limits Pricing

Shared Hosting

Managed wordpress Hosting

Managed Hosting Cloud server

VPS Hosting

Cloud Hosting – Unmanaged

Google cloud Pricing

Aws Pricing

Azure pricing

nginx server tutorials

apache server tutorials

linux sysadmin tutorials

mysql Commands list pdf

LEMP Install Ubuntu

Mysql Performance Tuning

Nginx Performance tuning

Linux Performance tuning

Php -fpm performance tuning

Redis Performance tuning

linux server security

nginx security best practices

wordpress security plugins

 

 

wordpress

 

Top 5  WP Google Analytics Plugins

WP Backup Plugins

Wp Comment Plugins

Top wordpress Security Plugins

WP Seo Plugins

WP Caching Plugins

Best Adsense Plugins for WordPress

Wp social Sharing Plugins

autoshare social media plugins

WP speed Optimization Plugins

Speedup WordPress google Score

More Wp tuts

Server Admin Cloud

 

Installing Nginx LEMP On ubuntu

Installing apache Lamp ubuntu

nginx fastcgi cache enable

php – fpm install  & Configuration

Opcache install & Configure

php -fpm pool manager explained

Mysql Install & Configuration

Redis Object cache install & configure

 

Nginx as Reverse Proxy and Load balancer

Load Balance / auto scaling in google cloud

Linux Commands PDF

Mysql Commands Pdf

Letsencrypt tutorial

mysqldump export & import 

Pagespeed Module install & configure

nginx.conf best file

mysql.conf best file

upgrade ubuntu

© 2023 - All Rights Reserved Disclaimer & Privacy Policy