Goal
Configure Wordpress to send proper cache-control headers, so the HTTP cache can be used.
Assumptions
- A WordPress installation on Platform.sh that uses Composer, for example one that uses the WordPress Template and is configured for a composer-based installation of plugins
- An SSH key configured on the project account
- Composer installed locally
Problems
By default, Wordpress doesn’t set proper headers to allow the build-in HTTP cache to work efficiently.
You can test if your site is cached based on the x-platform-cache
response header which will probably be a MISS
with the default Wordpress installation.
Steps
1. Install and enable the cache-control plugin
The main configuration that the HTTP cache needs to work properly, is Cache-Control
header. Since Wordpress does not include an option to set those headers in their core, you need to use a 3rd party plugin. The example used here is cache-control.
If you have set up your project to use a composer-based installation of plugins (see here on how to do this), you can use composer require wpackagist-plugin/cache-control
locally to update your composer files.
After that commit and push them so the plugin gets deployed to your project. In the Admin-Dashboard go to Plugins, find Cache-Control and click Activate.
2. Verify and adjust the header values
In the plugin settings, you can adjust the time different sites is being cached (e.g home page, post sites, etc). You can adjust that as necessary, following good practises.
3. Settings for static assets
For static assets, the Wordpress template includes a default Cache-Control
of 600
in your .platform.app.yaml
file. To adjust that (if needed), you find the expires
setting in the location block.
4. Verify your site is being cached
To check if the response is cached, you can use curl -I
to retrieve the headers. While the first response might be a MISS
(because the page has not been cached yet), as soon as you send the same request again, it should return HIT
.
curl -I https://master-7rqtwti-af6kbo7ndasc2.eu-3.platformsh.site/
HTTP/2 200
cache-control: max-age=300
…
x-platform-cache: HIT
…
Conclusion
With this setup, your Wordpress site can leverage the Platform.sh HTTP Cache and you have control over the caching behaviour for different parts of your Wordpress site.