I plugin di caching come WP Super Cache o W3 Total Cache aiutano, ma per siti ad alto traffico servono soluzioni server-side: Redis Object Cache, NGINX FastCGI Cache e Varnish.
La Piramide del Caching
┌─────────────────────────────────┐
│ CDN (Cloudflare, etc) │ ← Cache globale statica
├─────────────────────────────────┤
│ Full Page Cache (Varnish) │ ← Cache HTML completo
├─────────────────────────────────┤
│ FastCGI/NGINX Proxy Cache │ ← Cache a livello server
├─────────────────────────────────┤
│ Object Cache (Redis/Memcached)│ ← Cache query database
├─────────────────────────────────┤
│ Transient API WordPress │ ← Cache applicativo
├─────────────────────────────────┤
│ Browser Cache (headers) │ ← Cache lato client
└─────────────────────────────────┘
1. Redis Object Cache
L’Object Cache memorizza risultati di query database, riducendo drasticamente il carico su MySQL.
Installazione Redis
# Ubuntu/Debian
sudo apt install redis-server
# Verifica installazione
redis-cli ping
# Output: PONG
# Configura Redis per performance
sudo nano /etc/redis/redis.conf
# maxmemory 256mb
# maxmemory-policy allkeys-lru
Configurazione WordPress
// wp-config.php - PRIMA di require wp-settings.php
define( 'wp_REDIS_HOST', '127.0.0.1' );
define( 'wp_REDIS_PORT', 6379 );
define( 'wp_REDIS_DATABASE', 0 );
define( 'wp_REDIS_PREFIX', 'wpsite_' ); // Unico per ogni sito
// Opzionale: password se Redis è esposto
define( 'wp_REDIS_PASSWORD', 'your-secure-password' );
Attivazione con WP-CLI
# Installa e attiva Redis Object Cache plugin
wp plugin install redis-cache --activate
# Abilita object-cache.php drop-in
wp redis enable
# Verifica stato
wp redis status
# Output:
# Status: Connected
# Client: phpredis
# Host: 127.0.0.1
# Database: 0
2. NGINX FastCGI Cache
Più veloce di qualsiasi plugin PHP perché la cache è servita direttamente da NGINX senza toccare PHP.
Configurazione NGINX
# /etc/nginx/conf.d/fastcgi-cache.conf
# Definisci la cache zone
fastcgi_cache_path /var/cache/nginx levels=1:2
keys_zone=WORDPRESS:100m inactive=60m max_size=1g;
# Variabili per skip cache
map $request_uri $skip_cache {
default 0;
~*/wp-admin/ 1;
~*/wp-login.php 1;
~*preview=true 1;
}
map $http_cookie $skip_cache {
default 0;
~*wordpress_logged_in 1;
~*comment_author 1;
~*woocommerce_cart 1;
}
Server Block
# Nel server block del sito
location ~ \.php$ {
# FastCGI cache
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 60m;
fastcgi_cache_valid 404 1m;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
# Header per debug
add_header X-Cache-Status $upstream_cache_status;
# Standard FastCGI config
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
Cache Purge
# Purge manuale
sudo rm -rf /var/cache/nginx/*
sudo systemctl reload nginx
# Purge automatico via plugin NGINX Helper
wp plugin install nginx-helper --activate
3. Varnish (per Traffico Estremo)
Varnish è un reverse proxy cache che può servire migliaia di richieste al secondo.
# Architettura
[Client] → [Varnish :80] → [NGINX :8080] → [PHP-FPM] → [MySQL]
↓ cache hit
[Risposta immediata ~1ms]
# Verifica cache hit
curl -I https://example.com
# X-Varnish-Cache: HIT
4. Browser Cache Headers
# .htaccess o NGINX config
location ~* \.(css|js|jpg|jpeg|png|gif|webp|svg|woff2)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# Per HTML dinamico
location ~ \.php$ {
add_header Cache-Control "no-cache, must-revalidate";
}
5. WooCommerce: Cache Selettivo
E-commerce richiede attenzione: non cachare carrello e checkout!
# NGINX - Escludi pagine WooCommerce
map $request_uri $skip_cache {
~*/cart/ 1;
~*/checkout/ 1;
~*/my-account/ 1;
~*add-to-cart 1;
~*wc-ajax 1;
}
map $http_cookie $skip_cache {
~*woocommerce_cart_hash 1;
~*woocommerce_items_in_cart 1;
}
Benchmark: Impatto Reale
| Setup | TTFB | RPS |
|---|---|---|
| Senza cache | 800ms | 50 |
| Plugin cache (WP Super Cache) | 200ms | 200 |
| Redis Object Cache | 150ms | 400 |
| NGINX FastCGI Cache | 15ms | 5000 |
| Varnish | 2ms | 20000+ |
Il tuo hosting supporta Redis e NGINX cache? Molti shared hosting no. Contattaci per un assessment infrastrutturale e configurazione ottimale.