Implementing CORS whitelist in Caddy v2

With Caddy v2.1 I’d recommend this approach:

(cors) {
	@origin header Origin {args.0}
	header @origin Access-Control-Allow-Origin "{args.0}"
	header @origin Access-Control-Request-Method GET
}

origin1.com {
	import cors origin1.com
}

origin2.com {
	import cors origin2.com
}

This uses 2 new features:

  • single line named matcher syntax, which lets you omit the { } braces for the matcher if you only have one thing to match
  • import args (along with snippets which already existed), which let you use {args.*} as a placeholder where the numbers are the positional arguments given to the import directive.

The first beta for Caddy v2.1 was just released today: Release 2.1 beta 1 · caddyserver/caddy · GitHub

Frankly, I’m not sure what you’re referring to. That’s definitely invalid syntax. Could you clarify what you mean?

See the matcher docs here:

1 Like