WordPress安裝Cachify輕便緩存插件加速網(wǎng)站速度

麥子 運(yùn)維技術(shù)50字?jǐn)?shù) 3123閱讀10分24秒閱讀模式

WordPress提供的免費(fèi)和付費(fèi)緩存加速插件還是有很多的,但是對(duì)于大部分插件給我們用戶的感覺就是設(shè)置比較復(fù)雜,尤其是再遇到英文界面更加不懂如何設(shè)置。在這里,麥子有看到這款 Cachify 插件設(shè)置簡(jiǎn)單且支持DB, HDD, APC 和 Memcached多種緩存方式,如果我們有需要安裝可以看看。

Cachify通過緩存文章、頁面和自定義文章類型為靜態(tài)內(nèi)容來優(yōu)化您的頁面加載速度。我們可以從以下幾個(gè)緩存方式中選擇:數(shù)據(jù)庫、服務(wù)器硬盤(HDD)、Memcached(僅在Nginx上可用)或 APC(Alternative PHP Cache)——直接在Web服務(wù)器的系統(tǒng)緩存中。用戶訪問時(shí),頁面或文章可直接從緩存中拉取。數(shù)據(jù)庫和查詢和PHP請(qǐng)求數(shù)可顯著減少,如果選擇了合適的緩存方式,這個(gè)數(shù)字可能為0。

我們可以直接激活下載。

我們可以看到設(shè)置界面還很簡(jiǎn)單,而且還有中文語言。設(shè)置緩存方式有四種,比如我們需要用到 Memcached 緩存方式必須要Nginx環(huán)境引擎且我們當(dāng)前服務(wù)器安裝過 Memcached 才可以在下拉選擇緩存方式可以看到。

我們需要在當(dāng)前站點(diǎn)的nginx配置文件中加入:

## GZIP
gzip_static on;

## CHARSET
charset utf-8;

## INDEX LOCATION
location / {
error_page 404 405 = @nocache;

if ( $query_string ) {
return 405;
}
if ( $request_method = POST ) {
return 405;
}
if ( $request_uri ~ "/wp-" ) {
return 405;
}
if ( $http_cookie ~ (wp-postpass|wordpress_logged_in|comment_author)_ ) {
return 405;
}

default_type text/html;
add_header X-Powered-By Cachify;
set $memcached_key $host$uri;
memcached_pass localhost:11211;
}

## NOCACHE LOCATION
location @nocache {
try_files $uri $uri/ /index.php?$args;
}

然后我們?cè)谥貑GINX。如果我們是寶塔面板的話,直接用上面的替換掉WP的偽靜態(tài)。

如果我們是Apache,一般是啟動(dòng)APC緩存方式,那我們?cè)趥戊o態(tài)中也需要調(diào)整的。

# BEGIN CACHIFY
<IfModule mod_rewrite.c>
# ENGINE ON
RewriteEngine on
RewriteBase /

# set hostname directory
RewriteCond %{HTTPS} on
RewriteRule .* - [E=CACHIFY_HOST:https-%{HTTP_HOST}]
RewriteCond %{HTTPS} off
RewriteRule .* - [E=CACHIFY_HOST:%{HTTP_HOST}]

# set subdirectory
# sometimes %{REQUEST_URI} might be an empty string, so /$ won't match
RewriteCond %{REQUEST_URI} /$
RewriteRule .* - [E=CACHIFY_DIR:%{REQUEST_URI}]
RewriteCond %{REQUEST_URI} ^$
RewriteRule .* - [E=CACHIFY_DIR:/]

# gzip
RewriteRule .* - [E=CACHIFY_SUFFIX:]
<IfModule mod_mime.c>
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule .* - [E=CACHIFY_SUFFIX:.gz]
AddType text/html .gz
AddEncoding gzip .gz
</IfModule>

# Main Rules
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-content/cache)/.*
RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html -f
RewriteRule ^(.*) /wp-content/cache/cachify/%{ENV:CACHIFY_HOST}%{ENV:CACHIFY_DIR}index.html%{ENV:CACHIFY_SUFFIX} [L]
</IfModule>
# END CACHIFY

這是HTTPS方式,如果我們是HTTP,則需要這樣。

# BEGINN CACHIFY
<IfModule mod_rewrite.c>
# ENGINE ON
RewriteEngine On

# GZIP FILE
<IfModule mod_mime.c>
RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-content/cache)/.*
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} =""
RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{DOCUMENT_ROOT}/path to/wp-content/cache/cachify/%{HTTP_HOST}%{REQUEST_URI}index.html.gz -f
RewriteRule ^(.*) /path to/wp-content/cache/cachify/%{HTTP_HOST}%{REQUEST_URI}index.html.gz [L]

AddType text/html .gz
AddEncoding gzip .gz
</IfModule>

# HTML FILE
RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-content/cache)/.*
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} =""
RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
RewriteCond %{DOCUMENT_ROOT}/path to/wp-content/cache/cachify/%{HTTP_HOST}%{REQUEST_URI}index.html -f
RewriteRule ^(.*) /path to/wp-content/cache/cachify/%{HTTP_HOST}%{REQUEST_URI}index.html [L]
</IfModule>
# END CACHIFY
投上你的一票
 
  • 本文由 麥子 發(fā)表于 2024年11月1日 13:36:49
  • 轉(zhuǎn)載請(qǐng)務(wù)必保留本文鏈接:http://bjj.org.cn/wp-cachify.html