Regular expression hostname question

1. Caddy version (caddy version):


2. How I run Caddy:

Kubernetes or Docker

a. System environment:


b. Command:


c. Service/unit/compose file:


d. My complete Caddyfile or JSON config:


3. The problem I’m having:

I am trying to migrate from nginx and I need to use regular expressions in the hostname. Also, I am using a wildcard SSL certificate.
What is the recommended approach for this use case?

This is one example of the hostname regex

What I already tried:

I can write a middleware but would mean I will end up with one server definition like *.*.{$DOMAIN}.

6. Links to relevant resources:

Thanks in advance for any advice,

You can use the header_regexp matcher for this.

:443 {
	@hostnames header_regexp host Host ...
	handle @hostnames {
		respond "Hi {}"

	handle {
		# Fallback for otherwise unmatched hosts

I’m not sure how much of your regexp syntax is supported by Golang’s regexp engine, but you can play around on to find out.

Are you bringing your own wildcard certificate to Caddy, or do you expect to use Caddy to automate it? Because Caddy can only issue single-level wildcard certificates. You could use On-Demand TLS though, if there’s not an inordinate amount of possible combinations of domains you want to allow.

@francislavoie thank you for the quick response.
That was exactly the first approach I took. My next issue with that is that I need to extract details from some regexp group. That’s where it starts to get messy :slight_smile: .The middleware approach (Go) allows me to extract the fields and use an expression.

Are you bringing your own wildcard certificate to Caddy, or do you expect to use Caddy to automate

Yes, I am using a certificate generated with cert-manager (mounted as a volume). That works just fine.

For some context of what I am trying to do: I want to migrate from nginx in

This is almost working now but I feel this could be cleaner

Hmm, yeah I don’t think you can do much better. Seems like your custom plugin lets you do what you need.

You could change these to the single-line syntax to save a bit of room:

	@workspace_blobserve expression {http.handlers.gitpod_headers.isBlobServe} == true
	@workspace_id expression {http.handlers.gitpod_headers.isWorkspaceID} == true
	@workspace_port expression {http.handlers.gitpod_headers.isWorkspacePort} == true

And maybe this is just me but I tend to prefer to put the matchers right next to where they’re used, instead of all at the top. Reads more like code with this sort of structure:

condition1 = true
if condition1 {
	return ...

condition2 = false
if condition2 {
	return ...

return ...

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