Varnish配置

原创
2014/05/19 11:47
阅读数 37

# This is a basic VCL configuration file for varnish.  See the vcl(7)
# man page for details on VCL syntax and semantics.
# /usr/local/etc/varnish/default.vcl
# Default backend definition.  Set this to point to your content
# server.
# demo : varnish配置
# author: baixunliu
# date  : 2014-04-23
# email : wmsjhappy@gmail.com
# func  : website为网址的缩写  
#backend default {
#    .host = "192.168.1.213";
#    .port = "80";
#}

backend portal100_website_com {
    .host = "192.168.1.213";
    .port = "80";
    .connect_timeout = 30s;
}

backend portal200_website_com {
    .host = "192.168.1.216";
    .port = "80";
    .connect_timeout = 30s;
}

backend manage100_website_com {
    .host = "192.168.1.211";
    .port = "80";
    .connect_timeout = 30s;
}

backend manage200_website_com {
    .host = "192.168.1.211";
    .port = "80";
    .connect_timeout = 30s;
}

backend agent100_website_com {
    .host = "192.168.1.212";
    .port = "80";
    .connect_timeout = 30s;
}

backend agent200_website_com {
    .host = "192.168.1.212";
    .port = "80";
    .connect_timeout = 30s;
}

backend img1_website_com {
    .host = "192.168.1.215";
    .port = "8080";
    .connect_timeout = 30s;
}

#clean cache buffer
acl purge {
"localhost";
"127.0.0.1";
}

#backend rac
director portal1_website_com round-robin {
{
.backend = portal100_website_com;
}{
.backend = portal200_website_com;
}
}

director manage_website_com round-robin {
{
.backend = manage100_website_com;
}{
.backend = manage200_website_com;
}
}

director agent_website_com round-robin {
{
.backend = agent100_website_com;
}{
.backend = agent200_website_com;
}
}

director img_website_com round-robin {
{
.backend = img1_website_com;
}
}


# 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 {
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.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return (lookup);
}

# check host portal.website.com or portal1.website.com for server
if ( req.http.host ~ "^portal0.website.com" || req.http.host ~ "^portal1.website.com" || req.http.host ~ "^portal2.website.com" || req.http.host ~ "^portal3.website.com" || req.http.host ~ "^portal100.website.com" || req.http.host ~ "^portal200.website.com") {
set req.backend = portal1_website_com;
if (req.url ~ "\.(gif$|jpg$|jpeg$|swf($|\?)|png$|js($|\?)|css($|\?)|ico$|xml$|html$|txt$|cur$)"){
return (lookup);
}else{
return (pass);
}
}




# check host manage.website.com or manage100.website.com for server
if ( req.http.host ~ "^manage.website.com" || req.http.host ~ "^manage100.website.com" || req.http.host ~ "^manage200.website.com" || req.http.host ~ "^portal3.website.com") {
set req.backend = manage_website_com;
if (req.url ~ "\.(gif$|jpg$|jpeg$|swf($|\?)|png$|js($|\?)|css($|\?)|ico$|xml$|html$|txt$|cur$)"){
return (lookup);
}else{
return (pass);
}
}

# check host agent.website.com or agent1.website.com for server
if ( req.http.host ~ "^agent.website.com" || req.http.host ~ "^agent1.website.com" || req.http.host ~ "^agent100.website.com" || req.http.host ~ "^agent200.website.com") {
set req.backend = agent_website_com;
if (req.url ~ "\.(gif$|jpg$|jpeg$|swf($|\?)|png$|js($|\?)|css($|\?)|ico$|xml$|html$|txt$|cur$)"){
return (lookup);
}else{
return (pass);
}
}

# check host img.website.com for server
if ( req.http.host ~ "^img.website.com" || req.http.host ~ "^img1.website.com" || req.http.host ~ "^img100.website.com" || req.http.host ~ "^img200.website.com") {
set req.backend = img1_website_com;
if (req.url ~ "\.(gif$|jpg$|jpeg$|swf($|\?)|png$|js($|\?)|css($|\?)|ico$|xml$|html$|txt$|cur$)"){
return (lookup);
}else{
return (pass);
}
}
}
# 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);
# }

# sub vcl_hash {
#     hash_data(req.url);
#     if (req.http.host) {
#         hash_data(req.http.host);
#     } else {
#         hash_data(server.ip);
#     }
#     return (hash);
# }

# sub vcl_hit {
#     return (deliver);
# }

sub vcl_miss {
if (req.http.Range) {
return (pass);
}
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_fetch {
if ( beresp.status == 404 || beresp.status == 500 || beresp.status == 503 ) {
return (hit_for_pass);
}
set beresp.ttl = 30d;
}

# sub vcl_deliver {
#     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);
# } 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部