xl ревизій цього gist 1 month ago. До ревизії
1 file changed, 1 deletion
wordpress + fastcgi cache.md
| @@ -6,7 +6,6 @@ This guide assumes you're using: | |||
| 6 | 6 | - PHP-FPM | |
| 7 | 7 | - WordPress | |
| 8 | 8 | - Linux (Ubuntu/Debian) | |
| 9 | - | test | |
| 10 | 9 | --- | |
| 11 | 10 | ||
| 12 | 11 | ## 🔧 Step-by-Step: Full Page Caching with Nginx (FastCGI Cache) | |
xl ревизій цього gist 1 month ago. До ревизії
1 file changed, 1 insertion, 1 deletion
wordpress + fastcgi cache.md
| @@ -6,7 +6,7 @@ This guide assumes you're using: | |||
| 6 | 6 | - PHP-FPM | |
| 7 | 7 | - WordPress | |
| 8 | 8 | - Linux (Ubuntu/Debian) | |
| 9 | - | ||
| 9 | + | test | |
| 10 | 10 | --- | |
| 11 | 11 | ||
| 12 | 12 | ## 🔧 Step-by-Step: Full Page Caching with Nginx (FastCGI Cache) | |
xl ревизій цього gist 3 months ago. До ревизії
Без змін
xl ревизій цього gist 8 months ago. До ревизії
1 file changed, 4 deletions
wordpress + fastcgi cache.md
| @@ -153,7 +153,3 @@ sudo rm -rf /var/cache/nginx/wordpress/* | |||
| 153 | 153 | | 300–800ms TTFB | 50–100ms TTFB | | |
| 154 | 154 | | High CPU/PHP usage | Very low CPU usage | | |
| 155 | 155 | | DB load under traffic| Minimal DB activity | | |
| 156 | - | ||
| 157 | - | --- | |
| 158 | - | ||
| 159 | - | Would you like a **ready-to-use Nginx config block** or a **purge solution** recommendation next? | |
xl ревизій цього gist 8 months ago. До ревизії
1 file changed, 159 insertions
wordpress + fastcgi cache.md (файл створено)
| @@ -0,0 +1,159 @@ | |||
| 1 | + | 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). | |
| 2 | + | ||
| 3 | + | This guide assumes you're using: | |
| 4 | + | ||
| 5 | + | - Nginx (not Apache) | |
| 6 | + | - PHP-FPM | |
| 7 | + | - WordPress | |
| 8 | + | - Linux (Ubuntu/Debian) | |
| 9 | + | ||
| 10 | + | --- | |
| 11 | + | ||
| 12 | + | ## 🔧 Step-by-Step: Full Page Caching with Nginx (FastCGI Cache) | |
| 13 | + | ||
| 14 | + | --- | |
| 15 | + | ||
| 16 | + | ### ✅ 1. **Prepare Directories** | |
| 17 | + | ||
| 18 | + | Create a directory for cached pages: | |
| 19 | + | ||
| 20 | + | ```bash | |
| 21 | + | sudo mkdir -p /var/cache/nginx/wordpress | |
| 22 | + | sudo chown -R www-data:www-data /var/cache/nginx | |
| 23 | + | ``` | |
| 24 | + | ||
| 25 | + | --- | |
| 26 | + | ||
| 27 | + | ### 🧠 2. **Understand the Cache Key** | |
| 28 | + | ||
| 29 | + | Nginx will cache based on: | |
| 30 | + | - URI | |
| 31 | + | - Host | |
| 32 | + | - Request method (GET/POST) | |
| 33 | + | - Cookies (important for logged-in vs public users) | |
| 34 | + | ||
| 35 | + | --- | |
| 36 | + | ||
| 37 | + | ### ⚙️ 3. **Nginx Configuration** | |
| 38 | + | ||
| 39 | + | Open your Nginx server block (e.g., `/etc/nginx/sites-available/yourdomain.com`) and make the following changes: | |
| 40 | + | ||
| 41 | + | #### a. Enable the cache zone | |
| 42 | + | ||
| 43 | + | Add this at the top of the file (outside the server block): | |
| 44 | + | ||
| 45 | + | ```nginx | |
| 46 | + | fastcgi_cache_path /var/cache/nginx/wordpress levels=1:2 keys_zone=WORDPRESS:100m inactive=60m use_temp_path=off; | |
| 47 | + | fastcgi_cache_key "$scheme$request_method$host$request_uri"; | |
| 48 | + | ``` | |
| 49 | + | ||
| 50 | + | #### b. Inside your `server {}` block | |
| 51 | + | ||
| 52 | + | Insert the following: | |
| 53 | + | ||
| 54 | + | ```nginx | |
| 55 | + | set $no_cache 0; | |
| 56 | + | ||
| 57 | + | # Don't cache POST requests or certain URIs | |
| 58 | + | if ($request_method = POST) { | |
| 59 | + | set $no_cache 1; | |
| 60 | + | } | |
| 61 | + | if ($query_string != "") { | |
| 62 | + | set $no_cache 1; | |
| 63 | + | } | |
| 64 | + | ||
| 65 | + | # Don't cache logged-in users or recent commenters | |
| 66 | + | if ($http_cookie ~* "comment_author_|wordpress_logged_in_|wp-postpass_") { | |
| 67 | + | set $no_cache 1; | |
| 68 | + | } | |
| 69 | + | ``` | |
| 70 | + | ||
| 71 | + | #### c. Inside your `location ~ \.php$` block | |
| 72 | + | ||
| 73 | + | Modify it to include FastCGI caching: | |
| 74 | + | ||
| 75 | + | ```nginx | |
| 76 | + | location ~ \.php$ { | |
| 77 | + | include snippets/fastcgi-php.conf; | |
| 78 | + | fastcgi_pass unix:/run/php/php8.2-fpm.sock; | |
| 79 | + | ||
| 80 | + | fastcgi_cache WORDPRESS; | |
| 81 | + | fastcgi_cache_bypass $no_cache; | |
| 82 | + | fastcgi_no_cache $no_cache; | |
| 83 | + | fastcgi_cache_valid 200 301 302 60m; | |
| 84 | + | ||
| 85 | + | add_header X-FastCGI-Cache $upstream_cache_status; | |
| 86 | + | ||
| 87 | + | fastcgi_cache_use_stale error timeout invalid_header http_500; | |
| 88 | + | } | |
| 89 | + | ``` | |
| 90 | + | ||
| 91 | + | This will: | |
| 92 | + | - Cache pages for 60 minutes | |
| 93 | + | - Avoid caching for logged-in users and POST requests | |
| 94 | + | - Add a response header like `X-FastCGI-Cache: HIT` or `MISS` | |
| 95 | + | ||
| 96 | + | --- | |
| 97 | + | ||
| 98 | + | ### 💡 4. **Purge Cache (Optional)** | |
| 99 | + | ||
| 100 | + | Nginx FastCGI Cache does not automatically purge pages when WordPress content is updated. | |
| 101 | + | ||
| 102 | + | **Options:** | |
| 103 | + | ||
| 104 | + | - Use [`nginx-cache-purge`](https://github.com/FRiCKLE/ngx_cache_purge) module (requires recompiling Nginx) | |
| 105 | + | - Use a plugin like [**Nginx Cache**](https://wordpress.org/plugins/nginx-helper/) which works well with Redis or FastCGI: | |
| 106 | + | ```php | |
| 107 | + | define('RT_WP_NGINX_HELPER_CACHE_PATH', '/var/cache/nginx/wordpress'); | |
| 108 | + | define('RT_WP_NGINX_HELPER_LOG_PATH', '/var/log/nginx/wordpress-helper.log'); | |
| 109 | + | ``` | |
| 110 | + | ||
| 111 | + | --- | |
| 112 | + | ||
| 113 | + | ### ✅ 5. **Reload Nginx** | |
| 114 | + | ||
| 115 | + | ```bash | |
| 116 | + | sudo nginx -t | |
| 117 | + | sudo systemctl reload nginx | |
| 118 | + | ``` | |
| 119 | + | ||
| 120 | + | --- | |
| 121 | + | ||
| 122 | + | ## 🧪 6. **Test It** | |
| 123 | + | ||
| 124 | + | - Open a browser → Visit your homepage | |
| 125 | + | - Run: | |
| 126 | + | ```bash | |
| 127 | + | curl -I https://yourdomain.com | |
| 128 | + | ``` | |
| 129 | + | ||
| 130 | + | You should see: | |
| 131 | + | ```http | |
| 132 | + | X-FastCGI-Cache: HIT | |
| 133 | + | ``` | |
| 134 | + | ||
| 135 | + | Try again after logging in to WordPress. You should see: | |
| 136 | + | ```http | |
| 137 | + | X-FastCGI-Cache: BYPASS | |
| 138 | + | ``` | |
| 139 | + | ||
| 140 | + | --- | |
| 141 | + | ||
| 142 | + | ## 🔍 7. **Optional: Clear Cache Manually** | |
| 143 | + | ```bash | |
| 144 | + | sudo rm -rf /var/cache/nginx/wordpress/* | |
| 145 | + | ``` | |
| 146 | + | ||
| 147 | + | --- | |
| 148 | + | ||
| 149 | + | ## 🚀 Performance Gains | |
| 150 | + | ||
| 151 | + | | Without Cache | With Nginx FastCGI Cache | | |
| 152 | + | |---------------------|--------------------------| | |
| 153 | + | | 300–800ms TTFB | 50–100ms TTFB | | |
| 154 | + | | High CPU/PHP usage | Very low CPU usage | | |
| 155 | + | | DB load under traffic| Minimal DB activity | | |
| 156 | + | ||
| 157 | + | --- | |
| 158 | + | ||
| 159 | + | Would you like a **ready-to-use Nginx config block** or a **purge solution** recommendation next? | |