Today we're going to give a high-level overview of how to achieve Nginx-like microcaching on an Apache server. Be aware that this is a highly advanced feature and you should be comfortable with technical details and getting your hands dirty, especially considering the high chance it may cause problems for you! Be warned.
First of all, you'll need to install and load in the "mod_cache" and "mod_cache_disk" modules into Apache 2.4+, and if you haven't got "mod_deflate" installed then you should also install that too, since our code example works well with mod_deflate.
Next, we're going to provide you a pre-compiled code block that is tailored to Wordpress sites. This block can be inserted into httpd.conf's default server config, or inside a particular vhost if you're only looking to apply this to one domain. If you're using cPanel + WHM, you're best off using the Include Editor in Apache Configuration of WHM instead.
<IfModule mod_cache.c>
CacheQuickHandler Off
CacheHeader Off
CacheDetailHeader OffCacheIgnoreNoLastMod On
CacheDefaultExpire 180
CacheMinExpire 5
CacheStoreExpired Off
CacheLastModifiedFactor 0.5CacheIgnoreCacheControl On
CacheIgnoreHeaders Set-Cookie Cookie
CacheLock on
CacheLockMaxAge 5
CacheDisable /wp-admin
CacheDisable /wp-content
CacheDisable /wp-login.php
CacheDisable /wp-cron.phpSetOutputFilter CACHE
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/rss+xml text/xml image/svg+xml<IfModule mod_cache_disk.c>
CacheRoot /var/cache/apache2/proxy/
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
CacheMaxFileSize 20000000
</IfModule>
</IfModule>
This code block is good to go, in our experience, with any Wordpress site. BUT! You'll need to modify a line in .htaccess FIRST. Very important step, don't miss this!
As per this very useful post, we need to edit .htaccess and change:
RewriteRule . /index.php [L]into:
RewriteRule ^(.*)$ /index.php/$1 [L]
So let's review the block of code to give you some more in-depth detail.
<IfModule mod_cache.c>
We're assuming the module is loaded.
CacheQuickHandler Off
We're turning off CacheQuickHandler because in our experience it caused all kinds of problems with requests.
CacheHeader Off
CacheDetailHeader Off
We don't want to serve extra headers to show visitors what is cached, but you can enable them if you wish.
CacheIgnoreNoLastMod On
Letting Apache know we want to cache everything that even has no last modified header.
CacheDefaultExpire 180
The default cache time of assets - but note this is further governed by max_age cache control header (whichever is shortest).
CacheMinExpire 5
Thw minimum time we want to cache assets.
CacheStoreExpired Off
We don't want to keep expired assets on disk and serve them up stale.
CacheLastModifiedFactor 0.5
This one is complex (it's a cache ratio for assets missing cache control headers) but for our short cache span makes no difference.
CacheIgnoreCacheControl On
This will ignore public and private cache control headers.
CacheIgnoreHeaders Set-Cookie Cookie
We're removing/hiding cookies from responses before caching them, for security reasons.
CacheLock on
Turning on a static cache lock during stale caches and updates, so that we don't flood the origin server.
CacheLockMaxAge 5
But we don't want to keep expired assets for longer than 5 seconds.
CacheDisable /wp-admin
CacheDisable /wp-content
CacheDisable /wp-login.php
CacheDisable /wp-cron.php
Disabling some Wordpress pages from ever being cached.
SetOutputFilter CACHE
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/rss+xml text/xml image/svg+xml
Adding GZIP compression to the output of the cached files.
<IfModule mod_cache_disk.c>
CacheRoot /var/cache/apache2/proxy/
Where we want to store our cached files on disk.
CacheEnable disk /
We want to start at the root of the domain (domain.com/)
CacheDirLevels 2
How many on-disk directory levels to store the cache (used for performance reasons but not an issue unless your cache is gigantic).
CacheDirLength 1
Similarly, the maximum directory name length but it is largely irrelevant unless your caching directory structure is very deep.
CacheMaxFileSize 20000000
Set to 20MB of cache by default.
</IfModule>
</IfModule>