Proper way to set routes to reverse proxys

1. Output of caddy version:

# caddy version
v2.6.2 h1:wKoFIxpmOJLGl3QXoo6PNbYvGW4xLEgo32GPBEjWL8o=

2. How I run Caddy:

Via the APT package.

a. System environment:

Debian 11, SystemD

b. Command:

None - systemd does that for me :slight_smile:

c. Service/unit/compose file:

n/a

d. My complete Caddy config:

ingwie.me {
        # Matrix:
        reverse_proxy /_matrix/* localhost:8008
        handle /.well-known/matrix/server {
                header Content-Type application/json
                header Access-Control-Allow-Origin *
                respond `{"m.server": "ingwie.me:443"}`
        }

        handle /.well-known/matrix/client {
                header Content-Type application/json
                header Access-Control-Allow-Origin *
                respond `{"m.homeserver": {"base_url": "https://ingwie.me:443"}}`
        }

        # Mastodon:
        #  root * /srv/mastodon/public
        #  encode gzip
        #  @static file
        #  handle @static {
        #    file_server
        #  }

        #  handle /api/v1/streaming* {
        #    reverse_proxy localhost:4000
        #  }

        #  handle * {
        #    reverse_proxy localhost:3000
        #  }

        #  header {
        #    Strict-Transport-Security "max-age=31536000;"
        #  }

        #  header /sw.js  Cache-Control "public, max-age=0";
        #  header /emoji* Cache-Control "public, max-age=31536000, immutable"
        #  header /packs* Cache-Control "public, max-age=31536000, immutable"
        #  header /system/accounts/avatars* Cache-Control "public, max-age=31536000, immutable"
        #  header /system/media_attachments/files* Cache-Control "public, max-age=31536000, immutable"

        #  handle_errors {
        #    @5xx expression `{http.error.status_code} >= 500 && {http.error.status_code} < 600`
        #    rewrite @5xx /500.html
        #    file_server
        #  }
}

3. The problem I’m having:

I want to host my Matrix server and Mastodon on the same domain. I fooled around with the configuration a little bit but between all the handle/proxy statements, my brain is now spaghetti ^^; …

The basics:

  • Anything that goes to /_matrix should go to my Matrix server.
  • Everything else should go to Mastodon.

But when I use the config as-is, my Matrix server no longer gets any messages, but Mastodon, does. So I am pretty sure I did something wrong with the order of the statements or something…

4. Error messages and/or full log output:

In-between me messing up the configuration sometimes and actual logs, a long time has passed and I couldn’t spot anything that would hint me what I needed to know. Sorry…

5. What I already tried:

I left my last attempt as commented-out part of the configuration.

6. Links to relevant resources:

None, really.

Wrap this with a handle block, like this:

handle /_matrix/* {
	reverse_proxy localhost:8008
}

Then use handle blocks for the other things you want to serve. A handle with no matcher will always run last, if none other matched. And handle can be nested if necessary, so you can put all the stuff you have commented out in one big handle to make sure it’s mutually exclusive from the matrix stuff.

2 Likes

Thanks! Gonna give it a try :slight_smile:

1 Like

Well I managed to get a Matrix/Element/Coturn server using Caddy and what’s working:

  • element
  • synapse
  • coturn
    I can provide my setting if it can help ? (not now because I don’t have them on hands…)

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