RHEL 6.5----Varnish缓存服务器
主机名 | IP | 服务 |
master | 192.168.30.130 | varnish |
slave | 192.168.30.131 | httpd |
WebServer | 192.168.30.132 |
官网:
http://www.varnish-cache.org/releases/index.html
软件包官方下载地址:
https://packagecloud.io/varnishcache/varnish41?page=1
https://packagecloud.io/varnishcache/varnish41
官方安装说明:
https://packagecloud.io/varnishcache/varnish41/install#bash-rpm
https://packagecloud.io/varnishcache/varnish60/install#manual-rpm
GitHub地址:
https://github.com/varnishcache/varnish-cache
软件包库:
https://github.com/varnishcache/pkg-varnish-cache
准备安装文件
安装epel-release(官方给出的epel源地址是fedora:https://fedoraproject.org/wiki/EPEL,我使用的是中科大的)
[root@master ~]# wget http://mirrors.ustc.edu.cn/epel/epel-release-latest-6.noarch.rpm
[root@master ~]# rpm -ivh epel-release-latest-.noarch.rpm
或者直接
[root@master ~]# rpm -ivh http://mirrors.ustc.edu.cn/epel/epel-release-latest-6.noarch.rpm
Retrieving http://mirrors.ustc.edu.cn/epel/epel-release-latest-6.noarch.rpm
warning: /var/tmp/rpm-tmp.TB6RfW: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [%]
:epel-release ########################################### [%]
[root@master ~]# ls /etc/yum.repos.d/ | grep epel
epel.repo
epel-testing.repo
准备varnish的yum源
[root@master ~]# curl -s https://packagecloud.io/install/repositories/varnishcache/varnish41/script.rpm.sh | sh //这个脚本会去检测一些依赖,没有回自动安装,前提是配置好YUM源
Detected operating system as redhatenterpriseserver/.
Checking for curl...
Detected curl...
Downloading repository file: https://packagecloud.io/install/repositories/varnishcache/varnish41/config_file.repo?os=redhatenterpriseserver&dist=6&source=script
done.
Installing pygpgme to verify GPG signatures...
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
epel/metalink | 5.1 kB :
epel | 4.7 kB :
epel/primary_db | 6.0 MB :
rhel-source | 3.9 kB : ...
rhel-source/primary_db | 3.1 MB : ...
https://packagecloud.io/varnishcache/varnish41/el/6/SRPMS/repodata/repomd.xml: [Errno 14] PYCURL ERROR 7 - "Failed to connect to 2600:1fa0:c040:111:34db:182c::: Network is unreachable"
Trying other mirror.
Error: Cannot retrieve repository metadata (repomd.xml) for repository: varnishcache_varnish41-source. Please verify its path and try again
Installing yum-utils...
Loaded plugins: product-id, refresh-packagekit, security, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
varnishcache_varnish41-source/signature | B :
Retrieving key from https://packagecloud.io/varnishcache/varnish41/gpgkey
Importing GPG key 0xCBF5C49E:
Userid: "https://packagecloud.io/varnishcache/varnish41 (https://packagecloud.io/docs#gpg_signing) <support@packagecloud.io>"
From : https://packagecloud.io/varnishcache/varnish41/gpgkey
varnishcache_varnish41-source/signature | 1.0 kB : ...
varnishcache_varnish41-source/primary | 2.3 kB :
varnishcache_varnish41-source /
Setting up Install Process
Package yum-utils-1.1.-.el6.noarch already installed and latest version
Nothing to do
Generating yum cache for varnishcache_varnish41...
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Importing GPG key 0xCBF5C49E:
Userid: "https://packagecloud.io/varnishcache/varnish41 (https://packagecloud.io/docs#gpg_signing) <support@packagecloud.io>"
From : https://packagecloud.io/varnishcache/varnish41/gpgkey The repository is setup! You can now install packages.
[root@master ~]# ls /etc/yum.repos.d/ | grep varnish
varnishcache_varnish41.repo
[root@master ~]# cat /etc/yum.repos.d/varnishcache_varnish41.repo
[varnishcache_varnish41]
name=varnishcache_varnish41
baseurl=https://packagecloud.io/varnishcache/varnish41/el/6/$basearch
repo_gpgcheck=
gpgcheck=
enabled=
gpgkey=https://packagecloud.io/varnishcache/varnish41/gpgkey
sslverify=
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire= [varnishcache_varnish41-source]
name=varnishcache_varnish41-source
baseurl=https://packagecloud.io/varnishcache/varnish41/el/6/SRPMS
repo_gpgcheck=
gpgcheck=
enabled=
gpgkey=https://packagecloud.io/varnishcache/varnish41/gpgkey
sslverify=
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
metadata_expire=
安装varnish
[root@master ~]# yum install -y varnish //国外的网站可能会有些慢,依个人网络环境不同,可能有时会因为连接超时而失败,这时候只能去一个一个下载,然后使用rpm一个一个安装
[root@master ~]# varnish //安装完成后的一些命令
varnishadm varnishlog varnishstat
varnishd varnishncsa varnishtest
varnishhist varnish_reload_vcl varnishtop
[root@master ~]# varnishd -V //查看varnish的版本号
varnishd (varnish-4.1. revision 1d090c5a08f41c36562644bafcce9d3cb85d824f)
Copyright (c) Verdens Gang AS
Copyright (c) - Varnish Software AS
配置文件位置
[root@master ~]# ls /etc/sysconfig/varnish
/etc/sysconfig/varnish //主配置文件 [root@master ~]# ls /etc/varnish/default.vcl
/etc/varnish/default.vcl //VCL配置文件
修改内容
[root@master ~]# cp /etc/varnish/default.vcl{,.bak}
[root@master ~]# vim /etc/varnish/default.vcl backend default {
.host = "192.168.30.131";
.port = "";
}
.......... sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
if (obj.hits > ) {
set resp.http.X-Cache = "HIT from sishen cache";
}
else {
set resp.http.X-Cache = "MISS from sishen cache";
}
return (deliver);
}
[root@master ~]# cp /etc/sysconfig/varnish{,.bak}
[root@master ~]# vim /etc/sysconfig/varnish
.....
VARNISH_LISTEN_PORT=
.....
重启varnish
[root@master ~]# service varnish restart
在slave上部署web服务器,进行测试
[root@slave ~]# yum install -y httpd
[root@slave ~]# echo "<h1>This is a varnish test page\! </h1>" > /var/www/html/index.html
重启httpd
[root@slave ~]# service httpd restart
在物理机上使用Firefox直接访问slave,验证slave是否正常:
ok,没问题。
测试varnish:
刷新再次查看
Linux终端查看
这里如果缓存总是没有命中,需要修改httpd.conf文件,将KeepAlive的原值"off"修改为"on",重启httpd服务再次测试。
使用varnish缓存不同的域名网站,配置第二台web服务器:
[root@WebServer ~]# yum install -y httpd
[root@WebServer ~]# echo "<h1>#######This is the WebServer 192.168.30.132########</h1>" > /var/www/html/index.html
[root@WebServer ~]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
修改varnish配置文件
[root@master ~]# vim /etc/varnish/default.vcl
backend www {
.host = "192.168.30.132";
.port = "";
}
backend xueji {
.host = "192.168.30.131";
.port = "";
} sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
if (req.http.host ~ "^(www.)?sishen.cn") {
set req.http.host = "www.sishen.cn";
set req.backend_hint = www;
}
elif (req.http.host ~ "^xueji.sishen.cn") {
set req.backend_hint = xueji;
}
else {
return (synth(,"Sorry,The Page Not Found"));
}
} sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
if (obj.hits > ) {
set resp.http.X-Cache = "HIT from sishen cache";}
else {
set resp.http.X-Cache = "MISS from sishen cache";}
return (deliver);
}
重载配置文件
!!!注意:
varnish修改完配置文件后,使用reload方式更方便,如果配置文件有错误,可以直接指出错在哪里(具体到哪一行的那个字段)
在varnish上修改hosts文件
[root@master ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
:: localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.30.130 master
192.168.30.132 www.sishen.cn
192.168.30.131 xueji.sishen.cn
访问测试
使用物理机测试域名的话需要在C:\Windows\System32\drivers\etc\hosts文件末尾添加
192.168.30.130 xueji.sishen.cn
192.168.30.130 www.sishen.cn
linux终端测试
检测域名准确性
结果与上述default.vcl中的定义相同,即结果解析正确。
测试varnish的错误页面,直接IP访问
如果在default.vcl中不指定错误页面,结果会是怎样呢?
[root@master ~]# vim /etc/varnish/default.vcl sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
if (req.http.host ~ "^(www.)?sishen.cn") {
set req.http.host = "www.sishen.cn";
set req.backend_hint = www;
}
elif (req.http.host ~ "^xueji.sishen.cn") {
set req.backend_hint = xueji;
}
# else {
# return (synth(,"Sorry,The Page Not Found"));
# }
}
[root@master ~]# /etc/init.d/varnish reload
Loading vcl from /etc/varnish/default.vcl
Current running config name is reload_2018-05-23T172134
Using new config name reload_2018-05-23T173503
VCL compiled.
VCL 'reload_2018-05-23T173503' now active
available auto/cold 0 boot
available auto/cold 0 reload_2018-05-23T170836
available auto/cold 0 reload_2018-05-23T171224
available auto/warm 0 reload_2018-05-23T172134
active auto/warm 0 reload_2018-05-23T173503
Done
结果显示,如果没有制定错误页面,会访问到default.vcl中backend定义的第一个域名对应的页面。
varnish的缓存清理
需要注意的是curl -I www.sishen.cn与curl -I www.sishen.cn/index.html是两种访问方式
上述操作删除的是单个页面,删除所有页面使用通配符 .*$,删除一个目录使用 /admin/*
== 符合条件的 != 不符合条件的 ~ 正则匹配 !~ 非正则匹配
值得注意的是 == 、!= 、~、!~后面必须有空格
RHEL 6.5----Varnish缓存服务器的更多相关文章
- Varnish缓存服务器的搭建配置手册
Varnish缓存服务器的搭建配置手册 1.Varnish官方环境依赖提示 Installing Varnish Cache is as simple as enabling our package ...
- 利用varnish做Discuz论坛的缓存服务器
实验背景:公司有一台BBS服务器,用的是LNMP的架构搭建的.正好手头有一台空闲的虚拟机,于是想着给BBS前端加一台缓存服务器.于是选定了varnish,搜了很多教程,跌跌撞撞的完成了配置.这其中很多 ...
- 高性能缓存服务器Varnish
一.Varnish概述 Varnish是一款高性能的.开源的反向代理服务器和缓存服务器,计算机系统的除了有内存外,还有CPU的L1.L2,甚至L3级别的缓存,Varnish的设计架构就是利用操作系统的 ...
- Varnish,Nginx搭建缓存服务器
Varnish,Nginx搭建缓存服务器 一. varnish 1.安装pcre库,兼容正则表达式 # tar -zxvf pcre-8.10.tar.gz # cd pcre-8.10 # ./co ...
- Linux平台部署varnish 高性能缓存服务器
一:varnish部署前准备: 1.1相关软件以及系统,web服务 系统要求:Centos 6(以上) (64位) 相关中间件:varnish-4.0.2 1.2相关系统依赖包安装检查准备 1.2.1 ...
- Varnish缓存服务
Varnish缓存服务详解及应用实现 1.varnish的基本介绍 Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已 ...
- Varnish缓存服务详解及应用实现
1.varnish的基本介绍 Varnish 的作者Poul-Henning Kamp是FreeBSD的内核开发者之一,他认为现在的计算机比起1975年已经复杂许多.在1975年时,储存媒介只有 ...
- web缓存服务器varnish-4.1.6的部署及配置详解
web缓存服务器varnish-4.1.6的部署及配置详解 1.安装varnish4.1.6安装依赖 yum install -y autoconf automake jemalloc-devel l ...
- varnish缓存系统基础知识
缓存系统类型 1.页面缓存/pageCache 缓存静态资源(html js css image) 例如:varnish squid 2.数据缓存/dataCache 缓存应 ...
随机推荐
- Vue.js 使用 Swiper.js 在 iOS 11 时出现错误
前言 在H5项目中,需要用到翻页效果,通过 Swiper 来实现,安装 Swiper npm i swiper -S 但是实际使用中,发现低版本 iOS < 11 会出现下面这个错误: Synt ...
- ie8的圆角问题
pie.js的引用 1.在你的网页加载 PIE.js 脚本. 注意,用IE专用的注释,防止非IE浏览器下载. <!--[if lt IE 10]> <script type=&quo ...
- POJ3045 Cow Acrobats —— 思维证明
题目链接:http://poj.org/problem?id=3045 Cow Acrobats Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- codeforces 696C C. PLEASE(概率+快速幂)
题目链接: C. PLEASE time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- BZOJ1453: [WC2005]Dface双面棋盘
离线LCT维护MST,和3082的方法一样.然而比较码农,适合颓废的时候写. PS:线段树分治要好写得多,LCT比较自娱自乐. #include<bits/stdc++.h> using ...
- 【SDOI2009】SuperGCD
[题目链接] 点击打开链接 [算法] 1.关于求最大公约数的算法 若使用辗转相除法,那么显然会超时 不妨这样思考 : 要求gcd(a,b), 若a为偶数,b为偶数,则gcd(a,b) = 2 * gc ...
- javascript之常遇到的浏览器兼容问题和解决方法
转自http://www.cnblogs.com/duenyang/p/6066737.html 常遇到的关于浏览器的宽高问题: var winW=document.body.clientWidth| ...
- 【前端】CentOS 7 系列教程之四: 配置 git 服务器自动部署
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_4.html 安装pm2守护进程,备用 npm install -g pm2 创建/srv/www文件夹 ...
- LXD安装
#安装 #初始化(一路next) sudo lxd init #启动容器 lxc launch ubuntu:16.04 first #进到容器内 lxc exec first -- /bin/bas ...
- k8s-部署dashboard1.10.1-十七
一.获取镜像和填坑 我的k8s是1.13.1,这里dashboard用的1.10.1: 由于国内不能访问Google,而且大部分人可能也没有其他途径访问:只能在阿里云或者其他镜像网站上获取了: 镜像获 ...