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?
fastcgi_cache_bypass
andfastcgi_no_cache
? Which variables are set behind it?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!