16. Optimizing WordPress Performance
16.1. Introduction
Section titled “16.1. Introduction”After securing your WordPress installation, the next step is optimizing its performance. This section covers essential optimization techniques that will:
- Reduce server resource usage
- Improve page load times
- Handle more concurrent visitors
- Enhance overall user experience
16.2. Post Revisions Policy
Section titled “16.2. Post Revisions Policy”WordPress automatically saves revisions of your posts and pages as you edit them. While useful for content recovery, excessive revisions can bloat your database.
16.2.1. Limiting Post Revisions
Section titled “16.2.1. Limiting Post Revisions”You can control post revisions by adding a directive to your wp-config.php file:
# Navigate to your WordPress installationcd /var/www/example.com/sudo vim public_html/wp-config.phpAdd one of these lines to your configuration:
// Disable post revisions completelydefine('WP_POST_REVISIONS', false);
// Or limit to a specific number (recommended: 3-5)define('WP_POST_REVISIONS', 3);After making changes, restart PHP-FPM to clear the opcache:
sudo systemctl restart php8.3-fpm16.3. Setting the Max Memory Limit
Section titled “16.3. Setting the Max Memory Limit”WordPress can be memory-intensive, especially with complex themes and plugins. Increasing the memory limit can prevent “out of memory” errors.
16.3.1. PHP-FPM Pool Configuration
Section titled “16.3.1. PHP-FPM Pool Configuration”First, increase the memory limit in your PHP-FPM pool configuration:
# Edit your site's PHP-FPM pool configurationsudo vim /etc/php/8.3/fpm/pool.d/example.com.confFind and modify the memory limit line:
; Increase from default 32M to 256M;php_admin_value[memory_limit] = 32Mphp_admin_value[memory_limit] = 256M16.3.2. WordPress Configuration
Section titled “16.3.2. WordPress Configuration”Also set the memory limit in WordPress itself:
# Edit your WordPress configurationsudo vim /var/www/example.com/public_html/wp-config.phpAdd this line to your configuration:
/** Memory Limit for WordPress */define('WP_MEMORY_LIMIT', '256M');16.4. Replacing the WP CRON with a REAL CRON
Section titled “16.4. Replacing the WP CRON with a REAL CRON”WordPress uses a virtual cron system (WP-Cron) that triggers on page loads, which can be inefficient and unreliable. Replacing it with a real system cron job improves performance and reliability.
16.4.1. Disabling WP-Cron
Section titled “16.4.1. Disabling WP-Cron”First, disable the built-in WP-Cron:
# Edit your WordPress configurationsudo vim /var/www/example.com/public_html/wp-config.phpAdd this line to your configuration:
/** Disable the built-in cron system */define('DISABLE_WP_CRON', true);Restart PHP-FPM to apply changes:
sudo systemctl restart php8.3-fpm16.4.2. Setting Up a Real Cron Job
Section titled “16.4.2. Setting Up a Real Cron Job”Now, set up a system cron job to trigger WordPress cron events:
# Edit your crontabcrontab -eAdd this line to run WordPress cron every 15 minutes:
# Run WordPress cron every 15 minutes*/15 * * * * wget -q -O - https://example.com/wp-cron.php?doing_wp_cron >/dev/null 2>&116.5. Introduction to Caching
Section titled “16.5. Introduction to Caching”Caching is one of the most effective ways to improve WordPress performance. It reduces database queries and PHP execution by storing pre-generated content.
Types of WordPress Sites
For caching purposes, WordPress sites can be categorized as:
- Static sites: Content rarely changes and is the same for all users (blogs, brochure sites)
- Dynamic sites: Content changes frequently or is personalized for each user (e-commerce, membership sites)
16.5.1. Types of Caching
Section titled “16.5.1. Types of Caching”There are several layers of caching that can be implemented:
- Page Caching: Stores complete HTML pages
- Object Caching: Stores the results of database queries
- Browser Caching: Instructs browsers to store static assets
- CDN Caching: Distributes cached content across global servers
16.6. Implementing PHP OPcache
Section titled “16.6. Implementing PHP OPcache”PHP OPcache improves performance by storing precompiled script bytecode in memory, eliminating the need for PHP to load and parse scripts on each request.
16.6.1. Configuring OPcache
Section titled “16.6.1. Configuring OPcache”Edit your site’s PHP-FPM pool configuration:
# Navigate to the PHP-FPM pool directorycd /etc/php/8.3/fpm/pool.d/lssudo vim example.com.confAdd these OPcache settings to your configuration:
; OPCACHE CONFIGURATION - DEVELOPMENT SERVERphp_admin_flag[opcache.enabled] = 1php_admin_value[opcache.memory_consumption] = 256php_admin_value[opcache.interned_strings_buffer] = 32php_admin_value[opcache.max_accelerated_files] = 20000php_admin_flag[opcache.validate_timestamps] = 1php_admin_value[opcache.revalidate_freq] = 2php_admin_flag[opcache.validate_permission] = 1For production servers, use these settings instead:
; OPCACHE CONFIGURATION - PRODUCTION SERVERphp_admin_flag[opcache.enabled] = 1php_admin_value[opcache.memory_consumption] = 256php_admin_value[opcache.interned_strings_buffer] = 32php_admin_value[opcache.max_accelerated_files] = 20000php_admin_flag[opcache.validate_timestamps] = 0php_admin_flag[opcache.validate_permission] = 1Apply the changes:
sudo systemctl reload php8.3-fpm16.6.2. Determining Optimal max_accelerated_files
Section titled “16.6.2. Determining Optimal max_accelerated_files”To determine the optimal value for max_accelerated_files, count the PHP files in your installation:
cd /var/www/sudo find . -type f -print | grep php | wc -lSet max_accelerated_files to at least 20% higher than this number.
16.7. Caching a “Static” WordPress Site
Section titled “16.7. Caching a “Static” WordPress Site”For sites with content that doesn’t change frequently and is the same for all users, you can implement aggressive caching strategies. Choose one of the following methods based on your preferences and requirements.
16.7.1. Using FastCGI Caching
Section titled “16.7.1. Using FastCGI Caching”FastCGI caching is a server-level caching solution built into Nginx. It’s highly efficient and doesn’t require WordPress plugins.
16.7.1.1. Configuring Nginx for FastCGI Caching
Section titled “16.7.1.1. Configuring Nginx for FastCGI Caching”First, edit the main Nginx configuration:
cd /etc/nginxsudo vim nginx.confAdd the following directives to the http context (just above the “Virtual Host Configs” comment):
### FASTCGI CACHING# fastcgi_cache_path directive - PATH & NAME must be unique for each site# Add a new fastcgi_cache_path for each site and give a new keys_zone namefastcgi_cache_path /var/run/SITE levels=1:2 keys_zone=NAME:100m inactive=60m;# applied to all sitesfastcgi_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;16.7.1.2. Creating Cache Exclusion Rules
Section titled “16.7.1.2. Creating Cache Exclusion Rules”Create a file for cache exclusion rules:
cd /etc/nginx/includes/sudo vim fastcgi_cache_excludes.confAdd these rules to exclude dynamic content from caching:
# NGINX SKIP CACHE INCLUDE FILEset $skip_cache 0;# POST requests and urls with a query string should always go to PHPif ($request_method = POST) { set $skip_cache 1;}if ($query_string != "") { set $skip_cache 1;}# Don't cache uris containing the following segmentsif ($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 commentersif ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $skip_cache 1;}16.7.1.3. Updating Your Site’s Nginx Configuration
Section titled “16.7.1.3. Updating Your Site’s Nginx Configuration”Edit your site’s Nginx configuration:
cd /etc/nginx/sites-availablesudo vim example.com.confAdd these lines to your server block:
# Include cache exclusion rulesinclude /etc/nginx/includes/fastcgi_cache_excludes.conf;
# Add cache status headeradd_header X-FastCGI-Cache $upstream_cache_status;
# Add these directives to the PHP location blocklocation ~ \.php$ { # Existing PHP handling directives...
# FastCGI cache directives fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; fastcgi_cache NAME; fastcgi_cache_valid 60m;}16.7.1.4. Adding Cache Purge Capability
Section titled “16.7.1.4. Adding Cache Purge Capability”Add a location block for purging the cache:
# Add this to your server blocklocation ~ /purge(/.*) { fastcgi_cache_purge NAME "$scheme$request_method$host$1";}Apply the changes:
sudo nginx -tsudo systemctl reload nginx16.7.1.5. Testing the Cache
Section titled “16.7.1.5. Testing the Cache”Test if caching is working:
# First request should show MISScurl -I https://example.com
# Second request should show HITcurl -I https://example.comLook for the X-FastCGI-Cache header in the response.
16.7.1.6. Removing FastCGI Caching (If Needed)
Section titled “16.7.1.6. Removing FastCGI Caching (If Needed)”If you need to remove FastCGI caching:
cd /etc/nginx/sites-availablesudo vim example.com.confComment out or remove these lines:
# Remove or comment out these lines# include /etc/nginx/includes/fastcgi_cache_excludes.conf;# add_header X-FastCGI-Cache $upstream_cache_status;
# In the PHP location block, remove or comment:# fastcgi_cache_bypass $skip_cache;# fastcgi_no_cache $skip_cache;# fastcgi_cache NAME;# fastcgi_cache_valid 60m;
# Remove the purge location block# location ~ /purge(/.*) {# fastcgi_cache_purge NAME "$scheme$request_method$host$1";# }Apply the changes:
sudo nginx -tsudo systemctl reload nginxsudo systemctl restart php8.3-fpm16.7.2. Using WP Super Cache
Section titled “16.7.2. Using WP Super Cache”WP Super Cache is a popular WordPress caching plugin that generates static HTML files from your dynamic WordPress content.
16.7.2.1. Installing WP Super Cache
Section titled “16.7.2.1. Installing WP Super Cache”- Log in to your WordPress admin dashboard
- Go to Plugins > Add New
- Search for “WP Super Cache”
- Click “Install Now” and then “Activate”
16.7.2.2. Configuring Nginx for WP Super Cache
Section titled “16.7.2.2. Configuring Nginx for WP Super Cache”Create a configuration file for WP Super Cache:
cd /etc/nginx/includes/sudo vim wp_super_cache_excludes.confAdd these rules:
# WP Super Cache NGINX Cache Exclusions Rulesset $cache_uri $request_uri;
# POST requests and urls with a query string should always go to PHPif ($request_method = POST) { set $cache_uri 'null cache';}
if ($query_string != "") { set $cache_uri 'null cache';}
# Don't cache uris containing the following segmentsif ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") { set $cache_uri 'null cache';}
# Don't use the cache for logged in users or recent commentersif ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") { set $cache_uri 'null cache';}
# Use cached or actual file if they exists, otherwise pass request to WordPresslocation / { try_files /wp-content/cache/supercache/$http_host/$cache_uri/index-https.html $uri $uri/ /index.php?$args;}Update your site’s Nginx configuration:
cd /etc/nginx/sites-available/sudo vim example.com.confComment out the default location block and include the WP Super Cache configuration:
# Comment out the default location block# location / {# try_files $uri $uri/ /index.php$is_args$args;# }
# Include WP Super Cache configurationinclude /etc/nginx/includes/wp_super_cache_excludes.conf;Apply the changes:
sudo nginx -tsudo systemctl reload nginx16.7.2.3. Configuring WP Super Cache Plugin
Section titled “16.7.2.3. Configuring WP Super Cache Plugin”- Go to Settings > WP Super Cache in your WordPress admin
- Enable caching by selecting “Caching On”
- Under Advanced tab:
- Select “Use mod_rewrite to serve cache files”
- Check “Compress pages”
- Check “Don’t cache pages for known users”
- Check “Cache rebuild”
- Click “Update Status”
16.7.2.4. Removing WP Super Cache (If Needed)
Section titled “16.7.2.4. Removing WP Super Cache (If Needed)”If you need to remove WP Super Cache:
-
Deactivate and uninstall the plugin from WordPress admin
-
Restore your Nginx configuration:
cd /etc/nginx/sites-available/sudo vim example.com.confRemove the WP Super Cache include and restore the default location block:
# Remove this line# include /etc/nginx/includes/wp_super_cache_excludes.conf;
# Uncomment the default location blocklocation / { try_files $uri $uri/ /index.php$is_args$args;}Apply the changes:
sudo nginx -tsudo systemctl reload nginxsudo systemctl reload php8.3-fpm16.7.3. Using W3 Total Cache (W3TC)
Section titled “16.7.3. Using W3 Total Cache (W3TC)”W3 Total Cache is a comprehensive caching plugin with extensive configuration options for various caching methods.
16.7.3.1. Installing W3 Total Cache
Section titled “16.7.3.1. Installing W3 Total Cache”- Log in to your WordPress admin dashboard
- Go to Plugins > Add New
- Search for “W3 Total Cache”
- Click “Install Now” and then “Activate”
16.7.3.2. Preparing for W3TC Nginx Integration
Section titled “16.7.3.2. Preparing for W3TC Nginx Integration”Create a file for W3TC to write its Nginx rules:
cd /var/www/example.com/public_html/sudo touch nginx.confsudo chown username:username nginx.confsudo chmod 660 nginx.confAdd a security directive to prevent direct access to the nginx.conf file:
cd /etc/nginx/includes/sudo vim nginx_security_directives.confAdd this line:
location = /nginx.conf { deny all; }16.7.3.3. Creating W3TC Cache Exclusion Rules
Section titled “16.7.3.3. Creating W3TC Cache Exclusion Rules”Create a configuration file for W3TC cache exclusions:
cd /etc/nginx/includes/sudo vim w3tc_cache_excludes.confAdd these rules:
# ---------------------# W3 TOTAL CACHE EXCLUDES FILE# ---------------------set $cache_uri $request_uri;
# POST requests and urls with a query string should always go to PHPif ($request_method = POST) { set $cache_uri 'null cache';}if ($query_string != "") { set $cache_uri 'null cache';}# Don't cache uris containing the following segmentsif ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") { set $cache_uri 'null cache';}# Don't use the cache for logged in users or recent commentersif ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") { set $cache_uri 'null cache';}# Use cached or actual file if file exists, otherwise pass request to WordPresslocation / { try_files /wp-content/w3tc/pgcache/$cache_uri/_index.html $uri $uri/ /index.php?$args;}16.7.3.4. Updating Your Site’s Nginx Configuration
Section titled “16.7.3.4. Updating Your Site’s Nginx Configuration”Edit your site’s Nginx configuration:
cd /etc/nginx/sites-available/sudo vim example.com.confComment out the default location block and include the W3TC configurations:
# Comment out the default location block#location / {# try_files $uri $uri/ /index.php$is_args$args;#}
# Include W3TC configurationsinclude /etc/nginx/includes/w3tc_cache_excludes.conf;include /var/www/example.com/public_html/nginx.conf;Install the PHP Tidy extension required by W3TC:
sudo apt updatesudo apt install php8.3-tidyApply the changes:
sudo nginx -tsudo systemctl reload nginxsudo systemctl reload php8.3-fpm16.7.3.5. Configuring W3 Total Cache Plugin
Section titled “16.7.3.5. Configuring W3 Total Cache Plugin”- Go to Performance > General Settings in your WordPress admin
- Enable Page Cache and set Method to “Disk: Enhanced”
- Enable Browser Cache
- Enable Minify (optional)
- Under Performance > Page Cache:
- Check “Cache SSL”
- Check “Don’t cache pages for logged in users”
- Save all settings
16.7.3.6. Removing W3 Total Cache (If Needed)
Section titled “16.7.3.6. Removing W3 Total Cache (If Needed)”If you need to remove W3 Total Cache:
-
Deactivate and uninstall the plugin from WordPress admin
-
Restore your Nginx configuration:
cd /etc/nginx/sites-available/sudo vim example.com.confRemove the W3TC includes and restore the default location block:
# Restore the default location blocklocation / { try_files $uri $uri/ /index.php$is_args$args;}
# Remove these lines# include /etc/nginx/includes/w3tc_cache_excludes.conf;# include /var/www/example.com/public_html/nginx.conf;Remove W3TC files:
cd /var/www/example.com/public_html/wp-content/sudo rm -rf cache/ w3tc-config/
cd /var/www/example.com/public_html/sudo rm nginx.confApply the changes:
sudo systemctl reload php8.3-fpmsudo systemctl reload nginx16.8. Create a Persistent Object Cache using Redis
Section titled “16.8. Create a Persistent Object Cache using Redis”Object caching stores the results of database queries in memory, significantly reducing database load. Redis is an excellent choice for object caching due to its speed and persistence capabilities.
16.8.1. Installing Redis
Section titled “16.8.1. Installing Redis”Install Redis server and the PHP Redis extension:
sudo apt updatesudo apt install redis-server php8.3-redisVerify Redis is running:
sudo systemctl status redis-serverCheck the Redis log for any warnings or errors:
sudo cat /var/log/redis/redis-server.log16.8.2. Optimizing Redis Configuration
Section titled “16.8.2. Optimizing Redis Configuration”16.8.2.1. Fixing Memory Overcommit Warning
Section titled “16.8.2.1. Fixing Memory Overcommit Warning”If you see this warning in the Redis logs:
WARNING overcommit_memory is set to 0! Background save may fail under low memory condition.Fix it by creating a sysctl configuration file:
cd /etc/sysctl.d/sudo vim 11-redis.confAdd this line:
vm.overcommit_memory = 116.8.2.2. Setting Memory Limits
Section titled “16.8.2.2. Setting Memory Limits”Configure Redis memory limits:
sudo vim /etc/redis/redis.confFind and modify these settings:
# Set maximum memory to use (adjust based on your server resources)maxmemory 256mb
# Set eviction policy (LRU = Least Recently Used)maxmemory-policy allkeys-lruApply the changes:
sudo systemctl restart redis-server16.8.3. Configuring WordPress for Redis Object Caching
Section titled “16.8.3. Configuring WordPress for Redis Object Caching”16.8.3.1. Installing a Redis Object Cache Plugin
Section titled “16.8.3.1. Installing a Redis Object Cache Plugin”- Log in to your WordPress admin dashboard
- Go to Plugins > Add New
- Search for “Redis Object Cache”
- Install and activate the plugin by Till Krüss
16.8.3.2. Adding Redis Configuration to WordPress
Section titled “16.8.3.2. Adding Redis Configuration to WordPress”Edit your WordPress configuration:
cd /var/www/example.com/public_html/sudo vim wp-config.phpAdd these lines:
/** Redis Object Cache Settings */define('WP_CACHE_KEY_SALT', 'example.com');16.8.4. WooCommerce Considerations
Section titled “16.8.4. WooCommerce Considerations”If you’re running WooCommerce, you need special configuration to prevent caching of session data:
/** PREVENT REDIS CACHING WOO SESSION DATA */define('WP_REDIS_IGNORED_GROUPS', 'wc_session');Also, exclude these pages from page caching:
/cart//my-account//checkout/
And exclude these cookies from cache key generation:
woocommerce_cart_hashwoocommerce_items_in_cartwp_woocommerce_session_woocommerce_recently_viewedstore_notice[notice id]_wc_session_
16.9. Caching a “Dynamic” WordPress Site
Section titled “16.9. Caching a “Dynamic” WordPress Site”For sites with personalized content or frequent changes (e-commerce, membership sites), you need a different caching approach that combines page caching with object caching.
16.9.1. Introduction to Dynamic Site Caching
Section titled “16.9.1. Introduction to Dynamic Site Caching”Dynamic sites require:
- Object caching (Redis) to reduce database load
- Selective page caching that excludes personalized content
- Browser caching for static assets
16.9.2. WP Super Cache and Redis Combination
Section titled “16.9.2. WP Super Cache and Redis Combination”This approach uses WP Super Cache for page caching with careful exclusions and Redis for object caching.
- Follow the Redis installation steps from section 16.8
- Follow the WP Super Cache installation steps from section 16.7.2
- In WP Super Cache settings:
- Enable “Don’t cache pages for known users”
- Add custom exclusions for dynamic pages
16.9.3. W3TC and Redis Combination
Section titled “16.9.3. W3TC and Redis Combination”This approach uses W3 Total Cache with Redis as the object cache backend.
- Follow the Redis installation steps from section 16.8
- Follow the W3TC installation steps from section 16.7.3
- In W3TC Performance > General Settings:
- Set Object Cache method to “Redis”
- Configure Redis host (usually “localhost”) and port (6379)
- In Page Cache settings:
- Enable “Don’t cache pages for logged in users”
- Add custom exclusions for dynamic pages
16.10. Tuning PHP-FPM
Section titled “16.10. Tuning PHP-FPM”Proper PHP-FPM configuration is crucial for optimal WordPress performance.
16.10.1. Analyzing PHP-FPM Resource Usage
Section titled “16.10.1. Analyzing PHP-FPM Resource Usage”First, identify your PHP-FPM pool users:
# Display all PHP-FPM pool usernamesgrep -E '^\s*user\s*=' /etc/php/8.3/fpm/pool.d/*.conf | awk -F= '{print $2}' | xargs | tr ' ' '\n' | sort -uCalculate the average memory usage per PHP-FPM process:
# Replace POOL_USER with your actual pool usernameps -C php-fpm --user POOL_USER -o rss= | awk '{ sum += $1; count++ } END { if (count > 0) printf ("%d%s\n", sum/NR/1024,"M") }'Example:
ps -C php-fpm --user wordpress_user -o rss= | awk '{ sum += $1; count++ } END { if (count > 0) printf ("%d%s\n", sum/NR/1024,"M") }'16.10.2. Configuring OnDemand Process Manager
Section titled “16.10.2. Configuring OnDemand Process Manager”For most WordPress sites, the “ondemand” process manager is optimal:
cd /etc/php/8.3/fpm/pool.d/sudo vim example.com.confUpdate the process manager settings:
; Use ondemand process managerpm = ondemand
; Set max_children based on: (Total RAM - Reserved RAM) / Average Process Size; Example: (2048MB - 512MB) / 50MB = 30pm.max_children = 30
; Kill idle processes after 10 secondspm.process_idle_timeout = 10s
; Restart processes after handling 500 requestspm.max_requests = 500Apply the changes:
sudo systemctl reload php8.3-fpm16.10.3. Monitoring PHP-FPM Limits
Section titled “16.10.3. Monitoring PHP-FPM Limits”Check if your PHP-FPM processes are hitting limits:
sudo grep max_children /var/log/php8.3-fpm.logIf you see this warning, increase your pm.max_children setting:
WARNING: [pool www] server reached max_children setting (25), consider raising it16.11. Cloudflare Integration
Section titled “16.11. Cloudflare Integration”Cloudflare is a Content Delivery Network (CDN) and security service that can significantly improve WordPress performance and security.
16.11.1. Benefits of Cloudflare
Section titled “16.11.1. Benefits of Cloudflare”- Global CDN: Serves cached content from data centers around the world
- DDoS Protection: Mitigates distributed denial-of-service attacks
- Web Application Firewall: Blocks common attack vectors
- SSL/TLS: Provides free SSL certificates and optimized TLS connections
- Image Optimization: Automatically optimizes images for faster loading
16.11.2. Configuring Nginx for Cloudflare
Section titled “16.11.2. Configuring Nginx for Cloudflare”To ensure proper visitor IP logging when using Cloudflare, you need to configure Nginx to recognize Cloudflare’s IP addresses:
cd /etc/nginx/includessudo vim cloudflare_ip_list.confAdd the Cloudflare IP ranges:
# Cloudflare IP ranges# Last updated OCT 2022 - Check https://www.cloudflare.com/ips for updatesset_real_ip_from 173.245.48.0/20;set_real_ip_from 103.21.244.0/22;set_real_ip_from 103.22.200.0/22;set_real_ip_from 103.31.4.0/22;set_real_ip_from 141.101.64.0/18;set_real_ip_from 108.162.192.0/18;set_real_ip_from 190.93.240.0/20;set_real_ip_from 188.114.96.0/20;set_real_ip_from 197.234.240.0/22;set_real_ip_from 198.41.128.0/17;set_real_ip_from 162.158.0.0/15;set_real_ip_from 104.16.0.0/13;set_real_ip_from 104.24.0.0/14;set_real_ip_from 172.64.0.0/13;set_real_ip_from 131.0.72.0/22;set_real_ip_from 2400:cb00::/32;set_real_ip_from 2606:4700::/32;set_real_ip_from 2803:f800::/32;set_real_ip_from 2405:b500::/32;set_real_ip_from 2405:8100::/32;set_real_ip_from 2a06:98c0::/29;set_real_ip_from 2c0f:f248::/32;real_ip_header CF-Connecting-IP;Include this file in your site’s Nginx configuration:
cd /etc/nginx/sites-available/sudo vim example.com.confAdd this line to your server block:
include /etc/nginx/includes/cloudflare_ip_list.conf;Apply the changes:
sudo nginx -tsudo systemctl reload nginx16.11.3. Recommended Cloudflare Settings for WordPress
Section titled “16.11.3. Recommended Cloudflare Settings for WordPress”After signing up for Cloudflare and pointing your domain to their nameservers:
- SSL/TLS: Set to “Full” or “Full (Strict)” if you have a valid SSL certificate
- Caching:
- Set caching level to “Standard”
- Enable “Always Online”
- Set browser cache TTL to at least 4 hours
- Speed:
- Enable Auto Minify for HTML, CSS, and JavaScript
- Enable Brotli compression
- Enable Early Hints
- Enable Rocket Loader for JavaScript
- Page Rules:
- Create a rule to bypass cache for
/wp-admin/* - Create a rule to bypass cache for login and checkout pages
- Create a rule to bypass cache for
16.12. Conclusion
Section titled “16.12. Conclusion”Optimizing WordPress performance is a multi-layered approach that involves:
- Server-level optimizations: PHP-FPM tuning, OPcache configuration
- WordPress configuration: Post revisions, memory limits, WP-Cron
- Caching strategies: Page caching, object caching, browser caching
- Content delivery: CDN integration with Cloudflare
Remember that performance optimization is an ongoing process. Regularly monitor your site’s performance using tools like:
- Google PageSpeed Insights
- GTmetrix
- WebPageTest
Adjust your optimization strategy based on your specific WordPress site’s needs and traffic patterns.