Nginx return directive in caddy -- return json

I am trying to migrate a nginx config file to caddy. How can I rewrite this?

...
    location /.well-known/matrix/client {
        add_header Access-Control-Allow-Origin '*';
        return 200 '{ "m.homeserver": { "base_url": "domain here" } }';
    }
...

P.S: Let me know if you are running your matrix homeserver with Caddy. Love to hear from you.

Very easy, please see the docs: Caddyfile Directives — Caddy Documentation

handle /.well-known/matrix/client {
    header  Access-Control-Allow-Origin *
    respond `{ "m.homeserver": { "base_url": "domain here" } }`
}
2 Likes

Since I run multiple matrix servers, if figured I put my two cents in here :smiley:

If you wanna get all fancy, you could do the following to group both the /.well-known/matrix/client and /.well-known/matrix/server:

## matrix client/server delegation
handle_path /.well-known/matrix/* {

    header Access-Control-Allow-Origin *

    ## `Content-Type: application/json` isn't required by the matrix spec
    ## but some browsers (firefox) and some other tooling might preview json 
    ## content prettier when they are made aware via Content-Type
    header Content-Type application/json

    respond /client `{ "m.homeserver": { "base_url": "https://client-endpoint.example.com" } }`

    respond /server `{ "m.server": "federation-endpoint.example.com:443" }`

    ## return http/404 if nothing matches 
    respond 404
}

The port in /server is, and I quote the synapse docs here:

Note, specifying a port is optional. If no port is specified, then it defaults to 8448.


After I wrote the post here, I was curious whether the synapse docs have a client/server delegation example for Caddy.
And they do! :party:
synapse/reverse_proxy.md at e78d4f61fc881851ab35e9a889239a61cf9805e5 · matrix-org/synapse · GitHub

They set the Content-Type header too, in addition to Access-Control-Allow-Methods and Access-Control-Allow-Headers, which aren’t necessary either, but feel free to add them if you like :slight_smile:

And they opted to use Caddyfile#snippets with import (Caddyfile directive) — Caddy Documentation which works too, if you prefer that.

4 Likes

Thank you @IndeedNotJames!
I forgot to check the synapse docs (I use dendrite). On that note, have you tried it? It seems it has been in beta forever. Not sure if I should use synapse instead. I was captivated with the idea of using less resources.

I only run/maintain synapse servers right now :woman_shrugging:

I also kinda love how synapse still has

Help!! Synapse is slow and eats all my RAM/CPU!

in its README :smiley:

But it is the only spec-complete implementation as of right now.

Both Dendrite (Matrix/Element people) and Conduit (famedly) are still lacking some Server2Server and Client2Server APIs.

dendrite/README.md at d983d17355584b7de086389e069a935f1a5510a1 · matrix-org/dendrite · GitHub
Issues · matrix-org/dendrite · GitHub
README.md · a2a7c6187228bbcbbefadaa703e5afe469b7cbfe · Famedly / Conduit · GitLab
Conduit 1.0 · Milestones · Famedly / Conduit · GitLab

Dendrite is becoming more and more complete, but it will still take a while before it is truly complete.

All the cool p2p demos are built on dendrite though (This Week in Matrix 2022-03-18 | Matrix.org) and (Introducing P2P Matrix | Matrix.org) and (Introducing the Pinecone overlay network | Matrix.org).

I can’t really speak for Conduit because I don’t follow its development as much.

Oh, and most experimental MSCs [Matrix Spec Changes] usually land in synapse first, if you care about that.


So I would argue, stick with synapse for your main matrix.org server for now and maybe run another implementation on a separate (sub-) domain :slight_smile:

1 Like

:heart: ! Thank you @IndeedNotJames.
Do you hang out in any public matrix room? I’d love to join.

-drd

1 Like

Glad I could help :blush:

I only really lurk in public matrix rooms, but if you are searching rooms via Element Web/Desktop (onboarding is a bit rough):

  • Click on the little compass icon next to your search bar/icon top-left
  • Click on the Matrix rooms (<server name>) dropdown if you aren’t seeing any rooms (or wanna discover another public server)
  • Either select an existing one or click Add a new server… and add another server like matrix.org, mozilla.org, libera.chat, gitter.im or smaller ones like ungleich.ch.
    Now you should be able to see at least some public rooms

Alternatively, you could join/discover Spaces like matrix:#community:matrix.org. If you want to learn more about Spaces the following blog post by Element might be a good summary:

I understand we have public rooms in matrix. I wanted to know if you hang out in any of those so I can join too.

Thank you!

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