How to return a custom response for non configured hostname:port (instead of empty 200 OK)?

1. Caddy version (caddy version):

2.3.0

2. How I run Caddy:

a. System environment:

baremetal
ubuntu 18.04
systemd

b. Command:

systemctl start caddy

c. Service/unit/compose file:

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddyfile or JSON config:

{
    debug
    local_certs
}

a.foobar.com:43001 {
  respond "a"
}

b.foobar.com:43002 {
  respond "b"
}

3. The problem I’m having:

I would like to have a custom reponse instead of empty 200 OK for curl like that :

curl https://a.foobar.com:43002/ -v -k
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to a.foobar.com (127.0.0.1) port 43002 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/cert.pem
  CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-ECDSA-AES256-GCM-SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: [NONE]
*  start date: Jun  1 12:10:40 2021 GMT
*  expire date: Jun  2 00:10:40 2021 GMT
*  issuer: CN=Caddy Local Authority - ECC Intermediate
*  SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7f82c800d600)
> GET / HTTP/2
> Host: a.foobar.com:43002
> User-Agent: curl/7.64.1
> Accept: */*
>
* Connection state changed (MAX_CONCURRENT_STREAMS == 250)!
< HTTP/2 200
< server: Caddy
< content-length: 0
< date: Tue, 01 Jun 2021 13:23:38 GMT
<
* Connection #0 to host a.foobar.com left intact
* Closing connection 0

4. Error messages and/or full log output:

None, even with debug flag on

5. What I already tried:

add this at the end of caddyfile

:43001 {
  respond 404
}

but got error:

loading http app module: provision http: getting tls app: loading tls app module: tls: invalid configuration: automation policy 1 is the second policy that acts as default/catch-all, but will never be used

6. Links to relevant resources:

Found similar issues, but workaround does’nt work in my case.

That version of Caddy is over 6 months old; please try upgrading and see if that resolves the issue.

1 Like

Hello,

Thanks again for replying so quickly :slight_smile:

I just updated to v2.4.1
Same behavior, and nothing in logs

by the way, here the json config

caddy adapt --config /etc/caddy/Caddyfile | jq
{
  "logging": {
    "logs": {
      "default": {
        "level": "DEBUG"
      }
    }
  },
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":43001"
          ],
          "routes": [
            {
              "match": [
                {
                  "host": [
                    "a.foobar.com"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "body": "a",
                          "handler": "static_response"
                        }
                      ]
                    }
                  ]
                }
              ],
              "terminal": true
            }
          ]
        },
        "srv1": {
          "listen": [
            ":43002"
          ],
          "routes": [
            {
              "match": [
                {
                  "host": [
                    "b.foobar.com"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "body": "b",
                          "handler": "static_response"
                        }
                      ]
                    }
                  ]
                }
              ],
              "terminal": true
            }
          ]
        }
      }
    },
    "tls": {
      "automation": {
        "policies": [
          {
            "issuers": [
              {
                "module": "internal"
              }
            ]
          }
        ]
      }
    }
  }
}

I’m unable to reproduce the error. It works as expected.

Yep, I validated that JSON config as provided and I’m not getting an error.

$ ./caddy validate --config caddy.json
2021/06/02 01:36:46.480 INFO    using provided configuration    {"config_file": "caddy.json", "config_adapter": ""}
2021/06/02 01:36:46.481 INFO    tls.cache.maintenance   started background certificate maintenance      {"cache": "0xc0003405b0"}
2021/06/02 01:36:46.503 INFO    http    enabling automatic HTTP->HTTPS redirects        {"server_name": "srv0"}
2021/06/02 01:36:46.503 INFO    http    enabling automatic HTTP->HTTPS redirects        {"server_name": "srv1"}
2021/06/02 01:36:46.504 INFO    tls.cache.maintenance   stopped background certificate maintenance      {"cache": "0xc0003405b0"}
Valid configuration

Are you sure you’re running Caddy v2.4.1?

1 Like

Hello,

Sorry, indeed the following config behave as expected with 2.4.1 (I checked too fast yesterday)

{
    debug
    local_certs
}

a.foobar.com:43001 {
  respond "a"
}

b.foobar.com:43002 {
  respond "b"
}

:43001 {
  respond 404
}

:43002 {
  respond 404
}

Thanks !!
Sorry again for my mistake

2 Likes

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