Остання активність 1 month ago

Версія cd4c6e32e9e2b5d222cf99aa87f28a8a6a6ce60b

wordpress + fastcgi cache.md Неформатований

Absolutely! Implementing full-page caching with Nginx is one of the most powerful ways to dramatically boost WordPress performance, especially for high-traffic sites like yours (1k sustained users).

This guide assumes you're using:

  • Nginx (not Apache)
  • PHP-FPM
  • WordPress
  • Linux (Ubuntu/Debian)

🔧 Step-by-Step: Full Page Caching with Nginx (FastCGI Cache)


✅ 1. Prepare Directories

Create a directory for cached pages:

sudo mkdir -p /var/cache/nginx/wordpress
sudo chown -R www-data:www-data /var/cache/nginx

🧠 2. Understand the Cache Key

Nginx will cache based on:

  • URI
  • Host
  • Request method (GET/POST)
  • Cookies (important for logged-in vs public users)

⚙️ 3. Nginx Configuration

Open your Nginx server block (e.g., /etc/nginx/sites-available/yourdomain.com) and make the following changes:

a. Enable the cache zone

Add this at the top of the file (outside the server block):

fastcgi_cache_path /var/cache/nginx/wordpress levels=1:2 keys_zone=WORDPRESS:100m inactive=60m use_temp_path=off;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

b. Inside your server {} block

Insert the following:

set $no_cache 0;

# Don't cache POST requests or certain URIs
if ($request_method = POST) {
    set $no_cache 1;
}
if ($query_string != "") {
    set $no_cache 1;
}

# Don't cache logged-in users or recent commenters
if ($http_cookie ~* "comment_author_|wordpress_logged_in_|wp-postpass_") {
    set $no_cache 1;
}

c. Inside your location ~ \.php$ block

Modify it to include FastCGI caching:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;

    fastcgi_cache WORDPRESS;
    fastcgi_cache_bypass $no_cache;
    fastcgi_no_cache $no_cache;
    fastcgi_cache_valid 200 301 302 60m;

    add_header X-FastCGI-Cache $upstream_cache_status;

    fastcgi_cache_use_stale error timeout invalid_header http_500;
}

This will:

  • Cache pages for 60 minutes
  • Avoid caching for logged-in users and POST requests
  • Add a response header like X-FastCGI-Cache: HIT or MISS

💡 4. Purge Cache (Optional)

Nginx FastCGI Cache does not automatically purge pages when WordPress content is updated.

Options:

  • Use nginx-cache-purge module (requires recompiling Nginx)
  • Use a plugin like Nginx Cache which works well with Redis or FastCGI:
    define('RT_WP_NGINX_HELPER_CACHE_PATH', '/var/cache/nginx/wordpress');
    define('RT_WP_NGINX_HELPER_LOG_PATH', '/var/log/nginx/wordpress-helper.log');
    

✅ 5. Reload Nginx

sudo nginx -t
sudo systemctl reload nginx

🧪 6. Test It

  • Open a browser → Visit your homepage
  • Run:
    curl -I https://yourdomain.com
    

You should see:

X-FastCGI-Cache: HIT

Try again after logging in to WordPress. You should see:

X-FastCGI-Cache: BYPASS

🔍 7. Optional: Clear Cache Manually

sudo rm -rf /var/cache/nginx/wordpress/*

🚀 Performance Gains

Without Cache With Nginx FastCGI Cache
300–800ms TTFB 50–100ms TTFB
High CPU/PHP usage Very low CPU usage
DB load under traffic Minimal DB activity

Would you like a ready-to-use Nginx config block or a purge solution recommendation next?