Varnish4.0实战 | 部署 | 后端服务器缓存 | 负载均衡 | 按域名访问 |CDN 推送
一、部署varnish
实验环境
172.25.1.1 varnish端
172.25.1.2 后端服务器
172.25.1.3 后端服务器
部署
Varnish 官网: http://varnish-cache.org/
Varnish 4.0 安装说明网址:https://packagecloud.io/varnishcache/varnish40/install\#manual-rpm
以 RHEL7 为例:
yum install pygpgme yum-utils
#配置 yum 源,注意里面的 7 是我的系统版本,el 指的是红帽系统
vim /etc/yum.repos.d/varnishcache_varnish40.repo
[varnishcache_varnish40]
name=varnishcache_varnish40
baseurl=https://packagecloud.io/varnishcache/varnish40/el/7/$basearch
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/varnishcache/varnish40/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
[varnishcache_varnish40-source]
name=varnishcache_varnish40-source
baseurl=https://packagecloud.io/varnishcache/varnish40/el/7/SRPMS
repo_gpgcheck=1
gpgcheck=0
enabled=1
gpgkey=https://packagecloud.io/varnishcache/varnish40/gpgkey
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=300
更新yum缓存
yum -q makecache -y --disablerepo='*' --enablerepo='varnishcache_varnish40'
去网上下载一个 jemalloc ,安装varnish需要年这个依赖
rpm -ivh jemalloc-3.6.0-1.el7.x86_64.rpm
安装 varnish4.0 版本
yum install varnish-devel.x86_64 varnish.x86_64 -y
二、实现单个后端服务器缓存
1、修改varnish策略
vim /etc/varnish/default.vcl
backend default {
.host = "172.25.1.2"; #后端服务器IP
.port = "80"; #后端服务器端口
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "访问的是varnish端";
}else {
set resp.http.X-Cache = "访问的是server2端";
}
return (deliver);
}
vim /etc/varnish/varnish.params
####修改
VARNISH_LISTEN_PORT=80 # varnish端口
重启
systemctl restart varnish
2、后端服务器安装Apache
yum install httpd -y
echo 'httpd' > /var/www/html/index.html
systemctl start httpd
3、测试
注:本实验测试机都是任意机
curl -I 172.25.1.1
第一次curl,可以看到Age=0,缓存时间为0,访问的是后端服务器
- 第二次第三次,可以看到Age不等于0,这是缓存了多久,可以看到访问的是varnish缓存端
清理所有缓存
varnishadm ban req.url “~” /
清楚页面缓存
varnishadm ban req.url “~” /index.html
三、多个域名访问不同服务器
1、修改varnish策略
vim /etc/varnish/default.vcl
###############修改
backend web1 {
.host = "172.25.1.2";
.port = "80";
}
backend web2 {
.host = "172.25.1.3";
.port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?server.org"){
set req.http.host = "www.server.org";
set req.backend_hint = web1;
return(pass); #不缓存,可以删去
} elseif (req.http.host ~ "^bbs.server.org") {
set req.http.host = "bbs.server.org";
set req.backend_hint = web2;
}else{
return (synth(405));
}
}
2、测试
在测试机上添加本地解析(实验缘故,没有办法)
vim /etc/hosts
##写入
172.25.1.1 server1 www.server.org server.org bbs.server.org
- 测试
四、负载均衡功能
此时工作在七层应用层,其实不太好,要多做一整套三次握手,负担加倍
1、修改varnish策略
vim /etc/varnish/default.vcl
################仅需以下部分
import directors from "/lib64/varnish/vmods/libvmod_directors.so"; #倒入负载均衡模块
backend web1 {
.host = "172.25.1.2";
.port = "80";
}
backend web2 {
.host = "172.25.1.3";
.port = "80";
}
sub vcl_init {
new lb=directors.round_robin();
lb.add_backend(web1);
lb.add_backend(web2);
}
sub vcl_recv {
if (req.http.host ~ "^(www.)?server.org"){
set req.http.host = "www.server.org";
set req.backend_hint = lb.backend();
return(pass); #不缓存
} elseif (req.http.host ~ "^bbs.server.org") {
set req.http.host = "bbs.server.org";
set req.backend_hint = web2;
}else{
return (synth(405));
}
}
重启
systemctl restart varnish
2、测试
五、Apache虚拟主机
在server2端
1、编辑Apache配置目录
vim /etc/httpd/conf.d/vhost.conf
###########写入
<VirtualHost *:80>
DocumentRoot /www
ServerName www.server.org
</VirtualHost>
<Directory "/www">
require all granted
</Directory>
<VirtualHost *:80>
DocumentRoot /bbs
ServerName bbs.server.org
</VirtualHost>
<Directory "/bbs">
require all granted
</Directory>
2、添加发布内容
mkdir /www
echo 'www' > /www/index.html
mkdir /bbs
echo 'bbs' > /bbs/index.html
systemctl restart httpd
3、测试
- 虚拟主机成功
六、CDN网页清理缓存
在varnish端
yum install httpd php -y
拥有以下安装包:
解压:
[root@server1 ~]# unzip bansys.zip -d /var/www/html/
-bash: unzip: command not found
[root@server1 ~]# yum whatprovides unzip
Loaded plugins: product-id, search-disabled-repos, subscription-
: manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
unzip-6.0-16.el7.x86_64 : A utility for unpacking zip files
Repo : rhel7.3
yum install unzip-6.0-16.el7.x86_64 -y
将解压文件放到Apache发布目录下:
cd /var/www/html/
mv bansys/* .
修改推送包配置文件:
vim config.php
留下以下内容:
修改Apache端口:
vim /etc/httpd/conf/httpd.conf
systemctl start httpd
浏览器访问:
配置varnish配置文件
vim /etc/varnish/default.vcl
#######写入
acl server {
"127.0.0.1";
"172.25.1.0/24"; #0这个网段的ip
}
sub vcl_recv {
if(req.method == "BAN"){
if (!client.ip ~ server){
return(synth(405,"Puring not allowed for" + client.ip));
}
ban("req.url ~ " + req.url);
return(purge);
}
}
重启
systemctl restart varnish
浏览器推送测试:
shell命令使varnish端产生缓存
浏览器清理缓存
- .* 代表清理所有缓存
缓存清理成功!
还没有评论,来说两句吧...