wordpress緩存插件:wp super cache 使用筆記 nginx下

  1. 兩種模式
    1. 簡(jiǎn)單模式
    2. 專(zhuān)家模式

try_files $uri $uri/ /index.php?$args;實(shí)現(xiàn)偽靜態(tài)的前提下,兩種模式都會(huì)生成html格式的緩存文件

  1. 壓縮頁(yè)面以便讓來(lái)訪(fǎng)者更快瀏覽
    會(huì)在生成html緩存文件的同時(shí)生成一個(gè)gz壓縮包

  2. 到期時(shí)間和垃圾回收器
    根據(jù)設(shè)定來(lái)清理緩存。

    功能截圖

    定時(shí)器選項(xiàng)會(huì)在動(dòng)態(tài)頁(yè)面訪(fǎng)問(wèn)時(shí)判斷是否達(dá)到定時(shí)器間隔的時(shí)間,如果達(dá)到了間隔的時(shí)間就清理一次緩存(所有緩存超時(shí)的文件)。
    時(shí)間選項(xiàng)會(huì)在動(dòng)態(tài)頁(yè)面被訪(fǎng)問(wèn)時(shí)判斷是否達(dá)到設(shè)定的時(shí)間且緩存文件是否未清理,如果都兩個(gè)都滿(mǎn)足就清理一次緩存(所有緩存超時(shí)的文件)。

  3. 實(shí)現(xiàn)真靜態(tài)
    主機(jī)規(guī)則地址:https://wordpress.org/support/article/nginx/#wp-super-cache-rules

# WP Super Cache rules.
# Designed to be included from a 'wordpress-ms-...' configuration file.
 
set $cache_uri $request_uri;
 
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
        set $cache_uri 'null cache';
}
 
if ($query_string != "") {
        set $cache_uri 'null cache';
}   
 
# Don't cache uris containing the following segments
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';
}   
 
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
        set $cache_uri 'null cache';
}
 
# START MOBILE 如果手機(jī)端不需要緩存就把注釋去掉
# Mobile browsers section to server them non-cached version. COMMENTED by default as most modern wordpress themes including twenty-eleven are responsive. Uncomment config lines in this section if you want to use a plugin like WP-Touch
# if ($http_x_wap_profile) {
#        set $cache_uri 'null cache';
#}
 
#if ($http_profile) {
#        set $cache_uri 'null cache';
#}
 
#if ($http_user_agent ~* (2.0\ MMP|240x320|400X240|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|Googlebot-Mobile|hiptop|IEMobile|KYOCERA/WX310K|LG/U990|MIDP-2.|MMEF20|MOT-V|NetFront|Newt|Nintendo\ Wii|Nitro|Nokia|Opera\ Mini|Palm|PlayStation\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|SHG-i900|Small|SonyEricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|webOS|Windows\ CE|WinWAP|YahooSeeker/M1A1-R2D2|iPhone|iPod|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800)) {
 #       set $cache_uri 'null cache';
#}
 
#if ($http_user_agent ~* (w3c\ |w3c-|acs-|alav|alca|amoi|audi|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|doco|eric|hipt|htc_|inno|ipaq|ipod|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-|lg/u|maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|palm|pana|pant|phil|play|port|prox|qwap|sage|sams|sany|sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo|teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|wap-|wapa|wapi|wapp|wapr|webc|winw|winw|xda\ |xda-)) {
  #      set $cache_uri 'null cache';
#}
#END MOBILE
 
# Use cached or actual file if they exists, otherwise pass request to WordPress
location / {
        try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?$args ;
}

其中try_files /wp-content/cache/supercache/$http_host/$request_uri/index.html $uri $uri/ /index.php?$args;,如果在本地測(cè)試且主機(jī)帶有端口就把$http_host換成$host
需要注意的是,wp super cache 的垃圾回收器需要訪(fǎng)問(wèn)動(dòng)態(tài)頁(yè)面才能觸發(fā),在rewrite后,如果被訪(fǎng)客訪(fǎng)問(wèn)的頁(yè)面均已被緩存,那么訪(fǎng)問(wèn)的都是靜態(tài)文件。若沒(méi)有評(píng)論觸發(fā)緩存更新或管理員沒(méi)有手動(dòng)清除緩存,緩存將不會(huì)清理,也就不會(huì)更新。

5.簡(jiǎn)單與專(zhuān)家模式區(qū)別
沒(méi)看源碼不知道,但是使用體驗(yàn)是一致的,不論勾選簡(jiǎn)單模式還是專(zhuān)家模式都可以實(shí)現(xiàn)真靜態(tài)。

總結(jié):流量不大用緩存插件。當(dāng)流量很大,數(shù)據(jù)很多的時(shí)候,緩存插件的作用就不那么大了,應(yīng)該考慮從項(xiàng)目整體來(lái)優(yōu)化。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容