I am trying to figure out how to purge the NGINX FastCGI Cache in Trellis (https://roots.io/trellis/docs/fastcgi-caching/). I understand that Trellis doesn’t support cache clearing out of the box. Trellis stores cache in /var/cache/nginx/* and therefore these are the files that have to be deleted in order for the cache to be cleared. I am trying to implement a form in the WP Admin that can do that based on a user action.
Before we start:
Trellis runs PHP as the user web that is part of the www-data group. The permissions on the /var/cache/nginx folder are 700 with the owner user www-data. All the other subfolders in the directory are also owned by the user www-data with permission structure 700. So when the user web tries to run a shell command or a PHP script that would delete the files, it fails and gets permission denied.
I’ve tried changing the web_user in group_vars/all/users.yml to www-data instead of web but then I get an error when cloning project files from the git repo.
Potentially silly question, but why do you need to clear the cache? It’s a “micro” cache because the cache time is so short. Usually it’s fine to be ~15 seconds stale?
I want to extend the cache time so it acts more like a real cache, because it is still a much more suitable option than using php caching (with a WP plugin for example). Therefore I need to have a way to manually clear the cache. Is there at least a way change web_user: web in group_vars/all/users.yml.
If anyone is interested, you have to change the web_user in users.yml to www-data which is the default user that nginx uses. Then your web_user will have the appropriate permissions and will be able to delete the cache either with a recursive delete PHP script or simply a shell command using rm.
After you change the user to www-data you will not only have to reprovision the server but also spin up an entirely new Ubuntu instance (if you are using Digital Ocean, you can just rebuild the droplet with the same image).
This way you can set the expiry time of the server cache way beyond the “micro” oriented cache and clear it whenever needed.
I know this topic is a bit old but I would like to just document here another solution that does not require changing the nginx user and setting up a new instance.
Install the apt package libnginx-mod-http-cache-purge.
(If you can’t install this package because of any incompatibility with the installed nginx version, one solution is to stop using the nginx’s mainline repository (default in Trellis) and then install the nginx available in your OS repositories. This is going to downgrade the nginx.)
Customize your website nginx config (in the server, /etc/nginx/sites-available/yourwebsite.conf) and in trellis /trellis/roles/wordpress-setup/templates/wordpress-site.conf.j2. Add the following immediately after the location ~ \.php$ block.
Install and configure the plugin Nginx Helper, to automatically purge the cache of a page when it’s updated.
Enable the config “Enable Purge”, select “nginx FastCGI cache” as the caching method" and for the purge method select “Using a GET request to PURGE/url (Default option)”
Optional: if you want, increase the cache duration time in your trellis (and nginx) config.