来自Varnish的用户友好的错误页面

时间:2011-05-11 08:31:36

标签: plone varnish

我们在Plone前面使用Varnish。在Plone发生故障或内部错误的情况下,我们希望显示一个用户友好的静态HTML页面,其中包含一些CSS样式+图像。 (“服务器正在更新页面”)

如何配置Varnish来执行此操作?

4 个答案:

答案 0 :(得分:19)

另一种简单的方法是使用清漆附带的std vmod。这是我的首选方法,因为我希望在配置之外输入错误消息,以防您想要对不同的状态代码进行多次响应。

import std;

sub vcl_error {
    set obj.http.Content-Type = "text/html; charset=utf-8";
    synthetic std.fileread("/path/to/file.html");

    return (deliver);
}

答案 1 :(得分:15)

您可以自定义在vlc_error上提供的合成页面。 default.vcl配置文件已经显示了如何执行此操作,通过提供着名的“Guru Meditation”错误页面(啊,那些美妙的Amiga日)。

示例自定义:

    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>Sorry, server under maintainance - My Website"</title>
        <style src="css/style.css"></style>
      </head>
      <body>
        <h1>The server is being updated</h1>
        <p>Please check back later. Meanwhile, here's a picture of a rabbit with a pancake on its head:</p>
        <img src="img/wabbit.jpg" alt="awwwww!" />
      </body>
    </html>
    "};
    return (deliver);
}

答案 2 :(得分:4)

目前用Varnish 4做这件事并没有多大帮助。

以下是我最终的结果:

sub vcl_backend_error {
    set beresp.http.Content-Type = "text/html; charset=utf-8";
    synthetic(std.fileread("/var/www/errors/500.html"));
    return (deliver);
}

有关详细信息,请参阅upgrading to 4.0 docs

答案 3 :(得分:2)

如果您更喜欢从静态文件传递错误页面,可以使用一些C代码覆盖vcl_error():

sub vcl_error {
    set obj.http.Content-Type = "text/html; charset=utf-8";

    C{
        #include <stdio.h>
        #include <string.h>

        FILE * pFile;
        char content [100];
        char page [10240];
        char fname [50];

        page[0] = '\0';
        sprintf(fname, "/var/www/error/index.html", VRT_r_obj_status(sp));

        pFile = fopen(fname, "r");
        while (fgets(content, 100, pFile)) {
            strcat(page, content);
        }
        fclose(pFile);
        VRT_synth_page(sp, 0, page, "<!-- XID: ", VRT_r_req_xid(sp), " -->", vrt_magic_string_end);

        return (deliver);
    }C
}