Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 3e95c19

Browse files
tswfidklimpelolmari
authored
Docs: Add Nginx loadbalancing example with sticky mxid for workers (#15411)
* Docs: Add Nginx loadbalancing example with sticky mxid for workers Add example nginx configuration snippet that * does load balancing for workers * respects mxid part of the token * from both url parameter and auth header * and handles since parameter Thanks to @olmari for pushing me to write this and testing the configs Signed-off-by: Tatu Wikman <tatu.wikman@gmail.com> * Add changelog entry Signed-off-by: Tatu Wikman <tatu.wikman@gmail.com> * Update codeblock formatter Co-authored-by: Dirk Klimpel <5740567+dklimpel@users.noreply.github.com> * Remove indirectly related nginx-config Signed-off-by: Sami Olmari <sami@olmari.fi> * Proper definition of action how to target username for worker Signed-off-by: Sami Olmari <sami@olmari.fi> * Change "nginx" to general "reverse proxy" as it's concept now. Signed-off-by: Sami Olmari <sami@olmari.fi> * Wording in better English Co-authored-by: Tatu Wikman <tatu.wikman@gmail.com> * rename changelog entry to have correct extension --------- Signed-off-by: Tatu Wikman <tatu.wikman@gmail.com> Signed-off-by: Sami Olmari <sami@olmari.fi> Co-authored-by: Dirk Klimpel <5740567+dklimpel@users.noreply.github.com> Co-authored-by: Sami Olmari <sami@olmari.fi> Co-authored-by: Sami Olmari <sami+github@olmari.fi>
1 parent 301b415 commit 3e95c19

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

changelog.d/15411.doc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Docs: Add Nginx loadbalancing example with sticky mxid for workers.

docs/workers.md

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,7 @@ load balancing can be done in different ways.
325325

326326
For `/sync` and `/initialSync` requests it will be more efficient if all
327327
requests from a particular user are routed to a single instance. This can
328-
be done e.g. in nginx via IP `hash $http_x_forwarded_for;` or via
329-
`hash $http_authorization consistent;` which contains the users access token.
328+
be done in reverse proxy by extracting username part from the users access token.
330329

331330
Admins may additionally wish to separate out `/sync`
332331
requests that have a `since` query parameter from those that don't (and
@@ -335,6 +334,69 @@ when a user logs in on a new device and can be *very* resource intensive, so
335334
isolating these requests will stop them from interfering with other users ongoing
336335
syncs.
337336

337+
Example `nginx` configuration snippet that handles the cases above. This is just an
338+
example and probably requires some changes according to your particular setup:
339+
340+
```nginx
341+
# Choose sync worker based on the existence of "since" query parameter
342+
map $arg_since $sync {
343+
default synapse_sync;
344+
'' synapse_initial_sync;
345+
}
346+
347+
# Extract username from access token passed as URL parameter
348+
map $arg_access_token $accesstoken_from_urlparam {
349+
# Defaults to just passing back the whole accesstoken
350+
default $arg_access_token;
351+
# Try to extract username part from accesstoken URL parameter
352+
"~syt_(?<username>.*?)_.*" $username;
353+
}
354+
355+
# Extract username from access token passed as authorization header
356+
map $http_authorization $mxid_localpart {
357+
# Defaults to just passing back the whole accesstoken
358+
default $http_authorization;
359+
# Try to extract username part from accesstoken header
360+
"~Bearer syt_(?<username>.*?)_.*" $username;
361+
# if no authorization-header exist, try mapper for URL parameter "access_token"
362+
"" $accesstoken_from_urlparam;
363+
}
364+
365+
upstream synapse_initial_sync {
366+
# Use the username mapper result for hash key
367+
hash $mxid_localpart consistent;
368+
server 127.0.0.1:8016;
369+
server 127.0.0.1:8036;
370+
}
371+
372+
upstream synapse_sync {
373+
# Use the username mapper result for hash key
374+
hash $mxid_localpart consistent;
375+
server 127.0.0.1:8013;
376+
server 127.0.0.1:8037;
377+
server 127.0.0.1:8038;
378+
server 127.0.0.1:8039;
379+
}
380+
381+
# Sync initial/normal
382+
location ~ ^/_matrix/client/(r0|v3)/sync$ {
383+
proxy_pass http://$sync;
384+
}
385+
386+
# Normal sync
387+
location ~ ^/_matrix/client/(api/v1|r0|v3)/events$ {
388+
proxy_pass http://synapse_sync;
389+
}
390+
391+
# Initial_sync
392+
location ~ ^/_matrix/client/(api/v1|r0|v3)/initialSync$ {
393+
proxy_pass http://synapse_initial_sync;
394+
}
395+
location ~ ^/_matrix/client/(api/v1|r0|v3)/rooms/[^/]+/initialSync$ {
396+
proxy_pass http://synapse_initial_sync;
397+
}
398+
```
399+
338400
Federation and client requests can be balanced via simple round robin.
339401

340402
The inbound federation transaction request `^/_matrix/federation/v1/send/`

0 commit comments

Comments
 (0)