自定义503错误页面上光油


Answers:


13

光油常见问题建议使用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);
}

如果您想要一个自定义版本,只需在配置中覆盖该函数并替换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 -->
        "};
    }
}

这在VCL 4.0中不起作用-如果您使用vcl 4.0,请参见下面的答案
Philipp

18

请注意,以上答案是针对Varnish 3的。由于该问题未指定版本信息,因此似乎也应该包括针对Version 4的答案,因为它已经更改。

希望这可以避免人们阅读上面的答案并将vcl_error放入他们的V4 VCL中:)

适用于Varnish 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);
}

还要注意,如果您想从VCL中引发错误,则不再使用'error'函数,而是这样做:

return (synth(405));

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


请注意,这不会捕获“后端提取错误”。要捕获它们,还必须创建一个sub vcl_backend_error,如在serverfault.com/a/665917/102757serverfault.com/a/716767/102757
lucaferrario
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.