Caddyfile to work with Matrix/Synapse

1. Output of caddy version:

latest: docker

2. How I run Caddy:

Docker

a. System environment:

Ubuntu

b. Command:

Paste command here.

c. Service/unit/compose file:

Docker

Paste full file contents here.
Make sure backticks stay on their own lines,
and the post looks nice in the preview pane. -->

d. My complete Caddy config:

{
    # Global options block. Entirely optional, https is on by default
    # Optional email key for lets encrypt
    email lookatme33@protonmail.com
    # Optional staging lets encrypt for testing. Comment out for production.
#     acme_ca https://acme-staging-v02.api.letsencrypt.org/directory
}
matrix.naff.casa {
  header /.well-known/matrix/server {
    Content-Type application/json
  }
  respond /.well-known/matrix/server 200 {
    body "{ \"m.server\": \"matrix.naff.casa:443\" }"
    close
  }
  reverse_proxy 192.168.1.37:8008
  tls ryan.naff@gmail.com {
    dns cloudflare {env.CLOUDFLARE_API_TOKEN}
  }
}
}
share.naff.casa {
    reverse_proxy 192.168.1.37:7070
}
logs.naff.casa {
    reverse_proxy 192.168.1.37:9999
}
bin.naff.casa {
    reverse_proxy 192.168.1.37:6608
}
paste.naff.casa {
    reverse_proxy 192.168.1.37:8085
}
remote.naff.casa {
    redir / /guacamole
    reverse_proxy 192.168.1.37:6969
}
fleet.naff.casa {
    reverse_proxy 192.168.1.37:82
}
books.naff.casa {
    reverse_proxy 192.168.1.37:5006
}
port.naff.casa {
    reverse_proxy 192.168.1.37:9000
}
www.naff.casa {
    redir https://naff.casa{uri}
}
naff.casa {
    reverse_proxy 192.168.1.37:2368
}
sonarr.naff.casa {
    reverse_proxy 192.168.1.37:8989
}
radarr.naff.casa {
    reverse_proxy 192.168.1.37:7878
}
ombi.naff.casa {

Paste your config here, replacing this text.
Use `caddy fmt` to make it readable.
DO NOT REDACT anything except credentials.
LEAVE DOMAIN NAMES INTACT.
Make sure the backticks stay on their own lines.

3. The problem I’m having:

Caddy Crashes, doesn’t work

4. Error messages and/or full log output:

{"level":"info","ts":1666839995.8900654,"logger":"http.log","msg":"server running","name":"remaining_auto_https_redirects","protocols":["h1","h2","h3"]}
{"level":"info","ts":1666839995.8900948,"logger":"http","msg":"enabling automatic TLS certificate management","domains":["cctv.naff.casa","jellyfin.naff.casa","naff.casa","prox.naff.casa","remote.naff.casa","logs.naff.casa","ombi.naff.casa","bw.naff.casa","paste.naff.casa","code.naff.casa","bin.naff.casa","share.naff.casa","sonarr.naff.casa","fleet.naff.casa","port.naff.casa","radarr.naff.casa","books.naff.casa","www.naff.casa"]}
{"level":"info","ts":1666839995.9066005,"logger":"tls","msg":"finished cleaning storage units"}
{"level":"info","ts":1666839996.3497655,"msg":"autosaved config (load with --resume flag)","file":"/config/caddy/autosave.json"}
{"level":"info","ts":1666839996.3498397,"msg":"serving initial configuration"}
{"level":"info","ts":1666840718.2262626,"msg":"shutting down apps, then terminating","signal":"SIGTERM"}
{"level":"warn","ts":1666840718.226418,"msg":"exiting; byeee!! 👋","signal":"SIGTERM"}
{"level":"info","ts":1666840729.3958611,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840730.5192223,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840731.6074882,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840732.8571436,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840734.4902375,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840736.9517984,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840741.0038111,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840748.2703555,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840755.9658458,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840757.0943587,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840758.2733645,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840759.5022318,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840761.1504977,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840763.5864177,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840767.6384609,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840774.8987179,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840788.5559437,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840814.9872162,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840867.0385978,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: parsing caddyfile tokens for 'tls': /etc/caddy/Caddyfile:18 - Error during parsing: getting module named 'dns.providers.cloudflare': module not registered: dns.providers.cloudflare
{"level":"info","ts":1666840908.1474652,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
{"level":"warn","ts":1666840908.1531916,"msg":"Caddyfile input is not formatted; run the 'caddy fmt' command to fix inconsistencies","adapter":"caddyfile","file":"/etc/caddy/Caddyfile","line":2}
{"level":"info","ts":1666840908.1559613,"logger":"admin","msg":"admin endpoint started","address":"localhost:2019","enforce_origin":false,"origins":["//localhost:2019","//[::1]:2019","//127.0.0.1:2019"]}
{"level":"info","ts":1666840908.1580637,"logger":"tls.cache.maintenance","msg":"started background certificate maintenance","cache":"0xc000971500"}
{"level":"info","ts":1666840908.15878,"logger":"http","msg":"server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS","server_name":"srv0","https_port":443}
{"level":"info","ts":1666840908.1588638,"logger":"http","msg":"enabling automatic HTTP->HTTPS redirects","server_name":"srv0"}
{"level":"info","ts":1666840908.1620364,"logger":"tls","msg":"cleaning storage unit","description":"FileStorage:/data/caddy"}
{"level":"info","ts":1666840908.1622198,"logger":"http","msg":"enabling HTTP/3 listener","addr":":443"}
{"level":"info","ts":1666840908.1624398,"msg":"failed to sufficiently increase receive buffer size (was: 208 kiB, wanted: 2048 kiB, got: 416 kiB). See https://github.com/lucas-clemente/quic-go/wiki/UDP-Receive-Buffer-Size for details."}
{"level":"info","ts":1666840908.162716,"logger":"http.log","msg":"server running","name":"srv0","protocols":["h1","h2","h3"]}
{"level":"info","ts":1666840908.1628478,"logger":"http.log","msg":"server running","name":"remaining_auto_https_redirects","protocols":["h1","h2","h3"]}
{"level":"info","ts":1666840908.1628766,"logger":"http","msg":"enabling automatic TLS certificate management","domains":["cctv.naff.casa","sonarr.naff.casa","ombi.naff.casa","www.naff.casa","radarr.naff.casa","remote.naff.casa","bin.naff.casa","bw.naff.casa","jellyfin.naff.casa","logs.naff.casa","paste.naff.casa","port.naff.casa","share.naff.casa","naff.casa","code.naff.casa","books.naff.casa","prox.naff.casa","fleet.naff.casa"]}
{"level":"info","ts":1666840908.177766,"logger":"tls","msg":"finished cleaning storage units"}
{"level":"info","ts":1666840908.182922,"msg":"autosaved config (load with --resume flag)","file":"/config/caddy/autosave.json"}
{"level":"info","ts":1666840908.1829963,"msg":"serving initial configuration"}

5. What I already tried:

I’ve tried 3-4 different Caddyfile configurations that I’ve found on various tutorial posts for Matrix/Caddy. None of them work.

6. Links to relevant resources:

Howdy @naffhouse!

You have this configuration:

But your logs are full of this error (emphasis mine):

You need to build Caddy with this non-standard module included in order to make use of it in your config.

Since you’re on the Docker image, see the documentation for that here: docs/caddy at master · docker-library/docs · GitHub

2 Likes

OK I am using a different Caddyfile now, but a different error:

(matrix-well-known-header) {
        header {
                Access-Control-Allow-Origin "*"
                Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
                Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization"
                Content-Type "application/json"
        }
}

(matrix) {
        handle /.well-known/matrix/server {
                import matrix-well-known-header
                respond `{"m.server":"matrix.naff.casa"}`
        }
        handle /.well-known/matrix/client {
                import matrix-well-known-header
                respond `{"m.homeserver":{"base_url":"https://element.naff.casa"}}`
        }
}

naff.casa {
        import matrix
}

matrix.naff.casa {
reverse_proxy https://192.168.1.37:8008
        @matrix {
                path /_matrix/* /_synapse/client/*
        }
        reverse_proxy @matrix https://192.168.1.37:8008 {
                header_up +Origin
                transport http {
                        keepalive 300s
                        keepalive_idle_conns 64
                        keepalive_idle_conns_per_host 32
                }
        }
        log {
                output file /var/log/caddy/matrix.naff.casa:.log {
                        roll_size 1M
                        roll_keep 30
                }
                format json
        }
}
}
share.naff.casa {
    reverse_proxy 192.168.1.37:7070
}
logs.naff.casa {
    reverse_proxy 192.168.1.37:9999
}

AND LOGS:

Error: adapting config using caddyfile: /etc/caddy/Caddyfile:18 - Error during parsing: File to import not found: matrix-well-known-header
{"level":"info","ts":1666884354.0810087,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: /etc/caddy/Caddyfile:18 - Error during parsing: File to import not found: matrix-well-known-header
{"level":"info","ts":1666884355.2131808,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: /etc/caddy/Caddyfile:18 - Error during parsing: File to import not found: matrix-well-known-header
{"level":"info","ts":1666884356.4188266,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: /etc/caddy/Caddyfile:18 - Error during parsing: File to import not found: matrix-well-known-header
{"level":"info","ts":1666884358.0512698,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: /etc/caddy/Caddyfile:18 - Error during parsing: File to import not found: matrix-well-known-header
{"level":"info","ts":1666884360.606237,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: /etc/caddy/Caddyfile:18 - Error during parsing: File to import not found: matrix-well-known-header
{"level":"info","ts":1666884364.6072495,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":"caddyfile"}
Error: adapting config using caddyfile: /etc/caddy/Caddyfile:18 - Error during parsing: File to import not found: matrix-well-known-header

This seems to work fine for a friend who is testing and running caddy:latest – I’m not sure where I’m going wrong?

The Caddyfile you shared has a } too much between

}
} # <-- remove that one
share.naff.casa {

You can let Caddy format your Caddyfile via caddy fmt (docs) and check it without running via caddy validate (docs).
However, please note that caddy validate might not catch all errors.

PS: You could also use a slightly different formatted approach, which won’t actually fix your issue, but I felt like mentioning it since you wrote

in your opening :innocent:

2 Likes

Hey James – thanks for helping me out so much over hte last year or so.

Not sure if you recognize my screen name but I recognize yours!

I’m not exactly sure what to do with your response? How do I implement this into my Caddyfile and fix it?

I did remove the 1 } you pointed out =)

2 Likes

Ye, I recognized you too :slight_smile:

So uhm the provided Caddyfile (after removing that one }) works for me locally.

Is the Caddyfile your shared the whole Caddyfile or did you truncate some parts?

2 Likes

my buddy’s is working too.

So I have the matrix part commented because I had to to get caddy reloaded:

{
    # Global options block. Entirely optional, https is on by default
    # Optional email key for lets encrypt
    email lookatme33@protonmail.com
    # Optional staging lets encrypt for testing. Comment out for production.
#     acme_ca https://acme-staging-v02.api.letsencrypt.org/directory
#(matrix-well-known-header) {
#        header {
#                Access-Control-Allow-Origin "*"
#                Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
#                Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept, Authorization"
#                Content-Type "application/json"
#        }
#}
#
#(matrix) {
#        handle /.well-known/matrix/server {
#                import matrix-well-known-header
#                respond `{"m.server":"matrix.naff.casa"}`
#        }
#        handle /.well-known/matrix/client {
#                import matrix-well-known-header
#                respond `{"m.homeserver":{"base_url":"https://element.naff.casa"}}`
#        }
#}
#
#naff.casa {
#        import matrix
#}
#
#matrix.naff.casa {
#reverse_proxy https://192.168.1.37:8008
#        @matrix {
#                path /_matrix/* /_synapse/client/*
#        }
#        reverse_proxy @matrix https://192.168.1.37:8008 {
#                header_up +Origin
#                transport http {
#                        keepalive 300s
#                        keepalive_idle_conns 64
#                        keepalive_idle_conns_per_host 32
#                }
#        }
#        log {
#                output file /var/log/caddy/matrix.naff.casa:.log {
#                        roll_size 1M
#                        roll_keep 30
#                }
#                format json
#        }
#}
}
share.naff.casa {
    reverse_proxy 192.168.1.37:7070
}
logs.naff.casa {
    reverse_proxy 192.168.1.37:9999
}
}
bin.naff.casa {
    reverse_proxy 192.168.1.37:6608
}
paste.naff.casa {
    reverse_proxy 192.168.1.37:8085
}
remote.naff.casa {
    redir / /guacamole
    reverse_proxy 192.168.1.37:6969
}
fleet.naff.casa {
    reverse_proxy 192.168.1.37:82
}
books.naff.casa {
    reverse_proxy 192.168.1.37:5006
}
port.naff.casa {
    reverse_proxy 192.168.1.37:9000
#}
#www.naff.casa {
#    redir https://naff.casa{uri}
}
#naff.casa {
#    reverse_proxy 192.168.1.37:2368
#}
sonarr.naff.casa {
    reverse_proxy 192.168.1.37:8989
}
radarr.naff.casa {
    reverse_proxy 192.168.1.37:7878
}
ombi.naff.casa {
    reverse_proxy 192.168.1.37:3579

}
bw.naff.casa {
    reverse_proxy 192.168.1.37:8711
}
cctv.naff.casa {
    reverse_proxy 192.168.1.224:8123
}
jellyfin.naff.casa {
    reverse_proxy 192.168.1.37:8096
}
code.naff.casa {
    reverse_proxy 192.168.1.37:4443
}
prox.naff.casa {
reverse_proxy 192.168.1.218:8006 {
transport http {
tls_insecure_skip_verify
}
}
}

I am afraid there is another misplaced }, this time:

}
} # <-- that one
bin.naff.casa {

Please use caddy fmt (docs) and caddy validate (docs) to check your Caddyfile.
You don’t need to restart or reload just to see if your Caddyfile is valid.
In this case, it isn’t valid.
Since you are using docker, you will have to use docker exec for that.

I can reproduce your error

Error: adapting config using caddyfile: Caddyfile:18 - Error during parsing: File to import not found: matrix-well-known-header

at least when I remove all the #.

Please note that snippets like matrix-well-known-header must not be within the global options.
You might know that already, it’s just that in the latest version of your Caddyfile that just isn’t the case (plus some vhosts like naff.case shouldn’t be in the global options either).

So again, please consider using caddy fmt --overwrite, so Caddy will properly format your Caddyfile, which makes it way easier to spot a misplaced curly bracket, because it would indent your Caddyfile all weird.
And then use caddy validate to check the config without doing a reload or whatever.
There are also VSCode plugins for Caddyfile language support, in case you are using VSCode.

If you want me to post a valid version of your Caddyfile, just let me know

1 Like

when i try to exec into caddy i get this error:

rnaff@linuxbox:/docker/caddy$ docker exec caddy caddy fmt -overwrite
Error response from daemon: Container adf3ac5be2607a157fe3401a3213bd12ec1e460508cb9f453cf93b7b53819d69 is restarting, wait until the container is running
rnaff@linuxbox:/docker/caddy$ docker exec caddy caddy fmt -overwrite

looks like we sorted it!!!

THANK YOU AGAIN FOR ALL OF YOUR HELP!

2 Likes

Hey @IndeedNotJames – so Matrix works fine BUT I am not able to share files/images.

The team for synapse admins suggested it could be my caddy configuration.

no one is ussing caddy there so they told me to come back and ask here.

Is there a max file size config for Caddy?

Caddy does not enforce a max upload size (sometimes also referred to as post body size, or in case of the caddy docs, “request body size”) by default.
See request_body (Caddyfile directive) — Caddy Documentation if you want to limit it.
In case of the synapse server, you would usually use max_upload_size in the homeserver.yaml instead.
Which I guess you’ve been already told?

Can you link to the discussion you had?
That might make it easier for me to catch up :slight_smile:

Hi @IndeedNotJames - nice to hear from you, thank you again for helping me.

I would link you the discussing but it’s a chat room on Matrix.

Looks like I need to change the max_file settings within matrix since Caddy doesn’t have a limit!

thanks again buddy, have a nice night.

You can use the 3 dots next to your message in Element Web/Desktop and click share.
That will create a https://matrix.to link including the rooms’ address and your event id (read: message id) to jump to :slight_smile:
Or you could just name the matrix room, assuming it’s public, I guess

This topic was automatically closed after 39 days. New replies are no longer allowed.