Varnish搭建缓存代理提高网站访问速度

简介:

简介:

Varnish是一款高性能、开源的反向代理服务器和缓存服务器,官方说是squid的四倍,实际应用测试中虽然达不到四倍的性能,那也能达到1-2倍的效果。

VarnishSquid的对比:

Squid 也是一种开源的代理缓存软件,下面对比 Varnish 和 Squid 的不同点。

Varnish的稳定性很好。两者在完成相同负载的工作时,Squid服务器发生故障的几率要高于Varnish,因此Squid需要经常重启。Varnish访问速度更快。Varnish采用了 Visual Page Cache技术,所有缓存的数据都直接从内存读取,而Squid从硬盘读取缓存的数据,所以Varnish在访问速度方面会更快一些。Varnish可以支持更多的并发连接。因为VarnishTCP连接与释放比Squid快,所以在高并发连接情况下可以支持更多的TCP连接。Varnish可以通过管理端口来管理缓存,使用正则表达式就可以批量清除部分缓存,而Squid做不到这一点。

Varnish缺点:

Varnish在高并发状态下,CPUI/O和内存等资源的开销高于Squid。Varnish的进程一旦挂起、崩溃或者重启,缓存的数据都会从内存中释放出来。此时的所有请求都会被发送到后端应用服务器上,在高并发的情况下,就会给后端服务器造成很大压力。

一、安装varnish

1.下载:https://www.varnish-cache.org/releases/varnish-cache-3.0.3

1
2
3
4
5
yum  install  -y  make  automake pkgconfig libtool pcre-devel  #安装依赖包
tar  zxvf varnish-3.0.3. tar .gz
. /autogen .sh
. /configure  --prefix= /usr/local/varnish  -- enable -debugging-symbols -- enable -developer-warnings -- enable -dependency-tracking
make  &&  make  install

2.创建用户、组和日志文件

1
2
3
4
groupadd varnish
useradd  -g varnish -s  /sbin/nologin  varnish
touch  /usr/local/varnish/access .log  #创建访问日志文件
chown  -R varnish.varnish  /usr/local/varnish

二、修改配置varnish

前端varnish缓存是以轮询的方式分担到后端web服务器,以下vcl不对php进行缓存。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
vi  /usr/local/varnish/etc/varnish/default .vcl
  backend web1 {
  .host =  "192.168.0.203" #后端web1服务器ip
  .port =  "80" #通过80端口来访问后端web
  .connect_timeout = 1s;   #连接超时时间
  .probe = {
         .url =  "/" ;
         .interval = 5s;
         .timeout = 1 s;   #检测超时时间
         .window = 5;
         .threshold = 3;
     }
  }
  backend web2 {
  .host =  "192.168.0.204" ;
  .port =  "80" ;
  .connect_timeout = 1s;   #连接超时时间
  .probe = {
         .url =  "/" ;
         .interval = 5s;
         .timeout = 1 s;   #检测超时时间
         .window = 5;
         .threshold = 3;
     }
  }
director default round-robin {  #定义后端服务器组,默认轮询方式
  {
  .backend = web1;
  }
  {
  .backend = web2;
  }
}
  sub vcl_recv {
  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.request !=  "GET"  &&
  req.request !=  "HEAD"  &&
  req.request !=  "PUT"  &&
  req.request !=  "POST"  &&
  req.request !=  "TRACE"  &&
  req.request !=  "OPTIONS"  &&
  req.request !=  "DELETE" ) {
  return  (pipe);
  }
  if  (req.request !=  "GET"  && req.request !=  "HEAD" ) {
  return  (pass);
  }
  if  (req.http.Authorization || req.http.Cookie) {
  return  (pass);
  }
  return  (lookup);
  }
  sub vcl_pipe {
  return  (pipe);
  }
  sub vcl_pass {
  return  (pass);
  }
  sub vcl_hash {
  hash_data(req.url);
  if  (req.http.host) {
  hash_data(req.http.host);
  }
  else  {
  hash_data(server.ip);
  }
  return  ( hash );
  }
  sub vcl_hit {
  return  (deliver);
  }
  sub vcl_miss {
  return  (fetch);
  }
  sub vcl_fetch {
  if  (beresp.ttl <= 0s ||
  beresp.http.Set-Cookie ||
  beresp.http.Vary ==  "*" ) {
  set  beresp.ttl = 120 s;
  return  (hit_for_pass);
  }
  return  (deliver);
  }
  sub vcl_deliver {
  return  (deliver);
  }
  sub vcl_error {
  set  obj.http.Content-Type =  "text/html; charset=utf-8" ;
  set  obj.http.Retry-After =  "5" ;
  synthetic {"
  <?xml version= "1.0"  encoding= "utf-8" ?>
  <!DOCTYPE html PUBLIC  "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
  <html>
  < head >
  <title> "} + obj.status + "  " + obj.response + {" < /title >
  < /head >
  <body>
  <h1>Error  "} + obj.status + "  " + obj.response + {" < /h1 >
  <p> "} + obj.response + {" < /p >
  <h3>Guru Meditation:< /h3 >
  <p>XID:  "} + req.xid + {" < /p >
  <hr>
  <p>Varnish cache server< /p >
  < /body >
  < /html >
  "};
  return  (deliver);
  }
  sub vcl_init {
  return  (ok);
  }
  sub vcl_fini {
  return  (ok);
  }

语句解析:

Varnish处理请求的主要处理方法
1. vcl_recv
首先接收请求,判断是否要进一步处理,还是直接转发给后端(pass)等。 此过程中可以使用和请求相关的变量,例如客户端请求的urlipuser-agentcookie等,此过程中可以把不需缓存的地址,通过判断(相等、不相等、正则匹配等方法)转给后端,例如gif/png/jpg/css/js等静态文件;
2. vcl_fetch
当从后端服务器获取内容后会进入此阶段,除了可以使用客户端的请求变量,还可以使用从后端获取的信息(bersp),如后端返回的头信息,设置此信息的缓存时间TTL等;
3. vcl_miss
缓存未命中时中要做的处理
4. vcl_hit
缓存命中后做的处理
5. vcl_deliver
发送给客户端前的处理
6. vcl_pass
交给后端服务器
7. vcl_hash
设置缓存的键值key
Varnish处理流程
首次请求时过程如下:
recv->hash->miss->fetch->deliver
缓存后再次请求:
recv->hash->hit->deliverfetch的过程没了,这就是我们要做的,把要缓存的页面保存下来)
直接交给后端pass的情况:
recv->hash->pass->fetch->deliver(直接从后端获取数据后发送给客户端,此时Varnish相当于一个中转站,只负责转发)

三、测试

1、启动

1
/usr/local/varnish/sbin/varnishd  -f  /usr/local/varnish/etc/varnish/default .vcl -s malloc,512M -g varnish -u varnish -T 127.0.0.1:2000 -a 0.0.0.0:80

-f:选项用于指定Varnishd使用的配置文件的路径。

-s malloc,2G:–s选项用来确定Varnish使用的存储类型和存储容量,这里使用的是malloc类型(malloc是一个C函数,用于分配内存空间),2G 定义多少内存被malloced

-T:127.0.0.1:2000是Varnish基于文本方式的一个管理接口,启动后可以在不停止Varnish的情况下来管理Varnish。管理端口2000可以指定。因为不是任何人都可以访问Varnish管理端口,所以这里推荐只监听本机端口。

-a:0.0.0.0:80中-a选项表示Varnish监听所有IP发给80端口的HTTP请求。

2、设置varnish访问日志

1
2
3
4
5
6
7
8
/usr/local/ var nish/bin/ var nishncsa -n /usr/local/ var nish/ var / var nish/test.com -a -w /usr/local/ var nish/access.log & #将 var nish访问日志写入到access.log里
/usr/local/ var nish/ var / var nish/test.com :默认缓存目录
netstat -tuplan |grep  var nish #查看是否启动 2000 80 端口
tcp  0  0  0.0 . 0.0 : 80  0.0 . 0.0 :* LISTEN  28551 / var nishd
tcp  0  0  127.0 . 0.1 : 2000  0.0 . 0.0 :* LISTEN  28550 / var nishd
tcp  0  0  ::: 80  :::* LISTEN  28551 / var nishd
curl -I http: //ip #使用curl命令查看是否被缓存
killall - 9  var nishd #关闭 var nish

现在输入varnish服务器ip就可以访问到后端web服务器!

3、设置开机启动:

1
echo  '/usr/local/varnish/sbin/varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,512M -g varnish -u varnish -T 127.0.0.1:2000 -a 0.0.0.0:80'  >>  /etc/rc . local

4、添加多个后端服务器

如果添加多个后端网站服务器,在default.vcl里面添加不同网站的访问请求转发到对应的backend去。



本文转自 李振良OK 51CTO博客,原文链接:http://blog.51cto.com/lizhenliang/1300036,如需转载请自行联系原作者

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
2月前
|
缓存 运维 Linux
LAMP+Varnish缓存详解(三)——多网站缓存
LAMP+Varnish缓存详解(三)——多网站缓存
13 2
|
2月前
|
缓存 运维 编译器
LAMP+Varnish缓存详解(二)——单网站缓存
LAMP+Varnish缓存详解(二)——单网站缓存
10 0
|
2月前
|
存储 缓存 运维
LAMP+Varnish缓存详解(一)——Varnish简介
LAMP+Varnish缓存详解(一)——Varnish简介
20 0
|
6月前
|
缓存 Ubuntu Linux
百度搜索:蓝易云【Varnish开源HTTP反向代理缓存服务器、部署安装、测试】
通过按照上述步骤部署和配置Varnish,您可以将其作为反向代理缓存服务器来提高Web应用程序的性能和响应速度。记住,在实际部署中,您可能需要进一步调整Varnish的配置以满足您的具体需求。
48 2
|
6月前
|
缓存 Linux 开发者
百度搜索:蓝易云【Varnish开源HTTP反向代理缓存服务器、部署安装、测试。】
通过以上步骤,你可以成功部署和安装Varnish,并对Web应用程序进行测试。请根据具体需求进行适当的配置和调整,以确保Varnish能够按预期工作并提升Web应用程序的性能。
36 0
|
7月前
|
缓存 NoSQL 数据库
40分布式电商项目 - 网站首页(缓存广告数据)
40分布式电商项目 - 网站首页(缓存广告数据)
32 0
|
10月前
|
缓存 运维 网络协议
Linux巩固篇016-Linux Squid 部署代理缓存服务
纸上得来终觉浅,绝知此事要躬行
409 0
Linux巩固篇016-Linux Squid 部署代理缓存服务
|
5天前
|
存储 缓存 NoSQL
【Go语言专栏】Go语言中的Redis操作与缓存应用
【4月更文挑战第30天】本文探讨了在Go语言中使用Redis进行操作和缓存应用的方法。文章介绍了Redis作为高性能键值存储系统,用于提升应用性能。推荐使用`go-redis/redis`库,示例代码展示了连接、设置、获取和删除键值对的基本操作。文章还详细阐述了缓存应用的步骤及常见缓存策略,包括缓存穿透、缓存击穿和缓存雪崩的解决方案。利用Redis和合适策略可有效优化应用性能。
|
8天前
|
存储 缓存 NoSQL
Redis多级缓存指南:从前端到后端全方位优化!
本文探讨了现代互联网应用中,多级缓存的重要性,特别是Redis在缓存中间件的角色。多级缓存能提升数据访问速度、系统稳定性和可扩展性,减少数据库压力,并允许灵活的缓存策略。浏览器本地内存缓存和磁盘缓存分别优化了短期数据和静态资源的存储,而服务端本地内存缓存和网络内存缓存(如Redis)则提供了高速访问和分布式系统的解决方案。服务器本地磁盘缓存因I/O性能瓶颈和复杂管理而不推荐用于缓存,强调了内存和网络缓存的优越性。
27 1
|
3天前
|
缓存 NoSQL Java
优化Redis缓存:解决性能瓶颈和容量限制
优化Redis缓存:解决性能瓶颈和容量限制
12 0