Score:0

Nginx FastCGI Skip Cache Rules for /shop/ in Wordpress woocommerce, why aren't they working?

mx flag

I am running a LEMP server with Wordpress and Woocommerce. My site is live at https://www.mcmo.is.

My Nginx Skip Cache rules seem to be working except for on one page: https://www.mcmo.is/shop

When adding a product to the cart, and clicking "Continue Shopping", the cache doesn't update the product number in the cart on the shop page. All of my other pages show an updated cart number after adding something to the cart.

Here are my Nginx skip cache rules & the relative location block in my virtual host:

# https://www.linuxbabe.com/nginx/setup-nginx-fastcgi-cache
# Don't Skip Cache by Default
set $skip_cache 0;

# https://www.linuxbabe.com/nginx/setup-nginx-fastcgi-cache
# POST requests should always go to PHP
if ($request_method = POST) {
    set $skip_cache 1;
}

# URLs containing query strings should always go to PHP
# ADMIN Note: You might want to be sure to turn off query strings in H-code wordpress theme, and other themes
# https://www.linuxbabe.com/nginx/setup-nginx-fastcgi-cache
if ($query_string != "") {
    set $skip_cache 1;
}

# Don't cache uris containing the following segments
# https://www.linuxbabe.com/nginx/setup-nginx-fastcgi-cache
# https://easyengine.io/wordpress-nginx/tutorials/plugins/woocommerce/
# https://docs.cleavr.io/guides/woocommerce/
#if ($request_uri ~* "/wp-admin/|/wp-json/|/login/|/register/|/shopping-cart.*|.*add-to-cart.*|.*empty-cart.*|/cart.*|/checkout.*|/addons.*|/my-account.*|/wishlist.*|/xmlrpc.php|wp-.*.php|^/feed/*|/tag/.*/feed/*|index.php|/.*sitemap.*\.(xml|xsl)") {
#    set $skip_cache 1;
#}
# Original Linuxbabe
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|^/feed/*|/tag/.*/feed/*|index.php|/.*sitemap.*\.(xml|xsl)") {
    set $skip_cache 1;
}

# Woocommerce
# https://blog.khophi.co/my-ultimate-ncachewowo/
if ($request_uri ~* "/shop/|/shop.*|/shopping-cart/|/shopping-cart.*|/store.*|/cart.*|/my-account.*|/checkout.*|/addons.*|.*add-to-cart.*|.*empty-cart.*") {
    set $no_cache 1;
}

# Other
if ($request_uri ~* "/login/|/register/|/addons.*|/wishlist.*") {
    set $skip_cache 1;
}

# Pass Fastcgi to PHP
location ~ \.php$ {
    # Pass FastCGI to PHP 7.4
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTP_PROXY "";
    fastcgi_buffers 1024 4k;
    fastcgi_buffer_size 128k;
    # Split Path Info
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    # Check that the PHP script exists before passing it
    # The if lets NGINX check whether the *.php does indeed exist,
    # to prevent NGINX from feeding PHP FPM non php script files (like uploaded images).
    # see https://www.nginx.com/resources/wiki/start/topics/examples/phpfcgi/
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }
    fastcgi_index index.php;
    include fastcgi_params;
    # FastCGI Cache
    #fastcgi_cache off;
    fastcgi_cache mcmo.is;
    fastcgi_cache_valid 200 301 302 12h;
    fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
    fastcgi_cache_min_uses 1;
    fastcgi_cache_lock on;
    # Tell Nginx to send requests to upstream PHP-FPM server, instead of trying to find files in the
    # cache. If the value of $skip_cache is 1, then the first directive tells Nginx to send request
    # to upstream PHP-FPM server, instead of trying to find files in the cache.
    # ADMIN007 Note: fastcgi_cache_bypass $skip_cache and fastcgi_no_cache $skip_cache should be
    # uncommented if using google XML sitemap plugin, or Yoast SEO Plugin, or if you want to
    # enable the skip cache rules above.
    fastcgi_cache_bypass $skip_cache;
    # This directive tells Nginx not to cache the response.
    fastcgi_no_cache $skip_cache;
}

Here are is the relevant FastCGI info in my nginx.conf file:

http {
    ##
    # FASTCGI CACHE
    ##

    fastcgi_cache_path /var/www/cache/fastcgi_cache/mcmo.is levels=1:2 keys_zone=mcmo.is:100m max_size=700m inactive=24h use_temp_path=off; # In Memory (ramdisk)    #
    fastcgi_cache_key "$scheme$request_method$host$request_uri";

What am I doing wrong in my skip cache rules and how can I fix it so that Nginx doesn't cache the shopping cart item count on https://www.mcmo.is/shop?

Bombaci avatar
br flag
How do these directives appear in your Nginx configuration: `fastcgi_cache_bypass` and `fastcgi_no_cache`? Which variables are set behind it?
DanRan avatar
mx flag
Hey @Bombaci Thanks for the reply. In my original post, I have added all of the relative info from my nginx.conf file as well as my virtual hosts `location ~ \.php$ {` location block which implements fastcgi_no_cache and fastcgi_cache_bypass. This should be everything needed to help me I believe. Let me know what I can do! Thanks!
Score:1
br flag

Writing this on my mobile, during my vacation, cause I couldn't sleep, haha. So, my apologies for any grammar mistake.

Based on your delivered configuration, you are using both $skip_cache and $no_cache variables. Since the fastcgi_cache_bypass and fastcgi_no_cache directives both reference $skip_cache, all conditions that should bypass the cache need to be set to set $skip_cache 1.

[..]
# enable the skip cache rules above.
   fastcgi_cache_bypass $skip_cache;
# This directive tells Nginx not to cache the response.
   fastcgi_no_cache $skip_cache;

Due to this, the suspect here is set $no_cache 1. It looks like a small mistake due to the use of different tutorials.

Thus, my suggestion for you is to change this:

# Woocommerce
# https://blog.khophi.co/my-ultimate-ncachewowo/
if ($request_uri ~* "/shop/|/shop.*|/shopping-cart/|/shopping-cart.*|/store.*|/cart.*|/my-account.*|/checkout.*|/addons.*|.*add-to-cart.*|.*empty-cart.*") {
    set $no_cache 1;
}

Into this:

# Woocommerce
# https://blog.khophi.co/my-ultimate-ncachewowo/
if ($request_uri ~* "/shop/|/shop.*|/shopping-cart/|/shopping-cart.*|/store.*|/cart.*|/my-account.*|/checkout.*|/addons.*|.*add-to-cart.*|.*empty-cart.*") {
    set $skip_cache 1;
}

Use the following command to test your configuration before restarting Nginx:

sudo nginx -t

And after that

sudo systemctl reload nginx

Have fun and great earnings with your shop!

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.