带有清漆的自定义 503 错误页面

Chr*_*vén 14 varnish

如何告诉 Varnish 显示自定义 html 错误页面而不是默认的“Guru Meditation”消息

小智 19

请注意,上述答案适用于 Varnish 3。由于该问题未指定版本信息,因此似乎是时候将版本 4 的答案也包括在内,因为它已更改。

希望这能让人们免于阅读上述答案并将 vcl_error 放入他们的 V4 VCL :)

用于清漆 4.0 的内置 VCL

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);
}
Run Code Online (Sandbox Code Playgroud)

还要注意,如果你想从你的 VCL 中抛出一个错误,你不再使用“错误”函数,而是你会这样做:

return (synth(405));
Run Code Online (Sandbox Code Playgroud)

此外,来自后端的 413、417 和 503 错误会通过此功能自动路由。

  • 请注意,这不会捕获“后端获取错误”。要捕获它们,您还必须创建一个`sub vcl_backend_error`,如您在http://serverfault.com/a/665917/102757和http://serverfault.com/a/716767/102757中看到的 (2认同)

Con*_*roe 14

光油常见问题建议使用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);
}
Run Code Online (Sandbox Code Playgroud)

如果您想要自定义版本,只需覆盖配置中的函数并替换synthetic语句中的标记即可。

如果你想对不同的错误代码有不同的标记,你也可以很容易地做到这一点:

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 -->
        "};
    }
}
Run Code Online (Sandbox Code Playgroud)