JSON version of Caddyfile error

1. The problem I’m having:

After using caddy adapt and trying to load the resulting JSON, I get an error. Command I’m running is:
caddy adapt sites/myexamplesite.com.Caddyfile --adapter caddyfile

This is a working Caddyfile. I want the JSON output so I can emulate it and generate it dynamically.

2. Error messages and/or full log output:

Error: adapting config using caddyfile: /mnt/c/Users/bowie/Documents/GitHub/caddyserver/sites/myexamplesite.com.json:37 - Error during parsing: Unexpected '{}' at end of line

the “zstd”: {} is line 37, context here:

"encodings": {
    "gzip": {},
    "zstd": {}
  },

I tried adding a comma to the end of the line "zstd": {}, and the error changes to:

Error: adapting config using caddyfile: /mnt/c/Users/bowie/Documents/GitHub/caddyserver/sites/myexamplesite.com.json:126 - Error during parsing: Unexpected EOF

3. Caddy version:

v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=

4. How I installed and ran Caddy:

I have tested this in two environments, one on Ubuntu on an AWS server using:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

as well as in WSL2 for Windows, using the same above method.

a. System environment:

WSL2 in Windows 11 and Ubuntu Ubuntu 22.04.2 LTS

b. Command:

caddy adapt sites/myexamplesite.com.Caddyfile --adapter caddyfile
caddy run

d. My complete Caddy config:

myexamplesite.com, www.myexamplesite.com {
        encode zstd gzip
        rewrite * /{http.request.host.labels.1}.{http.request.host.labels.0}{uri}
        reverse_proxy * http://mybucket.s3-website-us-west-1.amazonaws.com {
                header_up Host {upstream_hostport}
                @error status 500 404
                handle_response @error {
                        rewrite * /{http.request.host.labels.1}.{http.request.host.labels.0}/404.html
                        reverse_proxy * http://mybucket.s3-website-us-west-1.amazonaws.com {
                                @200 status 200
                                replace_status @200 404
                                header_up Host {upstream_hostport}
                        }
                }
        }
}

JSON is not Caddyfile, so you can’t adapt JSON using the Caddyfile adapter. What I mean is you can’t pass a JSON file to the Caddyfile adapter.

I think you mean to use caddy adapt --config sites/myexamplesite.com.Caddyfile --adapter caddyfile, i.e. you need --config in front of the filename.

Correct, I’m not trying to adapt the JSON file, I’m trying to adapt the Caddyfile so I can replace it with JSON.

Using the --config flag provides the same exact JSON output, which caddy then doesn’t like.

bowie@Bowie:/mnt/c/Users/bowie/Documents/GitHub/caddyserver$ caddy adapt  --config sites/myexamplesite.com.Caddyfile --adapter caddyfile
2023/06/28 16:31:56.006 INFO    using adjacent Caddyfile
{"logging":{"logs":{"default":{"writer":{"output":"stdout"},"encoder":{"format":"json"},"level":"DEBUG","include":["http.log.access","admin.api"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":443"],"routes":[{"match":[{"host":["www.myexamplesite.com"]}],"handle":[{"handler":"subroute","routes":[{"group":"group1","handle":[{"handler":"rewrite","uri":"/{http.request.host.labels.1}.{http.request.host.labels.0}{http.request.uri}"}]},{"handle":[{"encodings":{"gzip":{},"zstd":{}},"handler":"encode","prefer":["zstd","gzip"]},{"handle_response":[{"match":{"status_code":[500,404]},"routes":[{"group":"group0","handle":[{"handler":"rewrite","uri":"/{http.request.host.labels.1}.{http.request.host.labels.0}/404.html"}]},{"handle":[{"handle_response":[{"match":{"status_code":[200]},"status_code":404}],"handler":"reverse_proxy","headers":{"request":{"set":{"Host":["{http.reverse_proxy.upstream.hostport}"]}}},"upstreams":[{"dial":"site-1-testing-bucket.s3-website-us-west-1.amazonaws.com:80"}]}]}]}],"handler":"reverse_proxy","headers":{"request":{"set":{"Host":["{http.reverse_proxy.upstream.hostport}"]}}},"upstreams":[{"dial":"site-1-testing-bucket.s3-website-us-west-1.amazonaws.com:80"}]}]}]}],"terminal":true},{"match":[{"host":["myexamplesite.com"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"static_response","headers":{"Location":["https://www.myexamplesite.com"]},"status_code":302}]}]}],"terminal":true}]}}}}}

I then replace my caddyfile with the json and

bowie@Bowie:/mnt/c/Users/bowie/Documents/GitHub/caddyserver$ sudo caddy run
2023/06/28 16:33:06.516 INFO    using adjacent Caddyfile
Error: adapting config using caddyfile: /mnt/c/Users/bowie/Documents/GitHub/caddyserver/sites/myexamplesite.com.json:1 - Error during parsing: Site addresses cannot contain a comma ',': '{"logging":{"logs":{"default":{"writer":{"output":"stdout"},"encoder":{"format":"json"},"level":"DEBUG","include":["http.log.access","admin.api"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":443"],"routes":[{"match":[{"host":["www.myexamplesite.com"]}],"handle":[{"handler":"subroute","routes":[{"group":"group1","handle":[{"handler":"rewrite","uri":"/{http.request.host.labels.1}.{http.request.host.labels.0}{http.request.uri}"}]},{"handle":[{"encodings":{"gzip":{},"zstd":{}},"handler":"encode","prefer":["zstd","gzip"]},{"handle_response":[{"match":{"status_code":[500,404]},"routes":[{"group":"group0","handle":[{"handler":"rewrite","uri":"/{http.request.host.labels.1}.{http.request.host.labels.0}/404.html"}]},{"handle":[{"handle_response":[{"match":{"status_code":[200]},"status_code":404}],"handler":"reverse_proxy","headers":{"request":{"set":{"Host":["{http.reverse_proxy.upstream.hostport}"]}}},"upstreams":[{"dial":"site-1-testing-bucket.s3-website-us-west-1.amazonaws.com:80"}]}]}]}],"handler":"reverse_proxy","headers":{"request":{"set":{"Host":["{http.reverse_proxy.upstream.hostport}"]}}},"upstreams":[{"dial":"site-1-testing-bucket.s3-website-us-west-1.amazonaws.com:80"}]}]}]}],"terminal":true},{"match":[{"host":["myexamplesite.com"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"static_response","headers":{"Location":["https://www.myexamplesite.com"]},"status_code":302}]}]}],"terminal":true}]}}}}}' - put a space after the comma to separate site addresses

I then format the JSON and

bowie@Bowie:/mnt/c/Users/bowie/Documents/GitHub/caddyserver$ sudo caddy run
2023/06/28 16:34:12.333 INFO    using adjacent Caddyfile
Error: adapting config using caddyfile: /mnt/c/Users/bowie/Documents/GitHub/caddyserver/sites/myexamplesite.com.json:53 - Error during parsing: Unexpected '{}' at end of line

Does caddy think my json file is a Caddyfile?

If I load it with the API it works fine but can’t use it as a standalone file with caddy run I guess?

If there’s a file in the current directory that starts with Caddyfile, then Caddy will load it as a Caddyfile. (That’s what " using adjacent Caddyfile" means.)

caddy adapt sites/myexamplesite.com.Caddyfile is missing the --config flag. I think that’s your problem.

Sorry if I’m not being clear - using a caddyfile works fine.

I don’t want to use a Caddyfile, I want to replace it with a json file and use caddy run to load the json file.

I think my issue is I was trying to use a root “Caddyfile” to load a json file in my “sites” directory, which clearly blows the whole thing up.

I can use a json file or a Caddyfile, but not both together.

1 Like

You have to specify the config file you want to run with to caddy run with the --config flag. If the file has Caddyfile in the name then Caddy will assume it’s a Caddyfile even if the contents are JSON. So use a different filename, or use --adapter json to force it not to parse it as Caddyfile.

After getting the proper json from myexamplewebsite.com.Caddyfile I delete it and replace it with myexamplewebsite.com.json

So my site structure is

Caddyfile
sites (folder)
  myexamplewebsite.com.json

the contents of Caddyfile are
import sites/*

I think trying to get a Caddyfile to import a json file is whats causing the issue.

1 Like

Yeah, you cannot mix Caddyfile and JSON. The import directive is a Caddyfile-exclusive feature which is just fancy copy-paste of Caddyfile config. You need to either only use Caddyfile or only JSON.

3 Likes

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