用 Varnish 加速 WordPress

25 Feb
2011
# 作者: riku / / 本文采用CC BY-NC-SA 2.5协议授权,转载请注明本文链接

上次在" 小内存 VPS 优化建议 " 一文中我们说到,有效提升网站访问速度的方案之一是使用缓存。所以,我们同样也可以在 VPS 上用基于缓存机制的 Varnish 来进一步提升 WordPress 的访问速度,Varnish 是一个轻量级、高效能的反向代理缓存服务器,特点是简单易用、速度快,但内存使用比较多。

下面简单介绍一下安装配置过程。

- 安装配置

安装 varnish

sudo apt-get install varnish

在 /etc/varnish/default.vcl 里输入以下规则

backend default {
.host = "localhost";
.port = "8080";
}
acl purge {
"localhost";
}
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return(lookup);
}
if (req.url ~ "^/$") {
unset req.http.cookie;
}
}
sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
}
sub vcl_miss {
if (req.request == "PURGE") {
error 404 "Not in cache.";
}
if (!(req.url ~ "wp-(login|admin)")) {
unset req.http.cookie;
}
if (req.url ~ "^/[^?]+.(jpeg|jpg|png|gif|ico|js|css|txt|gz|
zip|lzma|bz2|tgz|tbz|html|htm)(\?.|)$") {
unset req.http.cookie;
set req.url = regsub(req.url, "\?.$", "");
}
if (req.url ~ "^/$") {
unset req.http.cookie;
}
}
sub vcl_fetch {
if (req.url ~ "^/$") {
unset beresp.http.set-cookie;
}
if (!(req.url ~ "wp-(login|admin)")) {
unset beresp.http.set-cookie;
}
}

Varnish 会占用 80 端口,所以我们需要先更改web 服务器的端口为 8080,然后再启动 Varnish

sudo /etc/init.d/varnishd start

用 Curl 测试

riku@wowubuntu.com~$ curl -I http://wowubuntu.com
HTTP/1.1 200 OK
Server: nginx/0.8.54
Content-Type: text/html
Last-Modified: Fri, 25 Feb 2011 01:07:20 GMT
Content-Length: 42991
Date: Fri, 25 Feb 2011 07:26:19 GMT
X-Varnish: 959826775 959826503
Age: 119
Via: 1.1 varnish
Connection: keep-alive

从返回的结果可以看到已经启用了 varnish,另外我们也可以用 varnishstat 命令来查看运行数据及状况。

- 压力测试:

安装 Apache ab 工具

sudo apt-get install apache-utils

用 ab 进行测试

  • 没有启用 Varnish

li52-32 ~ # ab -n 1000 -c 100 http://wowubuntu.com:8080/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking wowubuntu.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software: nginx/0.8.54
Server Hostname: wowubuntu.com
Server Port: 8080

Document Path: /
Document Length: 0 bytes

Concurrency Level: 100
Time taken for tests: 23.895 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Total transferred: 450000 bytes
HTML transferred: 0 bytes
Requests per second: 41.85 [#/sec] (mean)
Time per request: 2389.537 [ms] (mean)
Time per request: 23.895 [ms] (mean, across all concurrent requests)
Transfer rate: 18.39 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 32 40 95.0 35 3034
Processing: 193 2230 422.7 2232 2960
Waiting: 193 2229 422.5 2232 2958
Total: 227 2270 432.9 2270 5148

Percentage of the requests served within a certain time (ms)
50% 2270
66% 2391
75% 2497
80% 2555
90% 2705
95% 2773
98% 2842
99% 2894
100% 5148 (longest request)

  • 启用了 Varnish

li52-32 ~ # ab -n 1000 -c 100 http://wowubuntu.com/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking wowubuntu.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software: nginx/0.8.54
Server Hostname: wowubuntu.com
Server Port: 80

Document Path: /
Document Length: 13157 bytes

Concurrency Level: 100
Time taken for tests: 2.124 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 13755352 bytes
HTML transferred: 13472584 bytes
Requests per second: 470.71 [#/sec] (mean)
Time per request: 212.444 [ms] (mean)
Time per request: 2.124 [ms] (mean, across all concurrent requests)
Transfer rate: 6323.07 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 32 36 6.9 34 70
Processing: 131 154 59.4 140 473
Waiting: 33 47 57.9 34 355
Total: 164 190 64.4 174 542

Percentage of the requests served within a certain time (ms)
50% 174
66% 177
75% 182
80% 183
90% 197
95% 228
98% 522
99% 525
100% 542 (longest request)

我们可以看到,启用了Varnish 的 Requests per second 值明显要大很多,说明负载能力更强。

最后你也可以用 whichloadsfaster.com 来比较测试一下,多试几次,可以看最后的平均值,明显启用了 Varnish 的要快很多。

[参考]

加入我们 / 来 Wow!Ubuntu 问答社区参与更多讨论

11 Responses to 用 Varnish 加速 WordPress

Avatar

高性价比 Ubuntu VPS [新闻] | Wow!Ubuntu

WordPress 3.1 WordPress 3.1

February 25th, 2011 at 3:36 pm

[...] 用 Varnish 加速 WordPress [...]

Avatar

Mucid

Firefox 3.6.13 Firefox 3.6.13 Ubuntu 10.10 Ubuntu 10.10

February 25th, 2011 at 6:59 pm

我的https怎么办?再监听一个?

Google Chrome 9.0.597.98 Google Chrome 9.0.597.98 GNU/Linux GNU/Linux

riku Reply:

@Mucid, 这个还没研究,你看一下官方文档吧。

Avatar

杰林修

Google Chrome 9.0.597.98 Google Chrome 9.0.597.98 Windows XP Windows XP

February 25th, 2011 at 10:15 pm

监听ip应该就可以共用80了吧,和lanmp一样。

看你的说明,感觉和用了wp-super cache差不多啊

Google Chrome 9.0.597.98 Google Chrome 9.0.597.98 GNU/Linux GNU/Linux

riku Reply:

@杰林修, 比 wp-super cache 效果更好,因为它是缓存在内存中的,不是磁盘上。

Avatar

小内存VPS的生存之道:Nginx + PHP FPM + Varnish » Little Tail

WordPress 3.1 WordPress 3.1

February 28th, 2011 at 10:32 pm

[...] /etc/default/varnish 应改成START=yes,否则会出现这个错误Not starting HTTP accelerator varnishd (http://twitter.com/#!/grosser/status/5249558112108544);另一个是/etc/varnish/default.vcl,我参考了http://wowubuntu.com/varnish.html 以及http://blog.mudy.info/2009/04/my-varnish-vcl-for-wordpress/,这里列出我的配置文件/etc/varnish/default.vcl: [...]

Avatar

猴子

Google Chrome 10.0.648.133 Google Chrome 10.0.648.133 Windows 7 Windows 7

March 15th, 2011 at 11:26 am

作个记号。

Avatar

第六空间

Google Chrome 7.0.517.44 Google Chrome 7.0.517.44 Windows 7 Windows 7

March 20th, 2011 at 10:09 am

安装个测试一下。我是没有明显的感觉到。对别人就不知道了。

Avatar

fish

Firefox 3.0.19 Firefox 3.0.19 Windows Vista Windows Vista

April 29th, 2011 at 9:50 pm

varnish可用来安装http proxy吗

Avatar

冠朔wordpress插件

Internet Explorer 9.0 Internet Explorer 9.0 Windows 7 x64 Edition Windows 7 x64 Edition

April 27th, 2012 at 1:22 pm

配置太麻烦了。

Avatar

网兆网络

Google Chrome 22.0.1229.94 Google Chrome 22.0.1229.94 Windows 7 Windows 7

November 14th, 2012 at 4:50 pm

top