Skip to content

Instantly share code, notes, and snippets.

@vanodevium
Created December 8, 2023 11:20
Show Gist options
  • Save vanodevium/563c7a3b1db7d5361f64388e62f9d08f to your computer and use it in GitHub Desktop.
Save vanodevium/563c7a3b1db7d5361f64388e62f9d08f to your computer and use it in GitHub Desktop.
Caddy server: enable CORS for any domain
(cors) {
@cors_preflight method OPTIONS
header {
Access-Control-Allow-Origin "{header.origin}"
Vary Origin
Access-Control-Expose-Headers "Authorization"
Access-Control-Allow-Credentials "true"
}
handle @cors_preflight {
header {
Access-Control-Allow-Methods "GET, POST, PUT, PATCH, DELETE"
Access-Control-Max-Age "3600"
}
respond "" 204
}
}
:80 {
import cors {header.origin}
}
@Elliot-Mernagh
Copy link

You legend! I've been trying to figure this out for ages, many thanks!

@jobinlawrance
Copy link

Thank you, this helped.

@Hussseinkizz
Copy link

do I nest this at my root inside my config file or per domain?

@vanodevium
Copy link
Author

Think of this example as a macro that you can use anywhere you work with routing.

@Hussseinkizz
Copy link

Think of this example as a macro that you can use anywhere you work with routing.

Got it to work but well it really took me nuts looking around for such, no even sufficient resources on the topic!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment