Varnish缓存代理

Varnish缓存代理

一、基本环境

1. IP地址配置

主机 IP地址 系统 软件版本
varnish-server 192.168.119.191/24 CentOS 7.8 varnishi 6.4.0
web01 192.168.119.192/24 CentOS 7.8 nginx
web02 192.168.119.193/24 CentOS 7.8 nginx

2. 主机名设置

3. 关闭Selinux

二、安装varnish及后端服务

1. 配置yum源

2. 安装

3. 配置varnish

# 备份$ cp /etc/varnish/default.vcl /etc/varnish/default.vcl.bak# 修改配置文件$ vim /etc/varnish/default.vcl##使用varnish版本4的格式vcl 4.1;##加载后端负载均衡模块import directors;##加载 std 模块import std;##创建名为 backend_healthcheck 的健康检查策略probe backend_healthcheck {    .url = "/";    .interval = 5s;    .timeout = 1s;    .window = 5;    .threshold = 3;}## Default backend definition. Set this to point to your content server.## 定义后端服务器backend web_app_01 {    .host = "192.168.119.192";    .port = "80";    .first_byte_timeout = 9s;    .connect_timeout = 3s;    .between_bytes_timeout = 1s;    .probe = backend_healthcheck;}backend web_app_02 {    .host = "192.168.119.193";    .port = "80";    .first_byte_timeout = 9s;    .connect_timeout = 3s;    .between_bytes_timeout = 1s;    .probe = backend_healthcheck;}## vcl_init 初始化子程序创建后端主机组sub vcl_init {    new web = directors.round_robin();    web.add_backend(web_app_01);    web.add_backend(web_app_02);}## 定义允许清理缓存的IPacl purgers {    "127.0.0.1";    "localhost";    "192.168.119.0/24";}##请求入口,用于接收和处理请求。这里一般用作路由处理,##判断是否读取缓存和指定该请求使用哪个后端sub vcl_recv {    ##将请求指定使用 web 后端集群 .在集群名后加上 .backend()    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);    }    ##如果不是 GET 和 HEAD 就跳到 pass    if (req.method != "GET" && req.method != "HEAD") {return (pass);    }    ##如果匹配动态内容访问请求就跳到 pass    if (req.url ~ ".(php|asp|aspx|jsp|do|ashx|shtml)($|") {return (pass);    }    ##具有身份验证的请求跳到 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|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)($|"") {unset req.http.cookie;return (hash);    }    ##把真实客户端 IP 传递给后端服务器 后端服务器日志使用 X-Forwarded-For 来接收    if (req.restarts == 0) {if (req.http.X-Forwarded-For) {    set req.http.X-Forwarded-For = req.http.X-Forwarded-For   ""

                                                        

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年3月19日
下一篇 2021年3月20日

相关推荐