简体   繁体   English

NGINX+Wordpress 缓存悖论

[英]NGINX+Wordpress Caching paradox

I'm running Wordpress + NGINX + PHM-FPM.我正在运行 Wordpress + NGINX + PHM-FPM。 I've made these changes to my NGINX config, taken from a best-practices article I've encountered:我已经对我的 NGINX 配置进行了这些更改,这些更改取自我遇到的最佳实践文章:

#Don't cache if there is a cookie called PHPSESSID
if ($http_cookie ~* "PHPSESSID"){
    set $no_cache 1;
}

#Don't cache if there is a cookie called wordpress_logged_in_[hash]
if ($http_cookie ~* "wordpress_logged_in_"){
    set $no_cache 1;
}

however, the means the post pages (majority of my hits) are not cached as they are requested while the PHPSESSID cookie is used.然而,这意味着在使用 PHPSESSID cookie 时,不会缓存帖子页面(我的大部分点击)。 The post pages do not contain per-user context and are general.帖子页面不包含每个用户的上下文并且是通用的。 IS there a better way to allow caching for post pages only?有没有更好的方法来只缓存帖子页面? perhaps using the "domain.com/yyyy/mm/dd/post-name" pattern?也许使用“domain.com/yyyy/mm/dd/post-name”模式?

This is the block I usually use to specifically disable Fast-CGI cache for Wordpress/Woocommerce site, it may help:这是我通常用来专门禁用 Wordpress/Woocommerce 站点的 Fast-CGI 缓存的块,它可能会有所帮助:

#disabler
set $no_cache 0;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
    set $no_cache 1;
}
if ($request_method = PATCH) {
    set $no_cache 1;
}
if ($query_string != "") {
    set $no_cache 1;
}   

# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/wp-login/|/xmlrpc.php|index.php|phpmyadmin|sitemap(_index)?.xml|sitemap$") {
    set $no_cache 1;
}   
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
    set $no_cache 1;
}
if ($request_uri ~* "(/wp-admin/|/cart/|/checkout/|/account/|/mon-compte/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
    set $no_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|postpass|wordpress_n$") {
    set $no_cache 1;
}

# Woocommerce
if ($request_uri ~* "/store.*|/cart.*|/my-account.*|/checkout.*|/addons.*|/panier.*|/paiement.*|/mon-compte.*") {
    set $no_cache 1;
}

if ( $arg_add-to-cart != "" ) { 
   set $no_cache 1;
}

# need more tests
#if ( $cookie_woocommerce_items_in_cart != "0" ) {  
#   set $no_cache 1;
#}

if ( $cookie_woocommerce_items_in_cart ) {
    set $no_cache 1;
}

This is an aggregate of multiple articles and Gist I found.这是我发现的多篇文章和要点的汇总。 Note that I usually add headers in each block to see what is the one disabling cache during tests.请注意,我通常在每个块中添加标头,以查看在测试期间禁用缓存的功能是什么。 Some rules may be duplicated.某些规则可能会重复。

After a log out, the user still has some cookie so sometimes, bypasses the cache.注销后,用户仍然有一些 cookie,因此有时会绕过缓存。 But it works correctly in most of the case.但它在大多数情况下都能正常工作。

Implementing all or part of this will, of course, need tests.当然,实现全部或部分这将需要测试。

I use this among the Wordpress Nginx Helper plugin, that is really useful for a global and conditional purge.我在Wordpress Nginx Helper插件中使用它,这对于全局和条件清除非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM