Varnish 5.1 & WordPress 带来内存级加速
本文最后更新于 2550 天前,其中的信息可能已经有所发展或是发生改变。

前言

Varnish 是一款高性能的开源HTTP加速器,挪威最大的在线报纸 Verdens Gang 使用3台 Varnish 代替了原来的 12 台 Squid,性能比以前更好。(超级老的梗了,但是就这么用吧。)

(去年7月写的了)Varnish 和 WordPress 搭配是比较经典的组合了,目前解决方案也非常的多,可以完美兼容 WordPress,灰常的耐撕啊。目前很多教程主要都是 Varnish 3.x 的,但是目前 Varnish 4.1 了,因为规则有变化(子版本号一般来说规则不变,大版本号肯定是变得),所以就写个教程吧。

Varnish 5/5.1 并没有升级新的语法,只要是 vcl4.0 开头的依旧是向下兼容的,目前 Varnish 默认不提供 HTTPS 支持,不过有一个 Hitch 来帮助实现HTTPS和HTTP/2,这个之后会介绍。

安装

RHEL /CentOS 安装:

curl -s https://packagecloud.io/install/repositories/varnishcache/varnish5/script.rpm.sh | sudo bash
yum install varnish

Debian/Ubuntu 安装:

curl -s https://packagecloud.io/install/repositories/varnishcache/varnish5/script.deb.sh | sudo bash
apt-get install varnish

更多系统的安装请看:

https://www.varnish-cache.org/releases/index.html

https://packagecloud.io/varnishcache/varnish5/install

配置规则

修改 Nginx / Apache 端口

首先,如果我们要让用户在 Http (80)端口访问的就是 Varnish 的缓存效果的话,肯定是得把 80 端口让出来的。所以我们需要修改我们的 Web 服务器软件的端口,这里以 Nginx 为例(Apache 我有空写)。

在 conf 目录里找相关配置文件,一般来说 nginx.confvhost/*.conf 文件里都是配置信息,不同安装方式、面板不同。然后就会看到有 lisen 80 意思就是监听 80 端口,我们这里改成 lisen 8080 就变成监听 8080 端口了,你可以试试在浏览器里访问以下例如: http://www.mf8.biz:8080 这样的,如果不能访问的话,就检查一下服务器的防火墙是否有开放 8080 端口。阿里云、腾讯云、AWS 这样的云还有安全策略这样的功能,可能端口被禁也是这里。(其实外网能不能访问,问题也不大,内网可以就好。。。。。)

记得重启 Nginx。 service nginx restart

缓存规则

因为 Varnish 已经跨了个版本了,所以就得规则已经不适宜新的版本了,所以你网上找到的至少是国内博客分享的教程可能都已经不能用了。这里贴一个。 规则不是唯一的,大家可以寻找更好的,也可以学习着自己配置、完善。

修改 /etc/varnish/default.vcl 或者在 /etc/varnish/ 目录下添加一个 VCl 文件。


# Based on:https://gist.githubusercontent.com/ivmm/243998f8fd888f28dfe2a9173447ffa8/raw/8bf3aedca049224e81779aca3621fc6d16d2c108/default.vcl
# 修改第 18行 的 mf8.biz 为你的域名

vcl 4.0;
import std;

backend default {
 .host = "127.0.0.1";
 .port = "80";
}

acl purger {
"localhost";
"139.59.239.92";
}

sub vcl_recv {
 if (client.ip != "127.0.0.1" && std.port(server.ip) == 80 && req.http.host ~ "^(?i)mf8.biz") {
 set req.http.x-redir = "https://" + req.http.host + req.url;
 return(synth(850, "Moved permanently"));
 }
 if (req.method == "PURGE") {
 if (!client.ip ~ purger) {
 return(synth(405, "This IP is not allowed to send PURGE requests."));
 }
 return (purge);
 }
 if (req.restarts == 0) {
 if (req.http.X-Forwarded-For) {
 set req.http.X-Forwarded-For = client.ip;
 }
 }
 if (req.http.Authorization || req.method == "POST") {
 return (pass);
 }
 if (req.url ~ "/feed") {
 return (pass);
 }
 if (req.url ~ "wp-admin|wp-login") {
 return (pass);
 }
 set req.http.cookie = regsuball(req.http.cookie, "wp-settings-\d+=[^;]+(; )?", "");
 set req.http.cookie = regsuball(req.http.cookie, "wp-settings-time-\d+=[^;]+(; )?", "");
 if (req.http.cookie == "") {
 unset req.http.cookie;
 }
}



sub vcl_synth {
 if (resp.status == 850) { 
 set resp.http.Location = req.http.x-redir; 
 set resp.status = 302;
 return (deliver);
 }
}

sub vcl_purge {
 set req.method = "GET";
 set req.http.X-Purger = "Purged";
 return (restart);
}

sub vcl_backend_response {
 set beresp.ttl = 24h;
 set beresp.grace = 1h;
 if (bereq.url !~ "wp-admin|wp-login|product|cart|checkout|my-account|/?remove_item=") {
 unset beresp.http.set-cookie;
 }
}

sub vcl_deliver {
if (req.http.X-Purger) {
set resp.http.X-Purger = req.http.X-Purger;
 }
}

修改 Varnish 端口

Varnish 的默认端口是 6081,而不是 80 端口。所以还得改改,

一、修改 /lib/systemd/system/varnish.service 文件

ExecStart=/usr/sbin/varnishd -a :6081 -T localhost:6082 -f /etc/varnish/default.vcl -S /etc/varnish/secret -s malloc,256m

二、修改 /etc/default/varnish 文件

DAEMON_OPTS="-a :6081 \
             -T localhost:6082 \
             -f /etc/varnish/default.vcl \
             -S /etc/varnish/secret \
             -s malloc,256m -t 120"

将两个文件的 -a :6081 \ ,改为 -a :80 \ 就可以监听 80 端口了,当然,前提是没有东西占用你的 80 端口。

然后重启 varnish。

systemctl daemon-reload
service varnish restart

安装清理缓存的插件

因为缓存一直存着吧,你的内容就不变动了,所以呢,我们还需要 WordPress 的插件配合。目前的话,主流的缓存插件 WP Super cache,W3tc 之流都是有相关配合 Varnish 的功能的,但是目前公认最能配合 Varnish 的插件是:Varnish HTTP Purge。 很多洋鬼子也是建议就算你有 w3tc 这样的插件了,最好还是额外安装 Varnish HTTP Purge 插件。

评论

  1. 7年前
    2017-6-18 22:00:20

    博主,非常感谢你的一系列优化教程,我的疑问是这些方案对于小博客的可行性,我一台512内存的机子,有必要,或者说有能力用来搞这些吗?也许你可以提供一个对机子性能要求较低的方案??非常感谢

    • 妙正灰
      博主
      Ashin
      7年前
      2017-6-19 10:56:00

      高性能势必意味着高配置,我这个至少1.5G内存

本文评论已关闭
上一篇
下一篇