varnish是优秀的缓存服务器,配置简单,命中率高,性能高效.
下面我们以wordpress博客程序为例看看如何配置varnish.
1、首先安装好web环境,可以使用本站的LNMP一键安装包配置.
2、安装varnish.
rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm
yum install varnish
下载default.vcl文件覆盖 /etc/varnish/default.vcl
文件. http://centos.googlecode.com/files/default.vcl
3、修改varnish配置文件 /etc/varnish/default.vcl
:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
8080端口是后缀nginx或apache的端口.
# Only cache the following site
if (req.http.host ~ "(localhost)") {
set req.backend = default;
} else {
return (pass);
}
这个是只允许localhost缓存,需要改成自己的wordpress域名.
[.........]
set beresp.ttl = 1h;
[.........]
这个是设置缓存页面过期时间为1h,单位可以是s(秒),h(小时),d(天)等.
4、修改 /etc/sysconfig/varnish
配置文件: 重要的有两个值:
VARNISH_LISTEN_PORT=80 定义varnish的端口为80.
VARNISH_STORAGE_SIZE=1G 定义varnish最大缓存大小为1G
5、管理varnish的命令
service varnish start(restart|reload) 进程的启动,重启,配置重载.
varnishstat 实时查看varnish缓存命中状态.
varnishadm varnish后台管理命令.
6、最后需要安装wordpress清除varnish缓存的插件wordpress varnish: 可以在后台搜索,也可以直接下载http://wordpress.org/extend/plugins/wordpress-varnish/
. 经过简单的六步,就完全了wordpress varnish缓存的配置.