设为首页 收藏本站
查看: 1832|回复: 0

[经验分享] 高性能缓存服务器Varnish架构配置

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-7-29 11:05:08 | 显示全部楼层 |阅读模式
    Varnish跟Squid都是一款内容加速缓存服务器,我们可以使用它们来对我们的网页内容进行缓存,以此来从某个方面提高用户体验度,提升网站整体的抗压能力。

    目前自建的CDN中,有很多都是基于Squid、Varnish等相关缓存软件,经过内部的二次开发实现了更好的内容加速及管理。
    那今天我们一起来学习一下Varnish简单的搭建及日常的维护,深入的东西后期分享,跟大家一起来交流。这里直接上Shell脚本自动初始化并安装:
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/bin/sh
#auto install varnish
#2014-07-28 by wugk
DIR=/usr/src
CODE=$?
VER=$1

if  [ -z $1 ];then
    echo "Usage:{$0 'Version' install |config |start|help } , Example $0 2.1.5 install}"
    exit
fi

URL=https://repo.varnish-cache.org/source/varnish-${VER}.tar.gz

function install()
{

if  [ ! -d /usr/local/varnish -o ! -f /etc/init.d/varnishd ];then
    cd $DIR ;wget -c $URL
    if  [ $CODE == '0' ];then
        echo "This varnish Files is Download Successed!"

    else
        echo "This varnish Files is Download Failed!"
    fi

    useradd  -s /sbin/noin varnish
    mkdir -p /data/varnish/{cache,}
    chown -R varnish.varnish /data/varnish/

    tar xzf varnish-${VER}.tar.gz ;cd varnish-${VER} ;/bin/sh autogen.sh  ;./configure --prefix=/usr/local/varnish --enable-dependency-tracking --enable-debugging-symbols --enable-developer-warnings -enable-extra-warnings &&make &&make install
else

    echo "This Varnish is exists,Please exit..."
    sleep 1
    exit 0
fi
}

function config()
{

    if  [ ! -d /usr/local/varnish/ -o -f /etc/init.d/varnishd ];then
     
        echo "You can't config varnish ,Please ensure you varnish dir is or not exist..."
        exit 0
    else
         
        echo "Varnish Already Success Install ,Please Config varnish ......"
    fi
    mv /usr/local/varnish/etc/varnish/default.vcl /usr/local/varnish/etc/varnish/default.vcl.bak
    cat >>/usr/local/varnish/etc/varnish/default.vcl <<EOF

backend server_1
{
.host ="192.168.149.128";
.port = "8080";
.probe = {
.timeout = 5s;
.interval = 2s;
.window = 8;
.threshold = 5;
}
}
backend server_2
{
.host ="192.168.149.129";
.port = "8080";
.probe = {
.timeout = 5s;   
.interval = 2s;   
.window = 8;     
.threshold = 5;
}
}
director rsver random {
{
.backend = server_1;
.weight = 6;
}
{
.backend = server_2;
.weight = 6;
}
}
acl purge {
"localhost";
"127.0.0.1";
}
sub vcl_recv
{
  if (req.http.host ~"^(.*).tdt.com")
  {     
     set req.backend =rsver;
  }  
     else
     {     
       error 200 "Nocahce for this domain";
     }           
       if (req.request =="PURGE")
         {        
           if (!client.ip ~purge)
             {           
                error 405"Not allowed.";        
             }
          else
             {
                return (pipe);
             }
}
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")
{        
return (pipe);
}
if (req.http.Expect)
{      
return (pipe);
}
if (req.http.Authenticate|| req.http.Cookie)
{        
return (pass);
}
if (req.http.Cache-Control~ "no-cache")
{      
return (pass);
}
if(req.url ~"\.jsp" || req.url ~ "\.php" )
{        
return (pass);
}
else
{
return (lookup);
}
}sub vcl_pipe
{
return (pipe);
}sub vcl_pass
{
return (pass);
}sub vcl_hash
{
set req.hash += req.url;
if (req.http.host)
{  
set req.hash +=req.http.host;
}
else
{
set req.hash +=server.ip;
}
  return (hash);
}sub vcl_hit
{
if (req.request =="PURGE")
{
set obj.ttl = 0s;      
error 200"Purged.";
}
if (!obj.cacheable)
{  
return (pass);
}
return (deliver);
}sub vcl_miss
{
if (req.request =="PURGE")
{  
error 404 "Not incache.";
}
if (req.http.user-agent ~"spider")
{   
error 503 "Notpresently in cache";
}
     return (fetch);
}
sub vcl_fetch
{
if (req.request =="GET" && req.url ~ "\.(txt|js)$")
{  
set beresp.ttl = 3600s;
}
else
{  
set beresp.ttl = 30d;
}
if (!beresp.cacheable)
{  
return (pass);
}
if (beresp.http.Set-Cookie)
{
return (pass);
}
return (deliver);
}
sub vcl_deliver {
if (obj.hits > 0) {
   set resp.http.X-Cache= "HIT FROM TDTWS Cache Center";
} else {
   set resp.http.X-Cache= "MISS FROM TDTWS Cache Center";
}
return (deliver);
}

EOF

if [ $? == 0 ];then
    echo '----------------------------------'
    sleep 2
    echo "This Varinsh Config Success !!!"
else
    echo "This Varinsh Config Failed,Please Check Conf!"
fi

}

function start()
{
if  [ ! -d /usr/local/varnish -o -f /etc/init.d/varnishd ];then
    echo "You can't config varnish ,Please ensure you varnish dir is or not exist..."
    exit 0

else
    count=`ps -ef |grep varnishd|grep -v grep |wc -l`
    if [ $count -eq 0 ];then
        echo "Varnish Already Success Install ,Now start varnish...."
        cat <<EOF
        ------------------------------------------------------
        Start Varnish to listen 0.0.0.0:80.
        The Varnish load balancer server1(192.168.149.128 8080).
        The Varnish load balancer server1(192.168.149.129 8080).
        The Varnish Mgr address to listen 0.0.0.0:8001.
        The Varnish Cache DIR /data/varnish/cache .
EOF
        /usr/local/varnish/sbin/varnishd -n /data/varnish/cache -f /usr/local/varnish/etc/varnish/default.vcl -a 0.0.0.0:80 -s file,/data/varnish/varnish_cache.data,16G  -p user=varnish -p group=varnish -p default_ttl=14400 -p thread_pool_max=8000 -p send_timeout=20 -w 5,51200,30 -T 0.0.0.0:8001  -P /usr/local/varnish/var/varnish.pid
        if [ $? == 0 ];then
            echo "Start varnish ...OK"
        fi
    else
        echo "Warning,This Varnish Service is exist."
    fi
fi
}

case $2 in

    install)
    install
    ;;
    config)
    config
    ;;

    start )
    start
    ;;

    *    )
    echo "Usage:{ Usage $0 version install |config |start|help }"
    ;;
esac



Varnish简单测试如下图:
1.第一张图,第一次访问没有命中,去后端服务器取数据,同时在本地缓存一份:(MISS)
wKioL1PWOnPwtbiAAAJcPaMseOQ005.jpg
2.第二张图,第二次访问,缓存服务器存在,则直接从缓存中返回:(HIT)
wKiom1PWOVmC4m2IAAI4pPzslf8299.jpg

    实际线上环境中,如果用户访问我们的网站,使用Ctrl+F5刷新,我们的缓存就会失效,因为我们配置文件里面是这么配置的,匹配浏览器头信息:
Pragma    no-cache
Cache-Control    no-cache
1
2
3
4
if (req.http.Cache-Control~ "no-cache")
{
return (pass);
}



    只有注释掉这段代码或者设置只允许某个特定的IP,用户通过浏览器按Crtl+F5才不会把缓存给清除。

    下面是针对某个特定的IP地址允许刷新:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
acl   local {
      "192.168.149.128"
}
sub vcl_hit {
      if (!obj.cacheable) {
         return (pass);
     }

     if (client.ip ~ local && req.http.Pragma ~ "no-cache") {
         set obj.ttl = 0s;
         return (pass);
     }
      return (deliver);
}



    由于时间的原因,文章就暂时写到这里,更多深入的东西继续分享,文章引用煮酒哥的配置,非常感谢。欢迎大家一起讨论。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-22839-1-1.html 上篇帖子: 高性能缓存加速器varnish(概念篇) 下篇帖子: Linux安装PHP加速器Xcache 服务器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表