Introduction
If you want to use Caddy 2 for a WordPress site and you are using a static cache that generates html file on the server, you can use a config to bypass entirely WordPress and php-fpm. That way, you will really quickly serve static files to your users, without cluttering the server.
That solution is common with most WordPress static cache plugins. In order to work, you must define some exceptions for all requests that need to hit WordPress and not the cache. For instance, if you are the administrator of the site and if you are connected, you don’t want to see the cached pages. Likewise, if you are in the admin interface of WP, you should go directly to php-fpm without going to the static cache first.
Choose the correct configuration depending on the WordPress plugin you’re using. And if your plugin is not on this list, you can use the comment to offer your configuration or request for help.
Cache enabler
The configuration has been tried by @nicolinux and it is working.
You can use the plugin default settings.
domain.com {
# Change the path here according to your setup
root * /var/www/domain.com
encode zstd gzip
file_server
@cache {
not header_regexp Cookie "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in"
not path_regexp "(/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)"
not method POST
not expression {query} != ''
}
route @cache {
try_files /wp-content/cache/cache-enabler/{host}{uri}/https-index.html /wp-content/cache/cache-enabler/{host}{uri}/index.html {path} {path}/index.php?{query}
}
# Change the path here according to your server
php_fastcgi unix//run/php/php7.4-fpm.sock
}
WP Super Cache
The configuration has been tried by @Kyle_Chen and it is working.
Configure the plugin to use the expert mode and ignore the alert about .htaccess
. You can find additional details in this other post.
domain.com {
# Change the path here according to your setup
root * /var/www/domain.com
encode zstd gzip
file_server
@cache {
not header_regexp Cookie "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in"
not path_regexp "(/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)"
not method POST
not expression {query} != ''
}
route @cache {
try_files /wp-content/cache/supercache/{host}{uri}/index-https.html /wp-content/cache/supercache/{host}{uri}/index.html {path} {path}/index.php?{query}
}
# Change the path here according to your server
php_fastcgi unix//run/php/php7.4-fpm.sock
}
WP-Rocket
This configuration was never tried. Let us know if it works in the comments.
The default configuration should work.
This configuration does not use the separate mobile cache that WP-Rocket creates. Help would be appreciated to use both with Caddy 2. Here’s the nginx file to translate.
domain.com {
# Change the path here according to your setup
root * /var/www/domain.com
encode zstd gzip
file_server
@cache {
not header_regexp Cookie "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in"
not path_regexp "(/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)"
not method POST
not expression {query} != ''
}
route @cache {
try_files /wp-content/cache/wp-rocket/{host}{uri}/index-https.html /wp-content/cache/wp-rocket/{host}{uri}/index.html {path} {path}/index.php?{query}
}
# Change the path here according to your server
php_fastcgi unix//run/php/php7.4-fpm.sock
}
WP Fastest cache
This configuration was never tried. Let us know if it works in the comments.
The default configuration should work.
This configuration does not use the separate mobile cache that WP Fastest cache premium creates. Help would be appreciated to use both with Caddy 2. Here’s the nginx file to translate.
domain.com {
# Change the path here according to your setup
root * /var/www/domain.com
encode zstd gzip
file_server
@cache {
not header_regexp Cookie "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in"
not path_regexp "(/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)"
not method POST
not expression {query} != ''
}
route @cache {
try_files /wp-content/cache/all/{uri}/index.html {path} {path}/index.php?{query}
}
# Change the path here according to your server
php_fastcgi unix//run/php/php7.4-fpm.sock
}
W3 Total Cache
This configuration was never tried. Let us know if it works in the comments.
Configure the plugin to activate the static page on disk with the advanced option.
This configuration does not use the multiple cache versions that W3 Total Cache creates. Help would be appreciated to use both with Caddy 2. Here’s the nginx file to translate.
domain.com {
# Change the path here according to your setup
root * /var/www/domain.com
encode zstd gzip
file_server
@cache {
not header_regexp Cookie "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in"
not path_regexp "(/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)"
not method POST
not expression {query} != ''
}
route @cache {
try_files /wp-content/cache/page_enhanced/{host}{uri}/_index_ssl.html /wp-content/cache/page_enhanced/{host}{uri}/_index.html {path} {path}/index.php?{query}
}
# Change the path here according to your server
php_fastcgi unix//run/php/php7.4-fpm.sock
}
Wiki changelog
- 17 May 2020 : first version ;
- 29 June 2020 : Caddy 2.1 warning and W3 Total cache untested configuration ;
- 1 July 2020 : removed the Caddy 2.1 warning, use at least Caddy 2.1.1 ;
- 25 August 2020 : fixed the Cache Enabler snippet for the latest versions of the plugin (source).