What you’re looking for is to set a variable that determines whether a user is logged in (and other circumstances where you don’t want caching), then pass that variable to fastcgi_cache_bypass
and fastcgi_no_cache
:
So in your server{}
block you want something like this:
set $skip_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
And then add these to your php location {}
block where you also have your fastcgi_cache
setting:
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
See this for more info: https://easyengine.io/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/
Doing just that worked for me, but if you follow the full guide and set up conditional purging that will probably get you the best results.