Nginx return directive in caddy -- return json

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/docs/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