Matrix url path matching for workers

1. The problem I’m having:

I’m trying to match matrix url path’s so that I can distribute matrix synapse workload across multiple different workers for better performance. I have been able to get a couple to work, but can’t get them all to work.

2. Error messages and/or full log output:

PASTE OVER THIS, BETWEEN THE ``` LINES.
Please use the preview pane to ensure it looks nice.

No error messages

3. Caddy version:

v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=

4. How I installed and ran Caddy:

a. System environment:

Debian 12
System package from official install guide2

b. Command:

sudo systemctl start caddy.service

c. Service/unit/compose file:

...

d. My complete Caddy config:

matrix.cronyakatsuki.xyz {
        # Federation
        reverse_proxy /_matrix/federation/v1/event/* localhost:8181
        reverse_proxy /_matrix/federation/v1/state/* localhost:8181
        reverse_proxy /_matrix/federation/v1/state_ids/* localhost:8181
        reverse_proxy /_matrix/federation/v1/backfill/* localhost:8181
        reverse_proxy /_matrix/federation/v1/get_missing_events/* localhost:8181
        reverse_proxy /_matrix/federation/v1/publicRooms.* localhost:8181
        reverse_proxy /_matrix/federation/v1/query/* localhost:8181
        reverse_proxy /_matrix/federation/v1/make_join/* localhost:8181
        reverse_proxy /_matrix/federation/v1/make_leave/* localhost:8181
        reverse_proxy /_matrix/federation/(v1|v2)/send_join/* localhost:8181
        reverse_proxy /_matrix/federation/(v1|v2)/send_leave/* localhost:8181
        reverse_proxy /_matrix/federation/(v1|v2)/invite/* localhost:8181
        reverse_proxy /_matrix/federation/v1/event_auth/* localhost:8181
        reverse_proxy /_matrix/federation/v1/timestamp_to_event/* localhost:8181
        reverse_proxy /_matrix/federation/v1/exchange_third_party_invite/* localhost:8181
        reverse_proxy /_matrix/federation/v1/user/devices/* localhost:8181
        reverse_proxy /_matrix/key/v2/query* localhost:8181
        reverse_proxy /_matrix/federation/v1/hierarchy/* localhost:8181

        # sync
        reverse_proxy /_matrix/client/(r0|v3)/sync* localhost:8282
        reverse_proxy /_matrix/client/(api/v1|r0|v3)/events* localhost:8282
        reverse_proxy /_matrix/client/(api/v1|r0|v3)/initialSync* localhost:8282
        reverse_proxy /_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync* localhost:8282

        # main
        reverse_proxy /_matrix/* localhost:8008
        reverse_proxy /_synapse/client/* localhost:8008
}

5. Links to relevant resources:

synapse worker url patterns


Simple url patterns that are just simple url/* work correctly and match, but some that are more complex that use things like (v1/v2) and /foo that will have parameters like /foo?jfdlkjl&jfkjldf

Caddy’s path matcher doesn’t support regexp. If you need regexp matching, use the path_regexp matcher instead. See Request matchers (Caddyfile) — Caddy Documentation

Also, it’s quite inefficient to have that many reverse_proxy. You should just have one per upstream, and use a single matcher that handles all the paths.

1 Like

Could you provide an example? I tried doing path_regexp but wasn’t able to understand it and failed miserably, same with trying to lower the amount of reverse_proxy so I just did this as a quick and dirty way before asking for help here.

You need to define a named matcher like this:

@sync path_regexp ^/_matrix/client/(r0|v3)/sync
reverse_proxy @sync localhost:8282

Up to you to craft the regexp to handle the various combinations you need.

Couldn’t you just do this though?

@federation path /_matrix/federation/* /_matrix/key/v2/query*
reverse_proxy @federation localhost:8181

@sync path /_matrix/client/*
reverse_proxy @sync localhost:8282

@main path /_matrix/* /_synapse/client/*
reverse_proxy @main localhost:8008
1 Like

Tnx for that, as for the second part no, because matrix has too many thjngs set under diff parts I need to be able to set for every url separatelly otherwise I can get endpoint that those workers don’t support there and break matrix, like media stuff fkr example, leaving me not able to upload stuff.