nginx php-fpm caching – Waiting (TTFB)

from 8.2s to 815ms

8.29 s
815.08 ms

Disk cache

fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=FASTCGICACHE:100m inactive=60m max_size=1024m; 

RAM cache

fastcgi_cache_path /var/run/nginx-cache/fcgi levels=1:2 keys_zone=FASTCGICACHE:100m inactive=60m max_size=1024m; # ram

You need to create the folder and change ownership permissions to nginx user.

mkdir /var/run/nginx-cache/; chown nginx:nginx /var/run/nginx-cache/

nginx configurations

[ Disk cache or RAM cache conf]
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;

on the /etc/nginx/conf.d/domain.com.conf inside server {

location ~ .php$ {

   (...)

   set skip_cache 1;
   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;
}

quick test on the configurations

nginx -t

For this changes, a nginx RESTART is required.

service nginx restart

https://raazkumar.com/tutorials/nginx/nginx-fastcgi-cache/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.