Have "unhandled" requests not return a 200 with an empty body but be "handled" instead

1. Caddy version (caddy version):

v2.5.0 h1:eRHzZ4l3X6Ag3kUt8nj5IxATprhqKq/wToP7OHlXWA0=

Today, a frustrated coworker sent me this Caddyfile. Now the issue’s obvious to me, but she had spent half a day on it:

:80 {
    handle_path /api* {
        reverse_proxy /api* {$SERVER}:5000
    }
}

and why this would result in a 200 with an empty body

curl -vvv 'http://caddy_server/api/my/path' -H 'some-header: some-value'

My annotated response to her was:

# disable Automatic HTTPS
:80 {
    # handle_path does the same as handle, but it strips a prefix from the request before running its handlers
    handle /api* {
        # NOTE: This is a redundant. Consider removing the matcher as the handler does the work for you. Let's discuss after standup
        reverse_proxy /api* {$SERVER}:5000
    }
}

That said, I really wanted to write something like this for her:

# disable Automatic HTTPS
:80 {
    # handle_path does the same as handle, but it strips a prefix from the request before running its handlers
    handle /api* {
        # NOTE: This is a redundant. Consider removing the matcher as the handler does the work for you. Let's discuss after standup
        reverse_proxy /api* {$SERVER}:5000

        handle {
            # Fallback for any otherwise unhandled requests, maybe a 404 if you want
            respond "Nope!" 404
        }

        respond * 500 {
            body "no handlers were found"
            close
        }
    }
}

Now, this seems to be a freq. problem:

… and I can imagine why this could be hard to implement (handle /api* does “handle” the request afterall), but any ideas come to mind where we can explicitly encode in the config the behavior of paths not explicitly processed?

This is just a user error. We have no plans to change the behaviour of unhandled requests.

I worked on trying to add a debug log to show something when a request went unhandled, but it was very tricky to actually implement for various reasons Add debug log if a request was not explicitly matched/handled · Issue #3445 · caddyserver/caddy · GitHub. Nearly two years later though, I could probably figure something out having more experience with the codebase.

To be clear, a handle doesn’t necessarily “handle” a request, cause you could use a handle and do nothing within it, which would still cause an empty 200.

The middle handle for fallback is in the wrong place here, it should be outside of the handle /api*. Also, this config would cause all API requests to respond with a 404, because handle has a higher directive order than reverse_proxy.

2 Likes