varnish介绍
- Varnish cache,或称Varnish,是一套高性能的反向网站缓存服务器(reverse proxy server)
- varnish拥有两套配置文件;一套配置文件用于varnish自身进程的参数配置,另一套用于定义缓存规则;定义缓存规则需要使用灵活的语言来定义,这就是VCL(varnish语言);应用时需要将VCL编写的规则送给VCC编译后才能运行,所以安装varnish需要依赖gcc编译器。
安装
- 下载相应的软件包
jemalloc-3.6.0-1.el7.x86_64.rpm
jemalloc-devel-3.6.0-1.el7.x86_64.rpm
varnish-4.0.5-1.el7.x86_64.rpm
varnish-libs-4.0.5-1.el7.x86_64.rpm
- 安装软件包
yum install -y jemalloc-3.6.0-1.el7.x86_64.rpm jemalloc-devel-3.6.0-1.el7.x86_64.rpm
yum install -y varnish-libs-4.0.5-1.el7.x86_64.rpm varnish-4.0.5-1.el7.x86_64.rpm
- 开启
systemctl start varnish
- varnish 主配置文件:
/etc/varnish/default.vcl
实现varnish反向代理
实验环境:
server1: varnish (192.168.43.10)
server2: 后端Apache服务器(192.168.43.2)
server3: 测试机(192.168.43.3)
- server1中安装后:
vim /etc/varnish/default.vcl
编写配置文件
backend default {
.host = "192.168.43.2";##serve2的ip
.port = "80";
}
- server2中安装apache并编写测试页:
- yum install -y httpd
- systemctl start httpd
- vim /var/www/html/index.html
[root@server2 html]# cat index.html
hello server2
- 修改varnish端口
- 用户访问web服务器也是80端口,若要做反向代理,即表示用户访问varnish服务器是80端口。故需要更改varnish端口:
在server1:
[root@server1 varnish]# ls
default.vcl secret varnish.params
[root@server1 varnish]# vim varnish.params
[root@server1 varnish]#
VARNISH_LISTEN_PORT=80
- 打开varnish
[root@server1 varnish]# systemctl restart varnish
- 测试:
在server3中写入主机解析
vim /etc/hosts/
- curl server1
- curl -I server1
- 反向代理成功
判断是否加速
- 在varnish服务器主配置文件vcl_deliver模块中写入:
sub vcl_deliver {
# Happens before we check if we have this in cache already.
#