User-friendly error pages from Varnish

前端 未结 4 1909
说谎
说谎 2021-02-04 07:49

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 +

相关标签:
4条回答
  • 2021-02-04 08:00

    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 讨论(0)
  • 2021-02-04 08:01

    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);
    }
    
    0 讨论(0)
  • 2021-02-04 08:02

    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);
    }
    
    0 讨论(0)
  • 2021-02-04 08:13

    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.

    0 讨论(0)
提交回复
热议问题