一、环境:
1、操作系统:Centos 7.3
2、Varnish:5.2.1
3、关闭防火墙:systemctl stop firewalld (开机禁止启动防火墙:systemctl disable firewalld )
4、关闭selinux:setenforce 0
二、Varnish部署安装
1、yum 安装 varnish 5.2.1
(1)安装EPEL源:
yum -y install epel-release
(2)安装用于校验签名的工具:
yum -y install pygpgme yum-utils
(3)新建一个repo源:
vi /etc/yum.repos.d/varnishcache_varnish5.repo
写入如下内容:
[varnishcache_varnish5] name=varnishcache_varnish5 baseurl=https://packagecloud.io/varnishcache/varnish5/el/7/$basearch repo_gpgcheck=1 gpgcheck=0 enabled=1 gpgkey=https://packagecloud.io/varnishcache/varnish5/gpgkey sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt metadata_expire=300 [varnishcache_varnish5-source] name=varnishcache_varnish5-source baseurl=https://packagecloud.io/varnishcache/varnish5/el/7/SRPMS repo_gpgcheck=1 gpgcheck=0 enabled=1 gpgkey=https://packagecloud.io/varnishcache/varnish5/gpgkey sslverify=1 sslcacert=/etc/pki/tls/certs/ca-bundle.crt metadata_expire=300
保存,然后执行:
yum -q makecache -y --disablerepo ='*'--enablerepo ='varnishcache_varnish5'
(4)现在就可以用yum来安装Varnish了:
yum -y install varnish
三、配置Varnish
1、修改Management进程配置文件
Management进程的配置文件为/etc/varnish/varnish.params。主要是修改端口和缓存类型及大小:
VARNISH_LISTEN_PORT=80 #varnish监听的端口,因为varnish要作为web服务器的反代进行工作时,才能将http的内容缓存,一般要将其改为80 端口,但是实际生产环境中,varnish一般是处于前端调度器的后面,所以可以在前端调度器上将调度的端口改为此处 的端口也可以。
VARNISH_STORAGE="malloc,256M" #“malloc,256M”为基于内存缓存,缓存大小256M,"file,/data/cache,1G"为基于磁盘缓存,/data/cache为缓存存 放路径。
2、修改Varnish总配置文件
Varnish的总配置文件为/etc/varnish/default.vcl:
vcl 4.0; import directors; #加载后端的轮询模块 probe backend_healthcheck { #设置名为backend_healthcheck的健康监测 .url = "/index.html"; .window = 3; #基于3次检查来判断其健康状态 .threshold = 2; #window的3次检查中至少有2次是成功的 .interval = 3s; #每隔3秒检查一次 .timeout = 1s; #超时 } backend web1 { #设置后端server .host = "10.207.252.*"; .port = "8888"; .probe = backend_healthcheck; } backend web2 { .host = "10.207.252.*"; .port = "8888"; .probe = backend_healthcheck; } sub vcl_init { new img_cluster = directors.round_robin(); #把web1和web2配置为轮询集群,取名为img_cluster img_cluster.add_backend(web1); img_cluster.add_backend(web2); } acl purgers { #定义可访问来源IP,权限控制 "127.0.0.1"; "10.207.0.0"/16; } sub vcl_recv { set req.backend_hint = img_cluster.backend(); #流量转发给所有结点,不加这条所有流量将只转发给web1 if (req.method == "GET") { #正常请求发给hash处理 return(hash); } if (req.method != "GET" && req.method != "HEAD" && req.method != "PUT" && req.method != "POST" && req.method != "TRACE" && req.method != "OPTIONS" && req.method != "PURGE" && req.method != "DELETE") { return (pipe); #除了上边的请求头部,通过通道直接扔后端的pass } if (req.method == "PURGE") { #PURGE请求的处理的头部,清缓存 if (client.ip ~ purgers) { return(purge); } } } sub vcl_hash { #定义vcl_hash 引擎,没有定义hit和Miss的路径,所以走默认路径 hash_data(req.url); } sub vcl_backend_response { #自定义缓存文件的缓存时长 if (bereq.url ~ "\.(jpg|jpeg|gif|png)$") { set beresp.ttl = 1d; } } sub vcl_deliver { if (obj.hits > 0) { #为响应添加X-Cache首部,显示缓存是否命中 set resp.http.X-Cache = "HIT"; } else { set resp.http.X-Cache = "MISS"; } unset resp.http.X-Powered-By; #取消显示php框架版本的header头 unset resp.http.Via; #取消显示varnish的header头 }
四、启动Varnish
systemctl start varnish
五、通过Varnish管理工具查看状态
varnishadm -S secret -T 127.0.0.1:6082
参考文章
1、https://www.cnblogs.com/keerya/p/7833724.html
2、https://www.cnblogs.com/mrlapulga/p/6885423.html
3、https://blog.51cto.com/13004186/1982356
5、https://blog.51cto.com/14158297/2350244?source=dra