Skip to content

How to bypass fastcgi_cache cache on specific URLs

Here is a couple of ways to bypass fastcgi_cache (sites created with --wpfc) on specific URLs.

1. Via custom nginx config

The cache rules for fastcgi_cache are defined in /etc/nginx/conf.d/map-wp-fastcgi-cache.conf. First, create an empty map-wp-fastcgi-cache.conf.custom so that your custom rules does not get overwritten in future WordOps updates:

touch /etc/nginx/conf.d/map-wp-fastcgi-cache.conf.custom

Then edit map-wp-fastcgi-cache.conf with your custom page slugs in the map $request_uri $uri_no_cache block:

map $request_uri $uri_no_cache {
....
    "~*/resetpass/" 1;
    "~*/my-page-slug/" 1;
}

Reload nginx:

nginx -t && systemctl reload nginx

A possible easier way is to set a custom cookie. As defined in /etc/nginx/conf.d/map-wp-fastcgi-cache.conf, cache is bypassed if certain cookies are present, for example wordpress_no_cache.

To take advantage of this, consider the following snippet placed in a plugin or in your theme's functions.php:

function set_bypass_cache_cookie() {
    setcookie('wordpress_no_cache', '1', 0, '/my-page-slug/');
}
add_action( 'init', 'set_bypass_cache_cookie');

Note that this method bypasses cache in all subfolders too, for example /my-page-slug/subfolder/another-subfolder/.

If you would like an user interface (screenshot below) for managing URLs using the method above, take a look at this plugin.