用varnish+openlite speed 给wordpress加速
varnish 配置如下:
vcl 4.0;
# 导入标准的 VCL 模块
import std;
# 后端定义,这里指向 OpenLiteSpeed 服务器
backend default {
. host = "OPENLITESPEED_IP";
. port = "80";
}
# 接收请求时的逻辑
sub vcl_recv {
# 根据域名处理请求
if (req.http.host ~ "(?i)^hearing-aid\.com$") {
set req.http.X-Cache-Site = "hearingaid.com";
} else if (req.http.host ~ "(?i)^site2\.com$") {
set req.http.X-Cache-Site = "site2";
} else if (req.http.host ~ "(?i)^site3\.com$") {
set req.http.X-Cache-Site = "site3";
} else {
return (synth(404, "Unknown Host"));
}
# 不缓存登录页和管理后台
if (req.url ~ "^/wp-(login|admin)") {
return (pass);
}
# 不缓存 POST 请求
if (req.method == "POST") {
return (pass);
}
return (hash);
}
# 配置缓存规则
sub vcl_backend_response {
# 针对动态内容不缓存
if (bereq.url ~ "^/wp-(login|admin)") {
set beresp.uncacheable = true;
set beresp.ttl = 0s;
return (deliver);
}
# 设置默认缓存时间
set beresp.ttl = 1h;
return (deliver);
}
# 自定义 404 错误处理
sub vcl_synth {
if (resp.status == 404) {
set resp.http.Content-Type = "text/html; charset=utf-8";
synthetic("<html><body><h1>404 Not Found</h1></body></html>
wordpress 可以安装 litespeed cache 和 varnish cache clear 插件。 进一步加速