Webinoly 安裝 Rocket-Nginx 快取外掛

·

WP Rocket

Photo by Jean-Philippe Delberghe

Rocket-Nginx 是 WordPress 快取外掛中 WP Rocket 的 Nginx 的配置,主要能使 Nginx 服務器能夠直接提供快取快取的文件,而無經過 WordPress 或 PHP 產生快取,它還添加標頭來緩存 CSS、JS 和圖片,以便通過減少對 Web 服務器的請求來利用瀏覽器的快取;

這也是我們常說的快取文件繞過 PHP 程序,利用 Nginx 服務器至接傳給客戶端的瀏覽器,達到網站加速的作用。

這個 Rocket-Nginx 快取配置文件,是由 Maxime Jobin (@maximejobin) 創建及維護,可以參考我之前「EasyEngine 上安裝 WP Rocket 快取外掛」這一篇文章。

2023/12/10 補充:

最早 (2018 年) 這個配置是由 Maxime Jobin ( @maximejobin ) 創建,現在是由 SatelliteWP 負責維護,依這個狀況看起來至少不會爛尾。

EasyEngine 上安裝 WP Rocket 快取外掛

https://yungke.me/wp-rocket-in-easyengine/

Webinoly 與 EasyEngine 相同性很高,但還是有一點不同,Rocket-Nginx 配置需要修改來配合 Webinoly 系統

停用 WordPress 計畫任務

Rocket-Nginx 配置後,會直接提供快取文件而無需從 WordPress 執行任何 PHP 程序,這可能會導致您的 WordPress 計畫任務會沒有作用。

這是因為 WordPress 計畫任務 (WP-Cron) 不像 VPS 主機端的 Linux cron 作業,只有在客戶端 (讀者) 在訪問您的網站時才會觸發 WordPress 執行。

最好的方法是禁用 WordPress cron 作業,改用後端的主機來執行。

在 wp-config.php 文件裡加入禁用代碼:

define('DISABLE_WP_CRON', true);

後端主機 crontab 裡加入每 15 分鐘就執行一次 php wp-cron.php 任務:

*/15 * * * * cd /home/user/public_html; php wp-cron.php &>/dev/null

安裝 Rocket-Nginx 腳本

注意:Webinoly 的網站配置是要基本的 sudo site domain.com -wp 網站才行,如果帶有 -cache 的 Nginx FastCgi Cache 是不可以的。

在 Nginx 配置目錄中創建 rocket-nginx 文件資料夾:

cd /etc/nginx
git clone https://github.com/maximejobin/rocket-nginx.git

rocket-nginx 2.0 版本之後,必須使用 rocket-nginx 腳本生成配置文件,要生成默認配置,您必須重命名已禁用的 ini 文件並運行配置生成器。

cd rocket-nginx
cp rocket-nginx.ini.disabled rocket-nginx.ini
php rocket-parser.php

新生成的 rocket-nginx 配置文件會在 /etc/nginx/rocket-nginx 資料夾裡,檔名為 default.conf。

將 default.conf 下載下來,用 Notepad++ 來修改,例如:HTTPS、CSS、JS、圖片快取部分要禁用,避免與 Webinoly 配置衝突,修改後的配置如下:

# Version 2.1.1
#
###################################################################################################
# Add debug information into header
set $rocket_debug 0;
###################################################################################################
# Do not alter theses values
#
set $rocket_bypass 1;				# Should NGINX bypass WordPress and call cache file directly ?
set $rocket_encryption "";			# Is GZIP accepted by client ?
set $rocket_file "";				# Filename to use
set $rocket_is_bypassed "No";		# Header text added to check if the bypass worked or not. Header: X-Rocket-Nginx-Serving-Static
set $rocket_reason "";				# Reason why cache file was not used. If cache file is used, what file was used
set $rocket_https_prefix "";		# HTTPS prefix to use when cached files are using HTTPS
set $rocket_hsts 0;					# Is HSTS is off (0) by default. Will be turned on (1) if request is HTTPS
###################################################################################################
# PAGE CACHE
#
if ($http_accept_encoding ~ gzip) {
	set $rocket_encryption "_gzip";
}

# Is Brotli accepted by client ?
if ($http_accept_encoding ~ br) {
	set $rocket_encryption "";
}

# Is SSL request ?
if ($https = "on") {
	set $rocket_https_prefix "-https";
	set $rocket_hsts 1;
}

# If HSTS is disabled, unset HSTS set for Rocket-Nginx configuration
if ($rocket_hsts = "0") {
	set $rocket_hsts_value "";
}

# File/URL to return IF we must bypass WordPress
# Desktop: index.html or index-https.html
# Mobile:  index-mobile.html or index-mobile-https.html
set $rocket_end "/cache/wp-rocket/$http_host/$request_uri/index$rocket_https_prefix.html$rocket_encryption";
set $rocket_url "/wp-content$rocket_end";
set $rocket_file "$document_root/wp-content$rocket_end";
set $rocket_mobile_detection "$document_root/wp-content/cache/wp-rocket/$http_host/$request_uri/.mobile-active";

# Do not bypass if it's a POST request
if ($request_method = POST) {
	set $rocket_bypass 0;
	set $rocket_reason "POST request";
}

# Do not bypass if arguments are found (e.g. ?page=2)
if ($is_args) {
	set $rocket_bypass 0;
	set $rocket_reason "Arguments found";
}

# Do not bypass if the site is in maintenance mode
if (-f "$document_root/.maintenance") {
	set $rocket_bypass 0;
	set $rocket_reason "Maintenance mode";
}

# Do not bypass if one of those cookie if found
# wordpress_logged_in_[hash] : When a user is logged in, this cookie is created (we'd rather let WP-Rocket handle that)
# wp-postpass_[hash] : When a protected post requires a password, this cookie is created.
if ($http_cookie ~* "(wordpress_logged_in_|wp\-postpass_|woocommerce_items_in_cart|woocommerce_cart_hash|wptouch_switch_toogle|comment_author_|comment_author_email_)") {
	set $rocket_bypass 0;
	set $rocket_reason "Cookie";
}

if (-f "$rocket_mobile_detection") {
	set $rocket_bypass 0;
	set $rocket_reason "Specific mobile cache activated";	
}

# Do not bypass if the cached file does not exist
if (!-f "$rocket_file") {
	set $rocket_bypass 0;
	set $rocket_reason "File not cached";
}

# If the bypass token is still on, let's bypass WordPress with the cached URL
if ($rocket_bypass = 1) {
	set $rocket_is_bypassed "Yes";
	set $rocket_reason "$rocket_url";
}

# Clear variables if debug is not needed
if ($rocket_debug = 0) {
	set $rocket_reason "";
	set $rocket_file "";
}

# If the bypass token is still on, rewrite according to the file linked to the request
if ($rocket_bypass = 1) {
	rewrite .* "$rocket_url" last;
}

# Add header to HTML cached files
location ~ /wp-content/cache/wp-rocket/.*html$ {
	etag on;
	add_header Vary "Accept-Encoding, Cookie";
	add_header Cache-Control "no-cache, no-store, must-revalidate";
	add_header X-Rocket-Nginx-Serving-Static $rocket_is_bypassed;
	add_header X-Rocket-Nginx-Reason $rocket_reason;
	add_header X-Rocket-Nginx-File $rocket_file;
	add_header Strict-Transport-Security "$rocket_hsts_value";
}

# Do not gzip cached files that are already gzipped
location ~ /wp-content/cache/wp-rocket/.*_gzip$ {
	etag on;
	gzip off;
	types {}
	default_type text/html;
	add_header Content-Encoding gzip;
	add_header Vary "Accept-Encoding, Cookie";
	add_header Cache-Control "no-cache, no-store, must-revalidate";
	add_header X-Rocket-Nginx-Serving-Static $rocket_is_bypassed;
	add_header X-Rocket-Nginx-Reason $rocket_reason;
	add_header X-Rocket-Nginx-File $rocket_file;
	add_header Strict-Transport-Security "$rocket_hsts_value";
}

# Debug header (when file is not cached)
add_header X-Rocket-Nginx-Serving-Static $rocket_is_bypassed;
add_header X-Rocket-Nginx-Reason $rocket_reason;
add_header X-Rocket-Nginx-File $rocket_file;

修改好之後,另存新檔名為 wprocket-nginx.conf,然後用 SFTP 上傳到域名資料夾下,如圖:

WP Rocket Nginx 配置

修改 Webinoly 原本的 domain.conf 的配置

打開 /etc/nginx/sites-available/domain.conf 文件,註釋掉 headers-http.conf 和 headers-html.conf,如下:

#include common/headers-http.conf;
include common/headers-https.conf;
#include common/headers-html.conf;

重啟 Nginx,讓 wprocket-nginx.conf 配置生效。

nginx -t && service nginx restart

檢查 Rocket-Nginx 狀態

正常一般的網站執行 WP Rocket 快取外掛,也會執行 PHP 程序,就如:

Nginx >> PHP-FPM >> PHP >> 靜態文件

經過 Rocket-Nginx 配置過,快取過程會是:

Nginx >> 靜態文件

也就是說,我們直接從 Nginx 提供靜態文件給客戶端,而不是在提供靜態文件之前將請求傳遞給 FPM 然後再傳遞給 PHP。

先將 wprocket-nginx.conf 打開,把 debug 打開 (0 改為 1),修改如下:

set $rocket_debug 1;

保存文件,再重啟一次 Nginx

利用 Chrome F12 查看配置是否正確繞過 PHP

WP Rocket-conf debug

x-rocket-nginx-serving-static: Yes 表示配置正確
x-rocket-nginx-serving-static: No 表示未繞過 PHP
x-rocket-nginx-file 表示讀取靜態文件的路徑

自定設定

使用預設的設定就可以,無需進行任何配置。

但是,也可以編輯一些你想要的設定。

打開 rocket-nginx.ini 文件去修改。

編輯 ini 檔案後,必須再一次執行生成器重新產生 Nginx 設定檔:

php rocket-parser.php

偵錯

Nginx 設定好之後,如何知道 Rocket-Nginx 是否正確的運行:

可以打開 Chrome DevTools F12 開發者工具,檢查 Response headers 標頭。

WP Rocket

詳細的標頭說明,請參考 Github

https://github.com/SatelliteWP/rocket-nginx

結語

在測試過程中,如果 WP Rocket 控制台,將「針對行動裝置建立獨立的快取檔案」這一項目打勾的話,就會無法繞過 WordPress 或 PHP 產生快取。

檢視都沒問題的話,記得將 debug 改為 0,重啟 Nginx,沒必較將快取路徑顯示出來。

hosted by kinsta

Comments

發佈留言

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

Hey, 想嘗試 Kinsta 主機嗎?

18748

Kinsta 高效能主機

wPowered Starter 方案

馬上訂購

18749
Your Mastodon Instance