對(duì)于使用 WordPress 建站的站長(zhǎng)來(lái)說(shuō),網(wǎng)站速度優(yōu)化一直是一個(gè)棘手的問(wèn)題。然而,現(xiàn)在隨著寶塔面板的普及,WP Super Cache + Redis 成為了最簡(jiǎn)單有效的 WordPress 速度優(yōu)化方案。
WP Super Cache 是目前最高效、最簡(jiǎn)單的 WordPress 靜態(tài)緩存插件之一,它可以將整個(gè)網(wǎng)頁(yè)生成為 HTML 文件,從而顯著提升 WordPress 博客的訪問(wèn)速度。
Redis 是一種基于內(nèi)存的 Key-Value 數(shù)據(jù)庫(kù),具有更快的查詢速度和更高的并發(fā)能力,可以讓網(wǎng)站達(dá)到秒開(kāi)的效果。因此,將 WP Super Cache 和 Redis 結(jié)合起來(lái)使用,可以大幅度提高 WordPress 網(wǎng)站的性能和用戶體驗(yàn)。

1. 安裝 Redis
進(jìn)入寶塔面板后臺(tái)管理界面→選擇軟件管理→運(yùn)行環(huán)境→Redis→點(diǎn)擊安裝,等待完成。

2. 安裝 PHP 的 Redis 和 Opcache 擴(kuò)展
① 在寶塔面板找到 PHP 設(shè)置:

② 在 PHP 設(shè)置中,安裝 Redis 和 Opcache 擴(kuò)展。

3. 在 WordPress 后臺(tái)中,安裝 Redis Object Cache 和 WP Super Cache 插件


4. 設(shè)置緩存插件
① 配置 WP Super Cache
通用 - 啟用緩存功能 (推薦) - 更新

高級(jí) - 如圖配置 - 設(shè)置完成后點(diǎn)擊更新保存設(shè)置。

預(yù)緩存 - 勾選”預(yù)緩存模式(垃圾回收器已禁用。推薦。)/預(yù)緩存標(biāo)簽,分類以及其他。“ - 保存

② 配置 Redis Object Cache
Enable Object Cache(啟用對(duì)象緩存)打開(kāi)開(kāi)關(guān)即可。

過(guò)一段時(shí)間有圖像則緩存成功:

5. 修改站點(diǎn)偽靜態(tài)
# WP Super Cache rules.
# Designed to be included from a 'wordpress-ms-...' configuration file.
set $cache_uri $request_uri;
# 請(qǐng)求方式為post時(shí)不使用緩存
if ($request_method = POST) {
set $cache_uri 'null cache';
}
if ($query_string != "") {
set $cache_uri 'null cache';
}
# uri包含以下內(nèi)容時(shí)不使用緩存
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';
}
# 對(duì)登錄用戶或已發(fā)布評(píng)論用戶不使用緩存
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
set $cache_uri 'null cache';
}
# START MOBILE
# 如果需要在移動(dòng)端禁用緩存,可以把代碼前的#刪掉
# 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
# 如果存在緩存,則使用緩存,如果沒(méi)有緩存,直接轉(zhuǎn)向動(dòng)態(tài)頁(yè)面
location / {
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?$args ;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;配置完成后,再去 WP Super Cache 重新生成緩存。
轉(zhuǎn)載自:https://blog.itluo.com/experience-guide/wordpress-speed-optimization-redis.html 特別感謝。

評(píng)論