Nginx FastCGI 跳过 Wordpress woocommerce 中 /shop/ 的缓存规则,为什么它们不起作用?

Nginx FastCGI 跳过 Wordpress woocommerce 中 /shop/ 的缓存规则,为什么它们不起作用?

我正在运行带有 Wordpress 和 Woocommerce 的 LEMP 服务器。我的网站位于https://www.mcmo.is

我的 Nginx Skip Cache 规则似乎有效,但有一页除外:https://www.mcmo.is/shop

将产品添加到购物车并点击“继续购物”时,缓存不会更新购物车中的产品编号店铺页面。将商品添加到购物车后,我的所有其他页面都会显示更新的购物车编号。

以下是我的 Nginx 跳过缓存规则和虚拟主机中的相对位置块:

# 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;
}

以下是我的nginx.conf文件中相关的 FastCGI 信息:

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";

我在跳过缓存规则中做错了什么,我该如何修复它,以便 Nginx 不会缓存购物车商品计数https://www.mcmo.is/shop

答案1

我在休假期间用手机写了这篇文章,因为我睡不着,哈哈。所以,如果有任何语法错误,我深表歉意。

根据您提供的配置,您将同时使用$skip_cache$no_cache变量。由于fastcgi_cache_bypassfastcgi_no_cache指令都引用$skip_cache,因此所有应绕过缓存的条件都需要设置为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;

因此,这里的嫌疑人是set $no_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;
}

变成这样:

# 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;
}

在重新启动 Nginx 之前使用以下命令测试您的配置:

sudo nginx -t

在那之后

sudo systemctl reload nginx

祝您购物愉快并赚大钱!

相关内容