Handle_errors unexpected behavior

Hi!

I stumbled on a surprising behavior of the handle_errors directive. I was not able to find anything in the documentation, the issues or the forum. I would be grateful if someone has some insight on what is happening.

1. Caddy version (caddy version):

v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=

2. How I run Caddy:

a. System environment:

I am on Debian Buster.

b. Command:

caddy run

d. My complete Caddyfile or JSON config:

http://localhost:2020/www* {
    root www
    uri strip_prefix /www
    file_server
    handle_errors {
        rewrite * /404.html
        file_server
    }
}

The www folder contains two files: index.html and 404.html

3. The problem I’m having:

The handle_errors directive is ignore. There is no error message or warning.

5. What I already tried:

It took me a day of trial and error to understand what was happening. I found that adding another site without a path and with the handle_errors directive fixes the problem.

http://localhost:2020/www* {
    root www
    uri strip_prefix /www
    file_server
    handle_errors {
        rewrite * /404.html
        file_server
    }
}

http://localhost:2020 {
    handle_errors {
        rewrite * /404.html
        file_server
    }
}
1 Like

I think you need to specify root inside of handle_errors as well.

But FYI, generally, I discourage the use of path matchers in the site address, because it can cause some confusion. Best to use a handle or route block instead.

Oh… I hadn’t considered the case where the handle_errors directive is used in a site block with a path matcher. Since error handling happens in a separate handler chain, we might need to make sure that path matcher is carried over into the error handler chain too…

Would you mind opening an issue on GitHub so we don’t forget? :smiley:

Thanks for your answers and your work on Caddy. I opened an issue at https://github.com/caddyserver/caddy/issues/3717

1 Like