什么是varnish缓存代理?如何安装和配置?
发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,varnish缓存代理varnish 缓存是 web 应用加速器,同时也作为 http 反向缓存代理。你可以安装 varnish 在任何http 的前端,同时配置它缓存内容。与传统的 squid 相比
千家信息网最后更新 2025年02月05日什么是varnish缓存代理?如何安装和配置?
varnish缓存代理
varnish 缓存是 web 应用加速器,同时也作为 http 反向缓存代理。你可以安装 varnish 在任何http 的前端,同时配置它缓存内容。与传统的 squid 相比,varnish 具有性能更高、速度更快、管理更加方便等诸多优点。
varnish 完整配置实例1、拓扑环境Varnish:192.168.31.250Web01:192.168.31.83Web02:192.168.31.141配置 web01、web02 做为后端服务器(过程略)确保 varnish 服务器能正常访问 web01、web02Varnish 缓存代理服务器配置:
安装 varnish
1、安装依赖关系的软件包(注:使用 centos 在线 yum 源)
[root@varnish ~]# yum -y install autoconf automake libedit-devel libtool ncurses-devel pcre-devel pkgconfig python-docutils python-sphinx
2、安装 varnish
Varnish 的官方网址为 http://varnish-cache.org,可以在这里下载最新版本的软件。
下载地址:https://www.varnish-cache.org/content/varnish-cache-403
注意:Varnish 网站有时会被墙。
Git 下载:git clone https://github.com/varnish/Varnish-Cache /var/tmp/
解压,进入解压目录编译安装:
[root@varnish ~]# tar zxf varnish-4.0.3.tar.gz[root@varnish ~]# cd varnish-4.0.3/[root@varnish varnish-4.0.3]# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
注:
./autogen.sh
如果从 Git 库下载的安装包时才需要运行,用于生成 configure 编译文件。
配置:
[root@varnish varnish-4.0.3]# ./configure
注:不指定安装路径,默认是安装在/usr/local 目录下
编译、安装
[root@varnish varnish-4.0.3]# make && make install
复制 vcl 文件(在编译安装目录下),如果安装目录里没有 default.vcl 文件。
复制到安装目录的/usr/local/var/varnish/目录下(当然并无必需要求在哪个目录,因为正式
启动时还得指定这个文件的目录)[root@varnish varnish-4.0.3]# cp etc/example.vcl /usr/local/var/varnish/default.vcl
配置default.vcl
[root@varnish varnish-4.0.3]# vim /usr/loacl/var/varnish/default.vclvcl 4.0;import directors;import std;probe backend_healthcheck { .url="/"; .interval = 5s; .timeout = 1s; .window = 5; .threshold = 3;}backend web_app_01 { .host = "192.168.164.10"; #要改 .port = "80"; .first_byte_timeout = 9s; .connect_timeout = 3s; .between_bytes_timeout = 1s; .probe = backend_healthcheck;}backend web_app_02 { .host = "192.168.164.20"; #要改 .port = "80"; .first_byte_timeout = 9s; .connect_timeout = 3s; .between_bytes_timeout = 1s; .probe = backend_healthcheck;} acl purgers { "127.0.0.1"; "localhost"; "192.168.164.0/24"; #添加网段} sub vcl_init { new web = directors.round_robin(); web.add_backend(web_app_01); web.add_backend(web_app_02);} sub vcl_recv {set req.backend_hint = web.backend();if (req.method == "PURGE") { if (!client.ip ~ purgers) { return (synth(405, "Not Allowed."));}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.url ~ "\.(php|asp|aspx|jsp|do|ashx|shtml)($|\?)") {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.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;}}return (hash);}sub vcl_hash {hash_data(req.url);if (req.http.host) {hash_data(req.http.host);} else {hash_data(server.ip);}return (lookup);}sub vcl_hit { if (req.method == "PURGE") { return (synth(200, "Purged.")); } return (deliver);}sub vcl_miss {if (req.method == "PURGE") {return (synth(404, "Purged."));}return (fetch);}sub vcl_deliver {if (obj.hits > 0) {set resp.http.X-Cache = "HIT";set resp.http.X-Cache-Hits = obj.hits;} else {set resp.http.X-Cache = "MISS";} unset resp.http.X-Powered-By;unset resp.http.Server;unset resp.http.X-Drupal-Cache;unset resp.http.Via;unset resp.http.Link;unset resp.http.X-Varnish;set resp.http.xx_restarts_count = req.restarts;set resp.http.xx_Age = resp.http.Age;set resp.http.hit_count = obj.hits;unset resp.http.Age;return (deliver);}sub vcl_pass {return (fetch);} sub vcl_backend_response {set beresp.grace = 5m;if (beresp.status == 499 || beresp.status == 404 || beresp.status == 502) {set beresp.uncacheable = true;} if (bereq.url ~ "\.(php|jsp)(\?|$)") {set beresp.uncacheable = true;} else { if (bereq.url ~ "\.(css|js|html|htm|bmp|png|gif|jpg|jpeg|ico)($|\?)") {set beresp.ttl = 15m;unset beresp.http.Set-Cookie;} elseif (bereq.url ~ "\.(gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|\?)") {set beresp.ttl = 30m;unset beresp.http.Set-Cookie;} else {set beresp.ttl = 10m;unset beresp.http.Set-Cookie;}}return (deliver);}sub vcl_purge {return (synth(200,"success"));}sub vcl_backend_error {if (beresp.status == 500 ||beresp.status == 501 ||beresp.status == 502 ||beresp.status == 503 ||beresp.status == 504) {return (retry);}}sub vcl_fini {return (ok); }
**启动 varnish**[root@varnish ~]# /usr/local/sbin/varnishd -f /usr/local/var/varnish/default.vcl -smalloc,200M -a 0.0.0.0:80[root@varnish ~]# netstat -anpt | grep 80
目录
配置
缓存
文件
代理
服务器
服务
编译
同时
软件
下编
优点
传统
内容
前端
加速器
地址
官方
官方网址
实例
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
大话西游青衫磊落服务器
为什么学无线网络技术
软件开发方针如何写
饥荒云服务器网速不稳定
服务器的日常维护与管理
http应用服务器
镇江营销软件开发项目
手机软件开发系统设计
阿里网络安全公司
达梦数据库登录自建数据库
路由器远程代理服务器
郑州债无虑互联网科技
网络安全中的反恐
选择好的软件开发培训班
宣传普及网络安全知识
恒山系列鲲鹏服务器订购
电信公网ip 服务器
国内企业网络安全软件终端排名
软件开发店铺招牌照片
厦门维网网络技术有限公司
文献数据库中TI
网络技术科研人员
数据库迁移工具研发
远程访问阿里云服务器
搜云社工库30g数据库
服务器自动生产线厂家联系方式
手机网络安全法律法规图片
孤狼网络技术有限公司
无锡进口软件开发资费
厦门维网网络技术有限公司