On 2/27/21 3:10 AM, Stuart Henderson wrote:
I might be missing something but doesn't this do the same?

         location not found match "/(.*)" {
                 request rewrite "/%1.html"
         }

Nope, you're not missing anything, I wasn't aware you could do this.
This *does* actually solve my problem, although it should be

    location not found match "/(.+)"

(with `.+` instead of `.*`) in order for it to work properly with
`index.html`. I guess my patch is pretty unnecessary :)

On 2/27/21 3:10 AM, Stuart Henderson wrote:
Custom error pages sound useful indeed.

My patch for that is actually quite small (including a slight tweak to
the default font). It hardcodes the error file path to `<server
root>/<error code>.html` (e.g. /var/www/htdocs/404.html) and if that
file exists, it uses that file's contents instead of a hardcoded error
string. It's not very configurable, but it gets the job done:

Index: server_http.c
===================================================================
RCS file: /cvs/src/usr.sbin/httpd/server_http.c,v
retrieving revision 1.143
diff -u -p -u -p -r1.143 server_http.c
--- server_http.c       5 Jan 2021 19:56:11 -0000       1.143
+++ server_http.c       27 Feb 2021 23:31:13 -0000
@@ -848,6 +848,8 @@ server_abort_http(struct client *clt, un
        char                     buf[IBUF_READ_SIZE];
        char                    *escapedmsg = NULL;
        int                      bodylen;
+       char                     err_path[PATH_MAX];
+       FILE                    *f;
if (code == 0) {
                server_close(clt, "dropped");
@@ -925,14 +927,25 @@ server_abort_http(struct client *clt, un
/* A CSS stylesheet allows minimal customization by the user */
        style = "body { background-color: white; color: black; font-family: "
-           "'Comic Sans MS', 'Chalkboard SE', 'Comic Neue', sans-serif; }\n"
+           "sans-serif; }\n"
            "hr { border: 0; border-bottom: 1px dashed; }\n"
            "@media (prefers-color-scheme: dark) {\n"
            "body { background-color: #1E1F21; color: #EEEFF1; }\n"
            "a { color: #BAD7FF; }\n}";
+ /* Check for error document HTML file */
+       bodylen = 0;
+       if (snprintf(err_path, sizeof(err_path), "%s/%d.html", srv_conf->root,
+           code) != -1 && (f = fopen(err_path, "r")) != NULL) {
+               fseek(f, 0, SEEK_END);
+               bodylen = ftell(f);
+               fseek(f, 0, SEEK_SET);
+               body = calloc(bodylen + 1, sizeof(char));
+               bodylen = body ? fread(body, sizeof(char), bodylen, f) : 0;
+       }
+
        /* Generate simple HTML error document */
-       if ((bodylen = asprintf(&body,
+       if (bodylen == 0 && (bodylen = asprintf(&body,
            "<!DOCTYPE html>\n"
            "<html>\n"
            "<head>\n"

Reply via email to