varnish 503 的配置(需改进)
来源:互联网 发布:淘宝自己刷销量 编辑:程序博客网 时间:2024/06/07 14:45
# 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.# #webserver1_13 backend webserver1 { .host = "192.168.0.13"; .port = "12113"; .probe = {.url = "/index.html"; .interval = 10s; .timeout = 10s;.window = 5; .threshold = 4; } .connect_timeout = 600s; .first_byte_timeout = 600s; .between_bytes_timeout = 600s; } #webserver2_14 backend webserver2 { .host = "192.168.0.14"; .port = "12113"; .probe = { .url = "/index.html"; .interval = 10s; .timeout = 10s; .window = 5; .threshold = 4; } .connect_timeout = 600s; .first_byte_timeout = 600s; .between_bytes_timeout = 600s; } #webserver_13_14 director webserver round-robin {{.backend = webserver1;#.weight = 5;}{.backend = webserver2;#.weight = 5;} } #allow clean cache control acl purge { "localhost"; "127.0.0.1"; "192.168.7.10"; }# # 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.http.host ~ "(.*)xxxx.xxx.com") { set req.http.host = "xxxx.xxx.com"; }# if ((req.http.host ~ "^xxxx.xxx.com") && (req.restarts == 0)) { set req.backend = webserver;# }# elseif (req.restarts == 1) {# set req.backend = webserver2;# } if (req.request == "PURGE") {if (!client.ip ~ purge) {error 405 "Not allowed."; }return (lookup); } #real IP 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; } } #gzip,need zlib-devel if (req.http.Accept-Encoding) {if (req.http.Accept-Encoding ~ "gzip") {set req.http.Accept-Encoding = "gzip";}elseif (req.http.Accept-Encoding ~ "deflate") {set req.http.Accept-Encoding = "deflate";}else {remove req.http.Accept-Encoding;} } 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); } if (req.request != "GET" && req.request != "HEAD") { /* We only deal with GET and HEAD by default */ return (pass); } if (req.http.Authorization || req.http.Cookie) { /* Not cacheable by default */ return (pass); } if (req.request == "GET" && req.url ~ "\.(jpg|png|gif|swf|jpeg|m3u8|mp4|flv|ts)$") {unset req.http.cookie; } 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); }# 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 { if (req.request == "PURGE") {set obj.ttl = 0s;error 200 "Purged."; } return (deliver); }# sub vcl_miss { if (req.request == "PURGE") {error 404 "Not in cache."; }set req.http.host = "miss"; return (fetch); }# sub vcl_fetch { #Define what state into the restart model if (beresp.status == 500 || beresp.status == 501 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) {set req.http.host = "status";set beresp.saintmode = 10s;return (restart); } #The definition does not contain the cache HTTP header request if (beresp.http.Pragma ~ "no-cache" || beresp.http.Cache-Control ~ "no-cache" || beresp.http.Cache-Control ~ "private") {return (hit_for_pass); } #Define different content cache time if (req.request == "GET" && req.url ~ "\.(css|js|html|htm)$") {set beresp.ttl = 7d; } if (req.request == "GET" && req.url ~ "\.(gif|jpg|jpeg|bmp|tiff|tif|ico|img|bmp|wmf)$") {set beresp.ttl = 10d; } if (!req.backend.healthy) {set req.grace = 1m; } else {set req.grace = 30s; } 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 { #set resp.http.x-hits = obj.hits;set resp.http.x-hits = req.restarts;set resp.http.x-hits = req.http.host; if (obj.hits > 0) {set resp.http.X-Cache = "HIT"; } else {set resp.http.X-Cache = "MISS"; } 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);# }