Nginx 中的 Cache Enabler 外掛高級配置

·

Cache Enabler 外掛是 KeyCDN 開發的一款 WordPress 輕量級快取外掛,創建靜態 HTML 文件並將其存儲在服務器硬碟上,Web 服務器將傳遞靜態 HTML 文件給你的網站讀者,可以避免太密集的進程在服務器後端 (CPU、外掛和數據庫),並提高你的 WordPress 網站的性能,有效加速網站速度,而且是完全免費。

內容目錄:

  • Cache Enabler Plugin 高級配置
  • 修改你的虛擬主機的 conf 文件

說道 Cache Enabler 外掛,就會提到德國非常受歡迎的 Cachify 快取外掛,但是作者似乎放棄更新,程式碼寫得很好,輕量且高效能。

Cache Enabler 外掛就是以 Cachify 為原型,加以改良,增加了 WebP 圖片優化,更與 Autoptimize 有完美的結合。

Cachify 外掛

Cache Enabler 配置的方法非常容易,啟動外掛後,依照預設的配置就可以,如個你還不滿意這樣的快取模式,可以修改 Nginx 的 conf 文件,完全繞過 PHP 程序,達到快取型態。

安裝 Cache Enabler 外掛:

Cache Enabler Plugin 高級配置

複製 /etc/nginx/common/php.conf 文件,給予一個新名稱,例如:wpcache.conf

打開 wpcache.conf 文件,修改如下:

# PHP NGINX CONFIGURATION
# DO NOT MODIFY, ALL CHANGES LOST AFTER UPDATE Webinoly

    set $cache_uri $request_uri;

    # bypass cache if POST requests or URLs with a query string
    if ($request_method = POST) {
        set $cache_uri 'nullcache';
    }
    if ($query_string != "") {
        set $cache_uri 'nullcache';
    }

    # bypass cache if URLs containing the following strings
    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 'nullcache';
    }

    # bypass cache if the cookies containing the following strings
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
        set $cache_uri 'nullcache';
    }

    # custom sub directory e.g. /blog
    set $custom_subdir '';

    # default html file
    set $cache_enabler_uri '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${cache_uri}index.html';

    # webp html file
    if ($http_accept ~* "image/webp") {
        set $cache_enabler_uri '${custom_subdir}/wp-content/cache/cache-enabler/${http_host}${cache_uri}index-webp.html';
    }
    
    location / {
        gzip_static on; # this directive is not required but recommended
        try_files $cache_enabler_uri $uri $uri/ $custom_subdir/index.php?$args;
    }	

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass php;
    }

存檔後,上傳至 /etc/nginx/common 資料夾內。

修改你的虛擬主機的 conf 文件

打開 /etc/nginx/sites-available/example.com 文件,將 php.conf 更換為 wpcache.conf,如下

...................................
include common/wpcache.conf;
include common/wpcommon.conf;
include common/locations.conf;
include common/headers-http.conf;
include common/headers-html.conf;
include common/headers-https.conf;
...................................

存檔後重啟 Nginx

nginx -t && service nginx restart

結語

默認的 Cache Enabler 設置將滿足大部分的使用情況。您不必再添加自定義代碼配置在 Nginx 上,也不需要設置 cron 作業。

多增加這個配置,以我目前的網站是感覺不出有甚麼不同,可能需要一個高流量網站來測試,會比較準確。

hosted by kinsta

Comments

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Hey, 想嘗試 Kinsta 主機嗎?

18748

Kinsta 高效能主機

wPowered Starter 方案

馬上訂購

18749
Your Mastodon Instance