varnish安装和配置
实验环境:CentOS7
Varnish是高性能开源的反向代理服务器和HTTP缓存服务器。
#varnish服务器:172.16.252.142
[root@varnish localhost]#yum -y install varnish
[root@varnish localhost]#cd /etc/varnish
[root@varnish localhost]#ls
default.vcl secret varnish.params
#varnish的配置文件
[root@varnish localhost]#vim varnish.params
#varnish需要缓存的服务器端口:
VARNISH_LISTEN_PORT=80
#管理varnish配置文件的工具的端口和地址
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082
#varnish缓存的两种方式:
VARNISH_STORAGE="malloc,256M"
#VARNISH_STORAGE="file,/data/cache/varnish_cache.bin,2G"
#单服务器的缓存配置:
#
[root@varnish localhost]#vim default.vcl
vcl 4.0; # Default backend definition. Set this to point to your content server.
#定义后端主机
backend default {
.host = "172.16.254.47";
.port = "80";
}
定义purger的访问控制
acl purgers {
"127.0.0.1"/8;
# "127.16.0.0"/16;
} sub vcl_recv {
#被允许的purger才允许清除缓存,否则,提示没有权限
if (req.method == "PURGE")
{
if (client.ip ~ purgers)
{
return(purge);
}
else
{
return(synth(405,"Purge not allowed" + client.ip));
}
} #定义/admin等文件不允许缓存
if (req.url ~ "(?i)^/(admin|login)")
{
return(pass);
}
#将客户端的ip访问记录在后端服务器
if (req.restarts == 0)
{
if (req.http.X-Forwarded-For)
{
set req.http.X-Forwarded-For = req.http.X-Forwarded-For+", "+ client.ip;
}
else
{
set req.http.X-Forwarded-For = client.ip;
}
}
}
#图片等静态资源的缓存
sub vcl_backend_response {
if (beresp.http.cache-control !~ "s-maxage")
{
if (bereq.url ~ "(?i)\.(jpg|jpeg|png|txt|gif|css|js)$")
{
unset beresp.http.Set-Cookie;
set beresp.ttl = 3600s;
}
}
}
#执行purge操作
sub vcl_purge {
return(synth(200,"Purged"));
}
#首部添加信息
sub vcl_deliver {
if (obj.hits>0)
{
set resp.http.X-Cache = "Hit via" + server.ip;
}
else
{
set resp.http.X-Cache = "Miss from" + server.ip;
}
#后端服务器:
[root@html localhost]#yum -y install httpd
[root@html localhost]#vim /etc/httpd/conf/httpd.conf
#将客户端ip计入访问日志
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined [root@html localhost]#pwd
/var/www/html
[root@html localhost]#ls
#在以下每个目录下新建index.html
admin index.html login test
#启动varnish的配置文件:
[root@~ localhost]#cd /etc/varnish/
[root@varnish localhost]#ls
default.vcl secret varnish.params
[root@varnish localhost]#varnishadm -S secret -T 127.0.0.1:6082
200
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux,3.10.0-514.el7.x86_64,x86_64,-smalloc,-smalloc,-hcritbit
varnish-4.0.4 revision 386f712 Type 'help' for command list.
Type 'quit' to close CLI session.
#列出使用的配置文件
vcl.list
200
available 0 boot
active 0 test1 help
200
help [<command>]
ping [<timestamp>]
auth <response>
quit
banner
status
start
stop
vcl.load <configname> <filename>
vcl.inline <configname> <quoted_VCLstring>
vcl.use <configname>
vcl.discard <configname>
vcl.list
param.show [-l] [<param>]
param.set <param> <value>
panic.show
panic.clear
storage.list
#重载新的配置,并命名为test3
vcl.load test3 default.vcl
200
VCL compiled.
#重载成功之后,使用
vcl.use test3
200
VCL 'test3' now active
vcl.list
200
available 0 boot
available 0 test1
active 0 test3
#测试:
#用curl测试purge,配置中允许127.0.0.1的主机进行purge操作
[root@varnish localhost]#curl -X PURGE http://127.0.0.1/index.html
<!DOCTYPE html>
<html>
<head>
<title>200 Purged</title>
</head>
<body>
<h1>Error 200 Purged</h1>
<p>Purged</p>
<h3>Guru Meditation:</h3>
<p>XID: 75</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>


#varnish反代两台服务器时:
[root@varnish localhost]#vim default.vcl
#导入varnish的模块:
import directors;
#定义后端服务器的健康状态检查;
probe healthychk {
#检查的页面地址
.url = "/index.html";
.timeout = 5s;
.window = 8;
.interval = 2s;
.threshold = 5;
}
#后端服务器1的命名:default
backend default {
.host = "172.16.254.47";
.port = "80";
#引用前面定义的检查策略
.probe = healthychk;
} #后端服务器2的命名:imgsrv
backend imgsrv {
.host = "172.16.253.177";
.port = "80";
.probe = healthychk;
}
#启动模块的初始化,添加后端服务器
sub vcl_init {
new staticsrvs = directors.round_robin();
staticsrvs.add_backend(default);
staticsrvs.add_backend(imgsrv);
}
#静态资源的调用设置:
sub vcl_recv {
if (req.url ~ "(?i)\.(jpg|jpeg|png|gif)$")
{
set req.backend_hint = imgsrv;
} else
{
set req.backend_hint = staticsrvs.backend();
} #静态资源撤销cookie设置,以便缓存
sub vcl_backend_response {
if (beresp.http.cache-control !~ "s-maxage")
{
if (bereq.url ~ "(?i)\.(jpg|jpeg|png|txt|gif|css|js)$")
{
unset beresp.http.Set-Cookie;
set beresp.ttl = 3600s;
}
}
#撤销服务器的私有ip地址以及版本等信息
sub vcl_deliver {
unset resp.http.Via;
unset resp.http.X-Varnish;
unset resp.http.X-Cache;
} #启动配置文件: varnish> vcl.load 3 default.vcl
200
VCL compiled.
vcl.use 3
200
VCL '3' now active
vcl.list
200
available 0 boot
available 0 test1
available 0 test3
available 0 1
available 0 2
active 0 3 backend.list
200
Backend name Refs Admin Probe
default(172.16.254.47,,80) 6 probe Healthy 8/8
imgsrv(172.16.253.177,,80) 3 probe Healthy 8/8
#default.vcl文件信息:
#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples. # Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0; import directors; probe healthychk {
.url = "/index.html";
.timeout = 5s;
.window = 8;
.interval = 2s;
.threshold = 5;
} # Default backend definition. Set this to point to your content server.
backend default {
.host = "172.16.254.47";
.port = "80";
.probe = healthychk;
} backend imgsrv {
.host = "172.16.253.177";
.port = "80";
.probe = healthychk;
} acl purgers {
"127.0.0.1"/8;
# "127.16.0.0"/16;
} sub vcl_init {
new staticsrvs = directors.round_robin();
staticsrvs.add_backend(default);
staticsrvs.add_backend(imgsrv);
} sub vcl_recv {
if (req.url ~ "(?i)\.(jpg|jpeg|png|gif)$")
{
set req.backend_hint = imgsrv;
} else
{
set req.backend_hint = staticsrvs.backend();
} if (req.method == "PURGE")
{
if (client.ip ~ purgers)
{
return(purge);
}
else
{
return(synth(405,"Purge not allowed" + client.ip));
}
} if (req.url ~ "(?i)^/(admin|login)")
{
return(pass);
} if (req.restarts == 0)
{
if (req.http.X-Forwarded-For)
{
set req.http.X-Forwarded-For = req.http.X-Forwarded-For+", "+ client.ip;
}
else
{
set req.http.X-Forwarded-For = client.ip;
}
}
}
# if (req.method == "PURGE")
# {
# if (client.ip ~ purgers)
# {
# return(purge);
# else
# {
# return(synth(405,"Purge not allowed" + client.ip)); }
# }
# }
# 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.
#} sub vcl_backend_response {
if (beresp.http.cache-control !~ "s-maxage")
{
if (bereq.url ~ "(?i)\.(jpg|jpeg|png|txt|gif|css|js)$")
{
unset beresp.http.Set-Cookie;
set beresp.ttl = 3600s;
}
}
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.
unset beresp.http.Server;
unset beresp.http.X-Powered-By; } sub vcl_purge {
return(synth(200,"Purged"));
} sub vcl_deliver {
if (obj.hits>0)
{
set resp.http.X-Cache = "Hit via" + server.ip;
}
else
{
set resp.http.X-Cache = "Miss from" + server.ip;
}
unset resp.http.Via;
unset resp.http.X-Varnish;
unset resp.http.X-Cache;
# 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.
}
#测试:




varnish安装和配置的更多相关文章
- 高性能HTTP加速器Varnish安装与配置
导读 Varnish是一款高性能且开源的反向代理服务器和HTTP加速器,它采用了全新的软件体系结构,和现在的硬件体系配合紧密.下面就由我给大家简单说说他的安装与配置. 安装 安装pcre 如果没有安装 ...
- 高性能HTTP加速器Varnish安装与配置(包含常见错误)
Varnish是一款高性能的开源HTTP加速器.挪威最大的在线报纸Verdens Gang使用3台Varnish取代了原来的12台Squid,性能竟然比曾经更好.Varnish 的作者Poul-Hen ...
- Varnish安装使用(初学)
本人对varnish也是新手,这里记录一下安装步骤! 环境:centos6.6 varnish安装包下载:wget https://repo.varnish-cache.org/source/varn ...
- 01 . Varnish简介,原理,配置缓存
简介 Varnish是高性能开源的反向代理服务器和HTTP缓存服务器,其功能与Squid服务器相似,都可以用来做HTTP缓存.可以安装 varnish 在任何web前端,同时配置它缓存内容.与传统的 ...
- JDK安装与配置
JDK安装与配置 一.下载 JDK是ORACLE提供免费下载使用的,官网地址:https://www.oracle.com/index.html 一般选择Java SE版本即可,企业版的选择Java ...
- Node.js 教程 01 - 简介、安装及配置
系列目录: Node.js 教程 01 - 简介.安装及配置 Node.js 教程 02 - 经典的Hello World Node.js 教程 03 - 创建HTTP服务器 Node.js 教程 0 ...
- 烂泥:redis3.2.3安装与配置
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 前一段时间写过一篇codis集群的文章,写那篇文章主要是因为当时的项目不支持redis自 ...
- mysql源码包手动安装、配置以及测试(亲测可行)
笔记编者:小波/qq463431476博客首页:http://www.cnblogs.com/xiaobo-Linux/ 记下这篇mysql笔记,望日后有用! redhat6采用centos yum源 ...
- 环境搭建系列-系统安装之centos 6.5安装与配置
按照国际惯例,系列目录先奉上: 系列一:系统安装之centos 6.5安装与配置 系列二:准备工作之Java环境安装 系列三:数据为先之MySQL读写集群搭建 系列四:谈分布式之RabbitMQ集群搭 ...
随机推荐
- Action三种编写方式
1. 创建普通类不实现接口与继承类 2. 实现Action接口 3. 继承ActionSupport类(常用)
- 移动端web常见问题解决方案
meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 忽略将页面中的数字识别为电话号码 忽略Android平台中对邮箱地址的识别 当网站添加到主屏幕快速启动方式,可隐藏地址栏,仅针对i ...
- PHP eval函数使用介绍
eval()函数中的eval是evaluate的简称,这个函数的作用就是把一段字符串当作PHP语句来执行. 复制代码代码如下: eval("echo'hello world';") ...
- 改善程序与设计的55个具体做法 day5
条款12:复制对象时勿忘其每一个成分 这里的复制是拷贝构造和operator= 每一个成分有几个维度: 1.每个成员变量 这个很好理解,添加新的成员时也要记得为每个新添加的成员执行合适的复制操作 2. ...
- iOS 展示 gif
gif 图 是多张依次有连续动作的图 顺时间展示的一种动态效果图 . 有的是均匀时间更换下一张 有的 则不是均匀时间变化 1. 那么 对于均匀 时间变化的gif图 比较适合 使用 iOS 系统自 ...
- python 3 并发编程多进程 paramiko 模块
python 3 paramiko模块 paramiko是一个用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作,值得一说的是,fabric和ansible内部的远程管理就是使用的pa ...
- 前端绘图方式Canvas和SVG的区别
Canvas和SVG是html5中支持2种可视化技术,都是可以在画布上绘制图形和放入图片.下面来介绍和分析一下他们. 一.Canvas 和 SVG 简介 1.什么是Canvas? Canvas 是H5 ...
- web框架详解之tornado 一 模板语言以及框架本质
一.概要 Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本.这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过 ...
- Android Studio Mac版快捷键
mac上按键符号 ⌥ : option / alt ⇧ : shift ⌃ : control ⌘ : command ⎋ : esc (一)查找/查看相关 搜索任意内容 双击 sft 当前文件查找/ ...
- HashOperations
存储格式:Key=>(Map<HK,HV>) 1.put(H key, HK hashKey, HV value) putAll(H key, java.util.Map<? ...