V2: how to add the CORS authorization via Caddyfile?

How do I enables CORS on Caddy v2?

1. My Caddy version (caddy -version):

VERSION 2.0.0-beta10

2. How I run Caddy:

Please provide all of the relevant information and DO NOT REDACT anything except passwords/keys. Thank you!

systemctl reload caddy

a. System environment:

DigitalOcean “Create Caddy Droplet”
Caddy
by Caddy
VERSION 2.0.0-beta10 OS Ubuntu 18.04

b. Command:

c. Service/unit/compose file:

d. My complete Caddyfile:

# The Caddyfile is an easy way to configure your Caddy web server.
#
# Unless the file starts with a global options block, the first
# uncommented line is always the address of your site.
#
# To use your own domain name (with automatic HTTPS), first make
# sure your domain's A/AAAA DNS records are properly pointed to
# this machine's public IP, then replace the line below with your
# domain name.
:80

# Set this path to your site's directory.
root * /usr/share/caddy

# Enable the static file server.
file_server

# CORS header https://enable-cors.org/server_caddy.html
#cors

# Another common task is to set up a reverse proxy:
# reverse_proxy localhost:8080

# Or serve a PHP site through php-fpm:
# php_fastcgi localhost:9000

# Refer to the Caddy docs for more information:
# https://github.com/caddyserver/caddy/wiki/v2:-Documentation

3. The problem I’m having:

I would like add the CORS authorization to the header using Caddy. I can not find the documentation that details the v2 replacement for adding

cors to the caddyfile

4. Error messages and/or full log output:

Please DO NOT REDACT any information except passwords/keys.

Job for caddy.service failed because the control process exited with error code.
See “systemctl status caddy.service” and “journalctl -xe” for details.

5. What I already tried:

6. Links to relevant resources:

https://caddyserver.com/docs/cors

#cors
headers / {
Access-Control-Allow-Origin *
-Server
}

:grinning:

2 Likes

Hi @sclarke001,

The http.cors plugin for Caddy v1 isn’t available for Caddy v2 at this time.

You can add headers manually using the header v2 Caddyfile directive. Docs are here:

header (Caddyfile directive) — Caddy Documentation

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.