Varnish configuration

本文介绍Varnish缓存系统的配置方法,包括后端服务器设置、请求处理流程、缓存策略等核心内容,并提供了详细的VCL示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# This is a basic VCL configuration file for varnish.  See the vcl(7)
# man page for details on VCL syntax and semantics.



# Default backend definition.  Set this to point to your content
# server.

# backend default {
#     .host = "127.0.0.1";
#     .port = "8080";
# }


##设置后端服务器地址
backend default {
  .host = "127.0.0.1";
  .port = "80";
}


##用于接受和处理请求,当请求成功被调用后,Varnish通过判断请求的数据来决定如何处理请求。
# Below is a commented-out copy of the default VCL logic.  If you
# redefine any of these subroutines, the built-in logic will be
# appended to your code.
# sub vcl_recv {


      ##首次访问增加X-Forwarded-For头信息,方便后端程序
      ##获取客户端IP
#     if (req.restarts == 0) {
        ##如果设置过此header则要再次附加上,用,隔开,如果只有一层代理的话,就无需设置了
# 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" &&
#       req.request != "PUT" &&
#       req.request != "POST" &&
#       req.request != "TRACE" &&
#       req.request != "OPTIONS" &&
#       req.request != "DELETE") {
#         /* Non-RFC2616 or CONNECT which is weird. */
#         return (pipe);
#     }


      ##只处理GET和HEAD请求,如果你不想缓存POST的页面的话
#     if (req.request != "GET" && req.request != "HEAD") {
#         /* We only deal with GET and HEAD by default */
#         return (pass);
#     }
      ##HTTP认证的页面也不处理
#     if (req.http.Authorization || req.http.Cookie) {
#         /* Not cacheable by default */
#         return (pass);
#     }
#     return (lookup);
# }



##管道 官方说法时此模式下请求会原封不动的转交给后端直到连接关闭
# sub vcl_pipe {
#     # Note that only the first request to the backend will have
#     # X-Forwarded-For set.  If you use X-Forwarded-For and want to
#     # have it set for all requests, make sure to have:
#     # set bereq.http.connection = "close";
#     # here.  It is not set by default as it might break some broken web
#     # applications, like IIS with NTLM authentication.
#     return (pipe);
# }



##交给后端服务器
# sub vcl_pass {
#     return (pass);
# }



##缓存文件名的hash处理
# sub vcl_hash {
      ##以url为hash
#     hash_data(req.url);
      ##加上host
#     if (req.http.host) {
#         hash_data(req.http.host);
#     } else {
          ##没有则获取IP
#         hash_data(server.ip);
#     }
#     return (hash);
# }



##缓存命中后的处理
# sub vcl_hit {
#     return (deliver);
# }



##缓存为命中
# sub vcl_miss {
#     return (fetch);
# }



##在后端主机更新缓存并且获取内容后调用的方法,接着,通过判断获取的内容来决定是否将内容放入缓存,还是直接返回给客户端。
# sub vcl_fetch {
#     if (beresp.ttl <= 0s ||
#         beresp.http.Set-Cookie ||
#         beresp.http.Vary == "*") {
# /*
# * Mark as "Hit-For-Pass" for the next 2 minutes
# */
# set beresp.ttl = 120 s;
# return (hit_for_pass);
#     }
#     return (deliver);
# }



##发送给客户端
# sub vcl_deliver {
      ##去掉varnish中的一些头信息(如果你不想保留的话,建议保留age,方便查看)
      ##remove resp.http.X-Varnish;
      ##remove resp.http.Via
#     return (deliver);
# }



##发送错误页面的信息
# sub vcl_error {
      ##设置返回页面的类型及编码
#     set obj.http.Content-Type = "text/html; charset=utf-8";
#     set obj.http.Retry-After = "5";
      ##设置返回的具体内容,可以根据自己的需要修改错误信息
#     synthetic {"
# <?xml version="1.0" encoding="utf-8"?>
# <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
#  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
# <html>
#   <head>
#     <title>"} + obj.status + " " + obj.response + {"</title>
#   </head>
#   <body>
#     <h1>Error "} + obj.status + " " + obj.response + {"</h1>
#     <p>"} + obj.response + {"</p>
#     <h3>Guru Meditation:</h3>
#     <p>XID: "} + req.xid + {"</p>
#     <hr>
#     <p>Varnish cache server</p>
#   </body>
# </html>
# "};
#     return (deliver);
# }

# sub vcl_init {
# return (ok);
# }

# sub vcl_fini {
# return (ok);
# }




参考文档:http://www.ibm.com/developerworks/cn/opensource/os-cn-varnish-intro/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值