⚠️ NOTE: You are viewing documentation for Caddy v1 (legacy version). Caddy v2 is the current version with different syntax. For Caddy v2, see: Caddy v2 CORS Configuration
To add CORS authorization to the header using Caddy 1, add the following line inside your Caddyfile:
cors
Access-Control-Allow-Origin: * allows any website to access your resources. Always specify exact origins in production.
Use this only for development and testing. DO NOT use in production.
# DEVELOPMENT ONLY - Allows all origins
# DO NOT use in production
example.com {
cors
# Your other directives...
}
For production environments, always specify allowed origins explicitly and enable credentials if needed:
# PRODUCTION - Secure configuration
example.com {
# Allow specific origins only
cors / {
origin https://example.com
origin https://app.example.com
methods GET,POST,PUT,DELETE,OPTIONS
allowed_headers Content-Type,Authorization,X-Requested-With
exposed_headers Content-Length,X-Custom-Header
allow_credentials true
max_age 86400
}
# Your other directives...
proxy / localhost:8080
}
Apply different CORS policies to different paths:
example.com {
# Public endpoints - no CORS restrictions
cors /public {
origin *
}
# Authenticated API - strict CORS
cors /api {
origin https://example.com
allow_credentials true
methods GET,POST,PUT,DELETE
allowed_headers Content-Type,Authorization
max_age 3600
}
# Your backend
proxy / localhost:8080
}
| Parameter | Description | Example |
|---|---|---|
origin |
Allowed origins (can specify multiple). Use specific domains for production. | https://example.com |
methods |
Allowed HTTP methods | GET,POST,PUT,DELETE |
allowed_headers |
Headers the client can send in requests | Content-Type,Authorization |
exposed_headers |
Headers the client can read from responses | Content-Length,X-Custom |
allow_credentials |
Allow cookies and authentication. Set to true for authenticated APIs. |
true or false |
max_age |
Preflight cache duration in seconds (86400 = 24 hours) | 86400 |
cors When:allow_credentials is true,
you MUST specify exact origins (cannot use wildcard *)
Vary: Origin header for proper cachinghttps://)
rather than HTTP (http://) for security
cors directive is in the correct blockmethodsallowed_headersallow_credentials true*) origin with credentialscredentials: 'include' in fetch requests
Caddy v2 offers better performance, improved configuration, and active security updates.
Note that v2 uses different syntax (the header directive instead of cors middleware).
See the Caddy v2 CORS Configuration page for current syntax,
or consult the Caddy v1 to v2 Migration Guide.
For comprehensive testing instructions including curl commands, browser DevTools usage, and troubleshooting common CORS errors, see the CORS Testing Guide.
The content on this site stays fresh thanks to help from users like you! If you have suggestions or would like to contribute, fork us on GitHub.
Save 39% on CORS in Action with promotional code hossainco at manning.com/hossain