CentOS 7.0 配置varnish缓存

简介:

1、安装varnish

1
2
3
4
rpm -ivh http: //mirrors .opencas.cn /epel/epel-release-latest-7 .noarch.rpm
yum  install  -y varnish
systemctl start varnish
systemctl  enable  varnish

2、设置监听端口

1
2
vim  /etc/varnish/varnish .params
     VARNISH_LISTEN_PORT=6081

3、设置后端服务器

1
2
3
4
5
vim  /etc/varnish/default .vcl
backend default {
     .host =  "172.16.1.21" ;
     .port =  "80" ;
}

4、配置VCL文件

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
#
# 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 https://www.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;
# Default backend definition. Set this to point to your content server.
backend default {
     .host =  "127.0.0.1" ;
     .port =  "80" ;
}
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.url ~  "\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)" ) {
         return  (pass);
     }
      
     if  (req.url ~  "\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)" ) {
         unset  req.http.cookie;
         return  ( hash );
     }
      
     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.http.Cache-Control ~  "(?i)no-cache" ) {
         if  (!(req.http.Via || req.http.User-Agent ~  "(?i)bot"  || req.http.X-Purge)) {
             return  (purge);
         }
     }
      
     if  (req.method !=  "GET"  && 
         req.method !=  "HEAD"  && 
         req.method !=  "PUT"  && 
         req.method !=  "POST"  && 
         req.method !=  "TRACE"  && 
         req.method !=  "OPTIONS"  && 
         req.method !=  "PATCH"  && 
         req.method !=  "DELETE" ) {        
         return  (pipe);
     }
      
     if  (req.method !=  "GET"  && req.method !=  "HEAD" ) {
         return  (pass);
     }
      
     if  (req.http.Authorization) {
         return  (pass);
     }
      
     if  (req.http.Accept-Encoding) {
         if  (req.url ~  "\.(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$" ) {
             unset  req.http.Accept-Encoding;        
         } elseif (req.http.Accept-Encoding ~  "gzip" ) {
             set  req.http.Accept-Encoding =  "gzip" ;
         } elseif (req.http.Accept-Encoding ~  "deflate" ) {
             set  req.http.Accept-Encoding =  "deflate" ;
         else  {
             unset  req.http.Accept-Encoding;
         }
     }
     if  (req.http.Upgrade ~  "(?i)websocket" ) {
         return  (pipe);
     }
      
      
     if  (req.http.x-pipe && req.restarts > 0) {
         unset  req.http.x-pipe;
         return  (pipe);
     }
      
     return  ( hash );
}
sub vcl_pipe {
     if  (req.http.upgrade) {
         set  bereq.http.upgrade = req.http.upgrade;
     }
      
     return  (pipe);
}
  
sub vcl_pass {
     if  (req.method ==  "PURGE" ) {
         return  (synth(502,  "PURGE on a passed object." ));
     }
}
  
sub vcl_hash {
     hash_data(req.url);
      
     if  (req.http.host) {
         hash_data(req.http.host);
     else  {
         hash_data(server.ip);
     }
      
     if  (req.http.Cookie) {
         hash_data(req.http.Cookie);
     }
      
     if  (req.http.Accept-Encoding ~  "gzip" ) {
         hash_data( "gzip" );
     } elseif (req.http.Accept-Encoding ~  "deflate" ) {
         hash_data( "deflate" );
     }
}
  
sub vcl_hit {
     if  (req.method ==  "PURGE" ) {
         return  (synth(200,  "Purged." ));
     }
      
     if  (obj.ttl >= 0s) {
         return  (deliver);
     }
      
     return  (deliver);
}
sub vcl_miss {
     if  (req.method ==  "PURGE" ) {
         return  (synth(404,  "Purged." ));
     }
      
     return  (fetch);
}
sub vcl_backend_response {
     # 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.
}
sub vcl_purge {
     if  (req.method !=  "PURGE" ) {
         set  req.http.X-Purge =  "Yes" ;
         return  (restart);
     }
}
#设置检查是否命中X-Cache
sub vcl_deliver {
     if  (obj.hits > 0) {
         set  resp.http.X-Cache =  "HIT" ;
     else  {
         set  resp.http.X-Cache =  "MISS" ;
     }
      
     unset  resp.http.X-Powered-By;
     unset  resp.http.Server;
      
     unset  resp.http.Via;
     unset  resp.http.X-Varnish;
      
     unset  resp.http.Age;
}

5、使用chrome浏览器是否命中

wKioL1ba22zh_0LBAAAkJEa73To624.png



参考博文:http://sofar.blog.51cto.com/353572/1653683/









     本文转自1321385590 51CTO博客,原文链接:http://blog.51cto.com/linux10000/1747881,如需转载请自行联系原作者


相关文章
|
3月前
|
应用服务中间件 Linux 网络安全
centos7 下离线安装gcc g++ nginx,并配置nginx进行网络流转发
centos7 下离线安装gcc g++ nginx,并配置nginx进行网络流转发
111 0
|
2月前
|
缓存
详解CentOS8更换yum源后出现同步仓库缓存失败的问题
详解CentOS8更换yum源后出现同步仓库缓存失败的问题
107 0
|
2月前
|
缓存 负载均衡 应用服务中间件
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
71 1
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
|
1天前
|
运维 网络协议 Linux
【运维系列】Centos7安装并配置PXE服务
PXE是Intel开发的预启动执行环境,允许工作站通过网络从远程服务器启动操作系统。它依赖DHCP分配IP,DNS服务分配主机名,TFTP提供引导程序,HTTP/FTP/NFS提供安装源。要部署PXE服务器,需关闭selinux和防火墙,安装dhcpd、httpd、tftp、xinetd及相关服务,配置引导文件和Centos7安装源。最后,通过syslinux安装引导文件,并创建pxelinux.cfg/default配置文件来定义启动参数。
5 0
|
1天前
|
运维 网络协议 Linux
【运维系列】Centos7安装并配置postfix服务
安装CentOS7的Postfix和Dovecot,配置Postfix的`main.cf`文件,包括修改完全域名、允许所有IP、启用邮箱等。然后,配置Dovecot的多个配置文件以启用auth服务和调整相关设置。重启Postfix和Dovecot,设置开机自启,并关闭防火墙进行测试。最后,创建邮箱账户并在Windows邮箱客户端中添加账户设置。
9 0
|
1天前
|
Linux 网络安全
Centos6.5安装并配置NFS服务
该内容描述了在Linux系统中设置NFS服务的步骤。首先挂载yum源,然后安装NFS服务,并编辑配置文件。接着,重启rpcbind和NFS服务,可能需要重复此过程以解决初始可能出现的问题。此外,关闭防火墙策略,并再次重启服务。最终,根目录被共享,特定IP网段被允许访问。
7 0
|
15天前
|
网络协议
centos8 网卡 Nmcli(是network的简写 Nmcli)配置网络
centos8 网卡 Nmcli(是network的简写 Nmcli)配置网络
15 0
|
30天前
|
缓存 运维 编译器
LAMP+Varnish缓存详解(二)——单网站缓存
LAMP+Varnish缓存详解(二)——单网站缓存
9 0
|
30天前
|
存储 缓存 运维
LAMP+Varnish缓存详解(一)——Varnish简介
LAMP+Varnish缓存详解(一)——Varnish简介
18 0
|
1月前
|
运维 Linux 应用服务中间件
Centos7如何配置firewalld防火墙规则
Centos7如何配置firewalld防火墙规则
47 0