我如何告诉Varnish显示一个自定义的html错误页面而不是默认的“Guru Meditation”消息 ?
光油常见问题build议使用vcl_error(这是我如何做到这一点):
这是错误页面的默认VCL:
sub vcl_error { set obj.http.Content-Type = "text/html; charset=utf-8"; 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> <address><a href="http://www.varnish-cache.org/">Varnish</a></address> </body> </html> "}; return(deliver); }
如果你想要一个自定义的版本,只需重写你的configuration中的函数,并replacesynthetic语句中的标记。
如果你想对不同的错误代码有不同的标记,你也可以很容易地做到这一点:
sub vcl_error { set obj.http.Content-Type = "text/html; charset=utf-8"; if (obj.status == 404) { synthetic {" <!-- Markup for the 404 page goes here --> "}; } else if (obj.status == 500) { synthetic {" <!-- Markup for the 500 page goes here --> "}; } else { synthetic {" <!-- Markup for a generic error page goes here --> "}; } }
请注意,上面的答案是用于清漆3的。由于问题没有指定版本信息,因此它似乎是适当的时候包含版本4的答案,因为它已经改变了。
希望这样可以避免人们阅读上面的答案,并将vcl_error放入他们的V4 VCL 🙂
Builtin VCL for Varnish 4.0
sub vcl_synth { set resp.http.Content-Type = "text/html; charset=utf-8"; set resp.http.Retry-After = "5"; synthetic( {"<!DOCTYPE html> <html> <head> <title>"} + resp.status + " " + resp.reason + {"</title> </head> <body> <h1>Error "} + resp.status + " " + resp.reason + {"</h1> <p>"} + resp.reason + {"</p> <h3>Guru Meditation:</h3> <p>XID: "} + req.xid + {"</p> <hr> <p>Varnish cache server</p> </body> </html> "} ); return (deliver); }
还要注意的是,如果你想从你的VCL中抛出一个错误,你不再使用'error'函数,而是你会这样做:
return (synth(405));
另外,来自后端的413,417和503错误通过这个function被自动路由。