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,如需转载请自行联系原作者


相关文章
|
4月前
|
Linux 网络安全 Apache
CentOS 7.2配置Apache服务httpd(上)
CentOS 7.2配置Apache服务httpd(上)
370 1
|
23天前
|
Java
CentOS7.8配置Adoptium-Java17运行环境
本指南介绍如何设置清华镜像源并安装 Temurin-17-JRE 运行环境。首先,编辑 `/etc/yum.repos.d/adoptium.repo` 文件,配置清华镜像源。接着,使用 `yum install -y temurin-17-jre` 命令安装 Temurin-17-JRE,并通过 `java --version` 验证安装成功。相关配置和操作界面截图附后。
40 8
|
22天前
|
网络协议 Java 应用服务中间件
centos7环境下tomcat8的安装与配置
本文介绍了在Linux环境下安装和配置Tomcat 8的详细步骤。首先,通过无网络条件下的文件交互软件(如Xftp 6或MobaXterm)下载并解压Tomcat安装包至指定路径,启动Tomcat服务并测试访问。接着,修改Tomcat端口号以避免冲突,并部署Java Web应用项目至Tomcat服务器。最后,调整Linux防火墙规则,确保外部可以正常访问部署的应用。关键步骤包括关闭或配置防火墙、添加必要的端口规则,确保Tomcat服务稳定运行。
|
1月前
|
缓存 NoSQL Java
Mybatis学习:Mybatis缓存配置
MyBatis缓存配置包括一级缓存(事务级)、二级缓存(应用级)和三级缓存(如Redis,跨JVM)。一级缓存自动启用,二级缓存需在`mybatis-config.xml`中开启并配置映射文件或注解。集成Redis缓存时,需添加依赖、配置Redis参数并在映射文件中指定缓存类型。适用于查询为主的场景,减少增删改操作,适合单表操作且表间关联较少的业务。
|
3月前
|
存储 缓存 安全
在 Service Worker 中配置缓存策略
Service Worker 是一种可编程的网络代理,允许开发者控制网页如何加载资源。通过在 Service Worker 中配置缓存策略,可以优化应用性能,减少加载时间,提升用户体验。此策略涉及缓存的存储、更新和检索机制。
|
3月前
|
关系型数据库 MySQL Linux
在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置 MySQL 服务、登录设置等。
本文介绍了在 CentOS 7 中通过编译源码方式安装 MySQL 数据库的详细步骤,包括准备工作、下载源码、编译安装、配置 MySQL 服务、登录设置等。同时,文章还对比了编译源码安装与使用 RPM 包安装的优缺点,帮助读者根据需求选择最合适的方法。通过具体案例,展示了编译源码安装的灵活性和定制性。
285 2
|
3月前
|
存储 缓存 监控
配置 Webpack 5 持久化缓存时需要注意哪些安全问题?
【10月更文挑战第23天】通过全面、系统地分析和应对安全问题,能够更好地保障 Webpack 5 持久化缓存的安全,为项目的成功构建和运行提供坚实的安全基础。同时,要保持对安全技术的关注和学习,不断提升安全防范能力,以应对日益复杂的安全挑战。
|
4月前
|
存储 缓存 监控
|
4月前
|
Java jenkins 持续交付
Centos7下docker的jenkins下载并配置jdk与maven
通过上述步骤,您将成功在CentOS 7上的Docker容器中部署了Jenkins,并配置好了JDK与Maven,为持续集成和自动化构建打下了坚实基础。
187 1
|
4月前
|
存储 监控 Linux
在 CentOS 7 中如何对新硬盘进行分区、格式化、挂载及配置最佳实践
本文详细介绍了在 CentOS 7 中如何对新硬盘进行分区、格式化、挂载及配置最佳实践,包括使用 `fdisk` 创建分区、`mkfs` 格式化分区、创建挂载点、编辑 `/etc/fstab` 实现永久挂载等步骤,旨在有效管理服务器磁盘空间,提高系统稳定性和可维护性。
653 1