Getting 421 instead of 404

I have a simple config like below

https://mysite.com/api {
    proxy / ...
}

it works fine when I type ‘https://mysite.com/api’. But when I try other urls like ‘https://mysite.com’ or ‘https://mysite.com/foobar’, I get 421 error like 421 Site mysite.com is not served on this interface instead of the usual 404 not found. Why is it so and how to make caddy send the usual 404 instead?

My guess is that caddy serves the subfolder and thus misdirects the request to root (please correct me if im wrong) and you could try http.status to set the response.

Hi @balki, welcome to the Caddy community.

When you configure a site with the address example.com/foo, that site only works on requests to example.com/foo (and its subfolders, such as example.com/foo/bar). That means Caddy doesn’t use this site configuration to serve requests for example.com or any subfolders other than /foo.

So, when you make requests to these non-/foo subfolders, Caddy responds as though you aren’t serving that site at all (status 421).

In order to handle requests for example.com or other subfolders, you need to tell Caddy how to handle them by adding new site blocks and configuration for them, e.g.:

example.com/foo {
  # This part serves /foo
  proxy / [...]
}

example.com/bar {
  # This part serves /bar
}

example.com {
  # This part serves any request other than /foo or /bar
}
1 Like

Thanks! @Whitestrake @conorlburns

Added a section to make it send 404. So my file looks like:

https://mysite.com/api {
    proxy / ...
}
https://mysite.com/ {
    status 404 /
}

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