CORS on Caddyserver version 1

⚠️ 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

Quick Start

To add CORS authorization to the header using Caddy 1, add the following line inside your Caddyfile:

cors
⚠️ Security Warning: Using Access-Control-Allow-Origin: * allows any website to access your resources. Always specify exact origins in production.

Configuration Examples

Development Only (Open CORS)

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...
}

Production (Secure Configuration)

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
}

Path-Specific CORS

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
}

Configuration Parameters

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

Choosing the Right Configuration

Use Simple cors When:

Use Full Configuration When:

Important Notes

Troubleshooting

CORS errors despite configuration:

Preflight failing:

Credentials not working:

Upgrading to Caddy v2

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.

Testing Your CORS Configuration

For comprehensive testing instructions including curl commands, browser DevTools usage, and troubleshooting common CORS errors, see the CORS Testing Guide.

Additional Resources

Who’s behind this

Monsur Hossain and Michael Hausenblas

Contribute

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.

Buy the book

Save 39% on CORS in Action with promotional code hossainco at manning.com/hossain