Handle_errors not working. What am I doing wrong?

1. The problem I’m having:

I am trying to get my error handling working. Currently, my easiest case is here: catch all errors and send to my 404.html.

In my caddyfile (I use :81 since :80 is taken), I have one route that successfully redirects. Anything else should error. I’ve copied the code directly from the handle_errors documentation.

My “error” is that I am NOT getting an error. I don’t get my 404.html and I don’t get status: 404.

curl -v http://localhost:81/bogus

Expected: Status: 404. Page: 404.html (rendered template)
Got: Status 200. No content.

2. Error messages and/or full log output:

 curl  -vL http://localhost:81/bogus
*   Trying 127.0.0.1:81...
* Connected to localhost (127.0.0.1) port 81 (#0)
> GET /bogus HTTP/1.1
> Host: localhost:81
> User-Agent: curl/7.88.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: Caddy
< Date: Thu, 18 Jan 2024 17:20:51 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact

3. Caddy version:

2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=

4. How I installed and ran Caddy:

sudo apt install caddy

a. System environment:

raspbian:
Linux pi5-seattle 6.1.0-rpi7-rpi-2712 #1 SMP PREEMPT Debian 1:6.1.63-1+rpt1 (2023-11-24) aarch64 GNU/Linux

b. Command:

caddy run --watch

c. Service/unit/compose file:

d. My complete Caddy config:

{
	admin :2020 # 2019 is in use
}

:81 {
	log {
		output stderr
		level DEBUG
		format console
	}

	handle /ha {
		redir http://pi-seattle:8123/dashboards-all/Overview 302
	}

	handle_errors {
		rewrite * /404.html
		templates
		file_server
	}
}

5. Links to relevant resources:

The reason is this request:

curl -v http://localhost:81/bogus

doesn’t invoke any of your configured HTTP routes, so the server replies with an empty/default HTTP response; and thus there is no error to handle.

@matt I’m clearly still misunderstanding something.

the request is to :81, so it should go to that large block.
it isn’t matched by anything. So isn’t that, by defiinition, a 404 (page not found)?

Or would it only fail if I had a route, tried to serve a local file, and the file wasn’t found?

How DO I do a catchall 404?

(And sorry if I’m obtuse.)

No, a 404 is not found. But it’s not looking for anything. Its route just has no handlers configured.

So yeah, this one.

I’m mobile right now but maybe someone else can get you an answer sooner. The error directive might help if you want to fabricate an error.

Got it! Thank you.

For others:


* {
    # First handle your known routes however you want to	
    handle /ha {
        rewrite *  <url_to_forward_to>
    }

    # Everything not already handled is a 404
    handle * {
        error "Not found" 404 
    }

    # Now handle the 404 (and any other errors)
    handle_errors {
		rewrite * /404.html
		templates
		file_server
	}
}

1 Like

This is wrong. * is not a valid site address.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.