WordPress W3 Total Cache Configuration

Complete Guide for Dynamic Sites with NGINX and Redis

Introduction

Configuring a caching plugin for a dynamic WordPress site follows a similar process to static sites, with critical differences in cache exclusions and redirect management. This guide provides comprehensive instructions for implementing W3 Total Cache on dynamic WordPress sites with WooCommerce integration, ensuring accurate dynamic content delivery while maintaining optimal performance.

Key Difference: Dynamic sites require careful management of cache exclusions to maintain accuracy of real-time content such as shopping carts, user sessions, and checkout processes.

W3 Total Cache Implementation Flow

Server-Side Configuration
WordPress Configuration
Plugin Installation
Cache Exclusions Setup
Testing & Optimization

Part 1: Server-Side Preparation

1Create W3TC Configuration File

Navigate to your site's document root and create the required configuration file:

cd /var/www/example.com/public_html
sudo touch nginx.conf

The touch command creates an empty file with zero size.

2Set File Ownership

Assign the correct ownership to the configuration file using your PHP pool user:

sudo chown username:username nginx.conf

Replace 'username' with your actual PHP-FPM pool username.

3Set File Permissions

Configure appropriate permissions for security:

sudo chmod 660 nginx.conf
sudo ls -l nginx.conf
Permission Explanation:
  • 6 (Owner): Read and Write
  • 6 (Group): Read and Write
  • 0 (Others): No permissions

4Configure NGINX Security Directives

Prevent direct access to the nginx.conf file:

cd /etc/nginx/includes/
sudo nano nginx_security_directives.conf

Add or uncomment the following directive:

location = /nginx.conf { deny all; }

5Verify W3TC Cache Excludes File

Confirm the existence of the W3TC excludes configuration:

cd /etc/nginx/includes/
cat w3tc_cache_excludes.conf
Note: The w3tc_cache_excludes.conf file configuration remains identical for both static and dynamic WordPress sites.

6Update NGINX Server Block

Modify your site's NGINX configuration:

cd /etc/nginx/sites-available/
sudo nano example.com.conf

Comment out the default location context:

#location / {
# try_files $uri $uri/ /index.php$is_args$args;
#}

Add the following include directives above the PHP processing location block:

include /etc/nginx/includes/w3tc_cache_excludes.conf;
include /var/www/example.com/public_html/nginx.conf;

7Test and Reload NGINX

Validate the configuration and apply changes:

sudo nginx -t
sudo systemctl reload nginx

Part 2: WordPress Configuration

8Configure Redis Cache Key

Set up the cache key salt in wp-config.php:

cd /var/www/example.com/public_html/
sudo nano wp-config.php

Add the following line in the salts section:

define('WP_CACHE_KEY_SALT', 'example.com');

9Clear PHP OpCache

Reload PHP-FPM to clear the opcache:

sudo systemctl reload php8.3-fpm

Part 3: Plugin Installation

10Install W3 Total Cache

Navigate to the WordPress dashboard and install the plugin:

  1. Go to Plugins → Add New
  2. Search for "W3 Total Cache"
  3. Click Install Now
Installation Error: If you encounter "Directory folder already exists" error, perform the following cleanup:
cd /var/www/example.com/public_html/wp-content/plugins
sudo rm -rf w3-total-cache
cd ../../
sudo systemctl reload php8.3-fpm

Then retry the installation from the WordPress dashboard.

11Activate the Plugin

After successful installation:

  1. Go to Installed Plugins
  2. Click Activate for W3 Total Cache
  3. You'll see a message: "NGINX rules have been updated"
sudo nginx -t
sudo systemctl reload nginx

Part 4: Initial Configuration Wizard

12Run Setup Guide

Navigate to Performance → Setup Guide in WordPress dashboard.

Page Cache Configuration

Select Disk: Enhanced as the page cache method and proceed to the next step.

Database Cache Configuration

Recommendation: Leave database cache set to None. Use Redis or Memcached for database caching only if your database engine is slower than disk caching. For most modern setups, the database engine is faster.

Object Cache Configuration

Select Redis for object caching.

Note: Redis object caching may be slightly slower than disk caching on sites with minimal content, but it scales better with traffic.

Browser Cache Configuration

Enable browser cache for optimal client-side performance.

WebP Converter (Optional)

Enable if you want to automatically convert images to WebP format for better compression and faster loading.

Lazy Loading (Recommended)

Enable lazy loading for image-heavy sites to improve initial page load times.

Complete Setup

Click All Done and then Empty Cache.

sudo nginx -t
sudo systemctl reload nginx

Part 5: WooCommerce Cache Exclusions

Critical for Dynamic Sites: Proper cache exclusions ensure that dynamic content (shopping carts, checkout, user accounts) remains accurate and functional.

13Configure Page Cache Exclusions

Navigate to Performance → Page Cache → Advanced.

Never Cache the Following Pages:

Add the following URLs to exclude from page caching:

/cart/
/my-account/
/checkout/

Rejected Cookies:

Add WooCommerce-specific cookies to prevent caching for users with these cookies:

Cookie Name Purpose
woocommerce_cart_hash Tracks cart content changes
woocommerce_items_in_cart Indicates items in cart
wp_woocommerce_session_ Maintains user session
woocommerce_recently_viewed Tracks recently viewed products
store_notice[notice id] Store notification dismissal

Click Save Settings, then Empty Cache.

sudo nginx -t
sudo systemctl reload nginx

14Configure Object Cache Exclusions

Navigate to Performance → Object Cache.

Test Redis Connection

Click Test to verify W3TC can connect to Redis server. You should see "Test Passed".

Non-Persistent Groups

Scroll to the Non-Persistent Groups section and add:

wc_session
Important: Adding 'wc_session' to non-persistent groups ensures all WooCommerce session data is excluded from Redis caching, maintaining accurate cart and checkout functionality.

Alternatively, configure this in wp-config.php:

define('WP_REDIS_IGNORED_GROUPS', 'wc_session');

Click Save Settings, then Empty Object Cache.

15Configure Minification

Navigate to Performance → Minify.

Ignored Comment Stems

Verify that the following is present in the ignored comment stems field:

mfunc
mfunc Explanation: 'mfunc' stands for "marker function". It's a special directive used by W3 Total Cache to embed dynamic PHP code within otherwise static cached pages. Excluding it from minification ensures that dynamic PHP code remains functional.

Click Save Settings.

Part 6: Testing and Optimization

Performance Testing Workflow

Clear All Caches
Test Static Pages
Test Dynamic Pages (Cart, Checkout)
Monitor Server Resources
Adjust Settings Based on Results

Testing Checklist

Part 7: Uninstalling W3 Total Cache

Important: Follow these steps carefully to ensure complete removal and prevent site errors.

16Purge All Caches

Navigate to Performance → Purge All Caches in WordPress dashboard.

17Disable All Caching Features

Go to Performance → General Settings and disable:

Click Save All Settings, then Purge All Caches again.

18Deactivate and Delete Plugin

  1. Go to Plugins → Installed Plugins
  2. Click Deactivate for W3 Total Cache
  3. Click Delete for W3 Total Cache

19Remove Server-Side Files

cd /var/www/example.com/public_html/wp-content/
sudo rm -rf cache/ w3tc-config/
cd ../
sudo rm nginx.conf

20Update wp-config.php

sudo nano wp-config.php

Remove the following lines:

define('WP_CACHE_KEY_SALT', 'example.com');
define('WP_REDIS_IGNORED_GROUPS', 'wc_session');

21Update NGINX Security Directives

cd /etc/nginx/includes/
sudo nano nginx_security_directives.conf

Comment out the location directive:

#location = /nginx.conf { deny all; }

22Update NGINX Server Block

cd /etc/nginx/sites-available/
sudo nano example.com.conf

Uncomment the default location context:

location / {
try_files $uri $uri/ /index.php$is_args$args;
}

Remove the W3TC include directives:

# Remove these lines:
# include /etc/nginx/includes/w3tc_cache_excludes.conf;
# include /var/www/example.com/public_html/nginx.conf;

23Test and Reload Services

sudo nginx -t
sudo systemctl reload nginx
sudo systemctl reload php8.3-fpm
Uninstallation Complete! W3 Total Cache and all associated configurations have been removed from your server. Your site will now serve uncached content.

Best Practices and Recommendations

Performance Optimization Tips

Common Issues and Solutions

Issue Solution
Cart not updating Verify WooCommerce cookies are properly excluded
Redis connection failed Check Redis service status and memory allocation
Old content displaying Purge all caches and check cache exclusion rules
NGINX test failure Review configuration syntax and include paths
Plugin installation error Remove existing w3-total-cache directory manually

Security Considerations

Final Note: W3 Total Cache is a powerful but complex plugin. The configuration presented here provides a solid foundation for dynamic WordPress sites with WooCommerce. Always test thoroughly in a staging environment before deploying to production, and continue to monitor and optimize based on your specific site requirements and traffic patterns.