Default behavior of Caddy when listening on port 80

1. The problem I’m having:

Hi!

When the following configuration is set in Caddy Server:

:8080 {
  respond "Hello, world from 8080!"
}

:9090 {
  respond "Hello, world from 9090!"
}
{
   "apps":{
      "http":{
         "servers":{
            "srv0":{
               "listen":[
                  ":8080"
               ],
               "routes":[
                  {
                     "handle":[
                        {
                           "body":"Hello, world from 8080!",
                           "handler":"static_response"
                        }
                     ]
                  }
               ]
            },
            "srv1":{
               "listen":[
                  ":9090"
               ],
               "routes":[
                  {
                     "handle":[
                        {
                           "body":"Hello, world from 9090!",
                           "handler":"static_response"
                        }
                     ]
                  }
               ]
            }
         }
      }
   }
}

Making calls to those ports will produce these results:

~ ❯ curl http://localhost:8080
Hello, world from 8080!

~ ❯ curl http://localhost:9090
Hello, world from 9090!

The curious part is when I make a call to port 80, Caddy’s default HTTP port. The response is as below:

~ ❯ curl localhost:80
Hello, world from 8080!

Caddy responds with the same content as the one defined in the block corresponding to port :8080. About this behavior, I have several questions.

  • Why does Caddy behave this way?
  • Looking at the configuration in JSON, it can be seen that the configuration of port :8080 is the first one, the one corresponding to srv0. Does this mean that Caddy, by default, unless there is an explicit configuration for port :80 (HTTP port), is going to serve on port :80 the first configuration that is defined in the Caddyfile, that is, the srv0?
  • If I want to prevent this from happening and block those responses from port :80, how could I do it? This applies to more complete configurations as well. The case that someone tries to access Caddy through port :80 using the server’s IP comes to mind. Could that be blocked?

Thank you very much.

2. Error messages and/or full log output:

N/A

3. Caddy version:

v2.8.4 running in docker container

4. How I installed and ran Caddy:

Using docker with the following image:

  • caddy:latest

a. System environment:

  • OS: Ubuntu 24.04
  • Docker Version: `Docker version 27.3.1, build ce12230``

b. Command:

docker compose up

c. docker-compose.yml:

services:
  caddy:
    image: caddy:latest
    container_name: caddy
    ports:
      - 80:80
      - 8080:8080
      - 9090:9090
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile

d. Caddyfile:

:8080 {
  respond "Hello, world from 8080!"
}

:9090 {
  respond "Hello, world from 9090!"
}

5. Links to relevant resources:

N/A