首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NGINX+Wordpress缓存悖论

NGINX+Wordpress缓存悖论
EN

Stack Overflow用户
提问于 2018-09-06 16:12:10
回答 1查看 1.1K关注 0票数 2

我在运行Wordpress + NGINX +PHM。我对我的NGINX配置做了这些更改,这些修改摘自我遇到的一篇最佳实践文章:

代码语言:javascript
运行
复制
#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;
}

但是,在使用PHPSESSID cookie时,post页面(我的大部分点击量)并不是在请求时缓存的。文章页面不包含每个用户的上下文,并且是通用的。是否有更好的方法允许只对post页面进行缓存?也许使用“domain.com/yyyy/mm/dd/post”模式?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-06 18:00:50

这是我通常用来专门禁用Wordpress/Woocommerce站点的快速CGI缓存的块,它可能会有帮助:

代码语言:javascript
运行
复制
#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;
}

这是我发现的多篇文章和要点的集合。注意,我通常在每个块中添加头,以查看在测试期间禁用缓存的是什么。有些规则可能会被重复。

注销后,用户仍然有一些cookie,因此,有时,绕过缓存。但在大多数情况下,它是正确的。

当然,实现全部或部分将需要测试。

我在Wordpress Nginx Helper插件中使用了这个插件,这对于全局和有条件的清除非常有用。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52208290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档