Server specific handlers

1. Caddy version (caddy version):

2.3.0

2. How I run Caddy: Caddy binary

a. System environment:

macOS Big Sur (11.2.3)

b. Command:

caddy run --config Caddyfile --adapter caddyfile

c. Service/unit/compose file:

NONE

d. My complete Caddyfile or JSON config:

{
	servers :80 {

	}

	servers :10000 {

	}
}

# for port :10000
metrics

# other handlers for :80

3. The problem I’m having:

I’m trying to run Caddy on Kubernetes and I’d like to run metrics and health check endpoints on port 10000, but I don’t want to expose them to the world. I suspect I can achieve that by creating a JSON config, but I’d like to use the simplified Caddyfile format if possible. Sadly the documentation and the forum here doesn’t mention this use case at all.

4. Error messages and/or full log output:

NONE

5. What I already tried:

See the Caddyfile above

6. Links to relevant resources:

NONE

I think you probably misunderstood the purpose of the servers global options. It’s just for configuring certain settings for servers in JSON that the Caddyfile doesn’t otherwise make possible via site configuration (because Caddyfile doesn’t map 1:1 to JSON at all, it involves a bunch of magic to make configuration more user-friendly and easy to use).

I think all you need is this:

:10000 {
	metrics
}

# Your actual site:
example.com {
	root * /srv
	file_server
}

Please read the Caddyfile Concepts docs, it explains how the Caddyfile is structured:

1 Like

@francislavoie Thank you very much for your answer. It clarifies things a bit. Yeah, I realized the differences between JSON and the Caddyfile format and that it is much simpler which is why I would prefer using that format.

My only remaining question is: How does caddy know which server should the example.com be served from? Or is it going to be served from all servers? Is there a way to map hosts to servers? I can’t seem to find the answer for that in the concept documentation. As far as I understood, the comma-separated site addresses are in OR and not in AND relationship.

Thanks again!

1 Like

In the Caddyfile, you don’t need to think about the concept of “servers”. It only matters if you’re looking to do some more advanced configuration.

Caddy is HTTPS by default (i.e. port 443) unless you tell it otherwise (e.g. by specifying http:// to turn off HTTPS).

You can see what JSON a Caddyfile adapts to with the caddy adapt --pretty command.

It’s hard to really answer your question because it’s kinda vague. It’s easier to talk about specifics if you can describe what you actually want to do.

Yeah - AND doesn’t make sense because a request can’t be both to example1.com and example2.com at the same time, a request only has one hostname.

1 Like

Unfortunately, in the context of Kubernetes, server/listener/port is a key differentiator.

I’d like to separate handlers to 3 different types of “ports”

  • telemetry on port 10000 for metrics and health checks
  • admin site (different from caddy admin) on port 8080
  • regular sites on port 80

The admin “site” should be served on port 8080 (that’s how I can control access to it in Kubernetes).

As mentioned before, telemetry should run in port 10000.

And then there are other sites (virtual hosts) that I want to access on port 80.

For example: example.com and example2.com should be accessible on port 80. admin.example.com should be accessible on port 8080. Is this something that the Caddyfile format supports?

Yep, like this:

:10000 {
	metrics
}

http://example.com {
	...
}

http://example2.com {
	...
}

http://admin.example.com:8080 {
	...
}

My point was, think about “ports” in the Caddyfile, not “servers”.

Makes sense, thanks a lot for your help @francislavoie !

I have to say that it was a bit hard to get this kind of information from the documentation, maybe it’s just me, but I’m usually good with documentation. My 2 cents.

Anyway, thanks again for the help.

1 Like

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