對于使用 WordPress 建站的站長來說,網站速度優化一直是一個棘手的問題。然而,現在隨著寶塔面板的普及,WP Super Cache + Redis 成為了最簡單有效的 WordPress 速度優化方案。
WP Super Cache 是目前最高效、最簡單的 WordPress 靜態緩存插件之一,它可以將整個網頁生成為 HTML 文件,從而顯著提升 WordPress 博客的訪問速度。
Redis 是一種基于內存的 Key-Value 數據庫,具有更快的查詢速度和更高的并發能力,可以讓網站達到秒開的效果。因此,將 WP Super Cache 和 Redis 結合起來使用,可以大幅度提高 WordPress 網站的性能和用戶體驗。
1. 安裝 Redis
進入寶塔面板后臺管理界面→選擇軟件管理→運行環境→Redis→點擊安裝,等待完成。
2. 安裝 PHP 的 Redis 和 Opcache 擴展
① 在寶塔面板找到 PHP 設置:
② 在 PHP 設置中,安裝 Redis 和 Opcache 擴展。
3. 在 WordPress 后臺中,安裝 Redis Object Cache 和 WP Super Cache 插件
4. 設置緩存插件
① 配置 WP Super Cache
通用 - 啟用緩存功能 (推薦) - 更新
高級 - 如圖配置 - 設置完成后點擊更新保存設置。
預緩存 - 勾選”預緩存模式(垃圾回收器已禁用。推薦。)/預緩存標簽,分類以及其他。“ - 保存
② 配置 Redis Object Cache
Enable Object Cache(啟用對象緩存)打開開關即可。
過一段時間有圖像則緩存成功:
5. 修改站點偽靜態
# WP Super Cache rules. # Designed to be included from a 'wordpress-ms-...' configuration file. set $cache_uri $request_uri; # 請求方式為post時不使用緩存 if ($request_method = POST) { set $cache_uri 'null cache'; } if ($query_string != "") { set $cache_uri 'null cache'; } # uri包含以下內容時不使用緩存 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'; } # 對登錄用戶或已發布評論用戶不使用緩存 if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") { set $cache_uri 'null cache'; } # START MOBILE # 如果需要在移動端禁用緩存,可以把代碼前的#刪掉 # 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 # 如果存在緩存,則使用緩存,如果沒有緩存,直接轉向動態頁面 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 重新生成緩存。
轉載自:https://blog.itluo.com/experience-guide/wordpress-speed-optimization-redis.html 特別感謝。
評論