我的wp缓存

发布于 2023-06-13  12 次阅读


wp硬查数据库,卡!要缓存!

肯定是大大的cf了

页面规则

cloudflare默认回源,要写页面规则才会对页面缓存,cf一个域名免费3条规则

屏幕截图 2023-06-13 215803.png

这样基本把wordpress缓存好了

还有Cache Rules

cache rules可以定义绕过缓存,把页面规则没绕过的也加进去,比如wp-admin和preview=true

再加cookie跳过规则,避免blog登录后的主页被缓存进去被别人看到头像

image-20230613220350866

nginx的wp supper cache规则

# WP Super Cache 规则
#嗯~ o(* ̄▽ ̄*)o这段不知道是从哪看到的了

set $cache_uri $request_uri;
set $nginx_static 'BYPASS For File';

if ($request_method = POST)
{
set $cache_uri 'null cache';
set $nginx_static 'BYPASS For POST';
}

if ($query_string != "")
{
set $cache_uri 'null cache';
set $nginx_static 'BYPASS For Query';
}

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 $cache_uri 'null cache';
set $nginx_static 'BYPASS For URL';
}

if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in|woocommerce_items_in_cart|woocommerce_cart_hash|wptouch_switch_toogle")
{
set $cache_uri 'null cache';
set $nginx_static 'BYPASS For Cookie';
}

if (-f $document_root/wp-content/cache/supercache/$http_host/$cache_uri/index-https.html)
{
set $nginx_static 'HIT';
}
if (-f $document_root/wp-content/cache/supercache/$http_host/$cache_uri/index.html)
{
set $nginx_static 'HIT';
}

location /
{
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index-https.html /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?$args;
}

add_header Nginx-cache-Status $nginx_static;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

  location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    include        fastcgi_params;
  }
  location ~ .*\.(gif|jpg|jpeg|png|webp|bmp|flv|ico)$ {
    expires 30d;
    access_log off;  
  }

  location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
  }

结果

配置完后注销登录就能在标头看到效果

image-20230613221833470

僕と契約して、魔法少女になってよ!
最后更新于 2024-01-18