开发者

User-friendly error pages from Varnish

开发者 https://www.devze.com 2023-03-05 05:33 出处:网络
We are using Varnish at the front of Plone. In the case Plone goes down or serves an internal error we\'d like to show a user-friendly static HTML page which some CSS styling + images. (\"The server i

We are using Varnish at the front of Plone. In the case Plone goes down or serves an internal error we'd like to show a user-friendly static HTML page which some CSS styling + images. ("The server is being updated page")

How to开发者_JS百科 configure Varnish to do this?


Another simple way to do it is using the std vmod that comes with varnish. This is my preferred way to do it because I like having the error messages outside the configuration in case you want to have multiple responses for different status codes.

import std;

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

    return (deliver);
}


You can customize the synthetic page being served on a vlc_error. The default.vcl configuration file already shows how to do this, by serving the famous "Guru Meditation" error page (ahh, those wonderful Amiga days).

An example customization:

    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);
}


There isn't much help for doing this with Varnish 4 at the moment.

Here is what I ended up with:

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

For more info see the upgrading to 4.0 docs.


If you rather prefer to deliver an error page from a static file, you can override vcl_error() with a bit of C-code:

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号