Matrix Docker Ansible eploy
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

355 Zeilen
18 KiB

  1. #jinja2: lstrip_blocks: True
  2. {% set room_workers = matrix_synapse_reverse_proxy_companion_synapse_workers_list | selectattr('type', 'equalto', 'room_worker') | list %}
  3. {% set sync_workers = matrix_synapse_reverse_proxy_companion_synapse_workers_list | selectattr('type', 'equalto', 'sync_worker') | list %}
  4. {% set client_reader_workers = matrix_synapse_reverse_proxy_companion_synapse_workers_list | selectattr('type', 'equalto', 'client_reader') | list %}
  5. {% set federation_reader_workers = matrix_synapse_reverse_proxy_companion_synapse_workers_list | selectattr('type', 'equalto', 'federation_reader') | list %}
  6. {% set generic_workers = matrix_synapse_reverse_proxy_companion_synapse_workers_list | selectattr('type', 'equalto', 'generic_worker') | list %}
  7. {% set stream_writer_typing_stream_workers = matrix_synapse_reverse_proxy_companion_synapse_workers_list | selectattr('type', 'equalto', 'stream_writer') | selectattr('stream_writer_stream', 'equalto', 'typing') | list %}
  8. {% set stream_writer_to_device_stream_workers = matrix_synapse_reverse_proxy_companion_synapse_workers_list | selectattr('type', 'equalto', 'stream_writer') | selectattr('stream_writer_stream', 'equalto', 'to_device') | list %}
  9. {% set stream_writer_account_data_stream_workers = matrix_synapse_reverse_proxy_companion_synapse_workers_list | selectattr('type', 'equalto', 'stream_writer') | selectattr('stream_writer_stream', 'equalto', 'account_data') | list %}
  10. {% set stream_writer_receipts_stream_workers = matrix_synapse_reverse_proxy_companion_synapse_workers_list | selectattr('type', 'equalto', 'stream_writer') | selectattr('stream_writer_stream', 'equalto', 'receipts') | list %}
  11. {% set stream_writer_presence_stream_workers = matrix_synapse_reverse_proxy_companion_synapse_workers_list | selectattr('type', 'equalto', 'stream_writer') | selectattr('stream_writer_stream', 'equalto', 'presence') | list %}
  12. {% set media_repository_workers = matrix_synapse_reverse_proxy_companion_synapse_workers_list | selectattr('type', 'equalto', 'media_repository') | list %}
  13. {% set user_dir_workers = matrix_synapse_reverse_proxy_companion_synapse_workers_list | selectattr('type', 'equalto', 'user_dir') | list %}
  14. {% macro render_worker_upstream(name, workers, load_balance) %}
  15. upstream {{ name }} {
  16. {#
  17. We need to use a zone so that the upstream is stored in shared memory,
  18. otherwise we can't use `resolve` below, as reported by nginx:
  19. > resolving names at run time requires upstream ".." in ... to be in shared memory
  20. #}
  21. zone {{ name }} 64k;
  22. {{ load_balance }}
  23. keepalive {{ ((workers | length) * 2) | string }};
  24. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  25. {% for worker in workers %}
  26. server "{{ worker.name }}:{{ worker.port }}" resolve;
  27. {% endfor %}
  28. }
  29. {% endmacro %}
  30. {% macro render_locations_to_upstream(locations, upstream_name) %}
  31. {% for location in locations %}
  32. location ~ {{ location }} {
  33. proxy_pass http://{{ upstream_name }}$request_uri;
  34. proxy_http_version 1.1;
  35. proxy_set_header Connection "";
  36. }
  37. {% endfor %}
  38. {% endmacro %}
  39. {% macro render_locations_to_upstream_with_whoami_sync_worker_router(locations, upstream_name) %}
  40. {% for location in locations %}
  41. location ~ {{ location }} {
  42. # Use auth_request to call the whoami sync worker router.
  43. # The handler resolves the access token to a user identifier and returns it
  44. # in the X-User-Identifier header, which is then used for upstream hashing.
  45. auth_request /_whoami_sync_worker_router;
  46. auth_request_set $user_identifier $sent_http_x_user_identifier;
  47. {% if matrix_synapse_reverse_proxy_companion_whoami_sync_worker_router_debug_headers_enabled %}
  48. add_header X-Sync-Worker-Router-User-Identifier $user_identifier always;
  49. add_header X-Sync-Worker-Router-Upstream $upstream_addr always;
  50. {% endif %}
  51. proxy_pass http://{{ upstream_name }}$request_uri;
  52. proxy_http_version 1.1;
  53. proxy_set_header Connection "";
  54. }
  55. {% endfor %}
  56. {% endmacro %}
  57. {% if matrix_synapse_reverse_proxy_companion_synapse_workers_enabled %}
  58. # Whether to upgrade HTTP connection
  59. map $http_upgrade $connection_upgrade {
  60. default upgrade;
  61. '' close;
  62. }
  63. #Extract room name from URI
  64. map $request_uri $room_name {
  65. ~^/_matrix/(client|federation)/.*?(?:%21|!)(?<room>[A-Za-z0-9._=\-\/]+)(?::|%3A)[A-Za-z0-9._=\-\/]+ $room;
  66. }
  67. # End maps
  68. {% if matrix_synapse_reverse_proxy_companion_synapse_cache_enabled %}
  69. proxy_cache_path {{ matrix_synapse_reverse_proxy_companion_synapse_cache_path }} levels=1:2 keys_zone={{ matrix_synapse_reverse_proxy_companion_synapse_cache_keys_zone_name }}:{{ matrix_synapse_reverse_proxy_companion_synapse_cache_keys_zone_size }} inactive={{ matrix_synapse_reverse_proxy_companion_synapse_cache_inactive_time }} max_size={{ matrix_synapse_reverse_proxy_companion_synapse_cache_max_size_mb }}m;
  70. {% endif %}
  71. # Round Robin "upstream" pools for workers
  72. {% if room_workers | length > 0 %}
  73. {{- render_worker_upstream('room_workers_upstream', room_workers, 'hash $room_name consistent;') }}
  74. {% endif %}
  75. {% if sync_workers | length > 0 %}
  76. {{- render_worker_upstream('sync_workers_upstream', sync_workers, 'hash $user_identifier consistent;') }}
  77. {% endif %}
  78. {% if client_reader_workers | length > 0 %}
  79. {{- render_worker_upstream('client_reader_workers_upstream', client_reader_workers, 'least_conn;') }}
  80. {% endif %}
  81. {% if federation_reader_workers | length > 0 %}
  82. {{- render_worker_upstream('federation_reader_workers_upstream', federation_reader_workers, 'hash $http_x_forwarded_for;') }}
  83. {% endif %}
  84. {% if generic_workers | length > 0 %}
  85. {{- render_worker_upstream('generic_workers_upstream', generic_workers, 'hash $http_x_forwarded_for;') }}
  86. {% endif %}
  87. {% if stream_writer_typing_stream_workers | length > 0 %}
  88. {{- render_worker_upstream('stream_writer_typing_stream_workers_upstream', stream_writer_typing_stream_workers, '') }}
  89. {% endif %}
  90. {% if stream_writer_to_device_stream_workers | length > 0 %}
  91. {{- render_worker_upstream('stream_writer_to_device_stream_workers_upstream', stream_writer_to_device_stream_workers, '') }}
  92. {% endif %}
  93. {% if stream_writer_account_data_stream_workers | length > 0 %}
  94. {{- render_worker_upstream('stream_writer_account_data_stream_workers_upstream', stream_writer_account_data_stream_workers, '') }}
  95. {% endif %}
  96. {% if stream_writer_receipts_stream_workers | length > 0 %}
  97. {{- render_worker_upstream('stream_writer_receipts_stream_workers_upstream', stream_writer_receipts_stream_workers, '') }}
  98. {% endif %}
  99. {% if stream_writer_presence_stream_workers | length > 0 %}
  100. {{- render_worker_upstream('stream_writer_presence_stream_workers_upstream', stream_writer_presence_stream_workers, '') }}
  101. {% endif %}
  102. {% if media_repository_workers | length > 0 %}
  103. {{- render_worker_upstream('media_repository_workers_upstream', media_repository_workers, 'least_conn;') }}
  104. {% endif %}
  105. {% if user_dir_workers | length > 0 %}
  106. {{- render_worker_upstream('user_dir_workers_upstream', user_dir_workers, '') }}
  107. {% endif %}
  108. {% endif %}
  109. server {
  110. listen 8008;
  111. server_name {{ matrix_synapse_reverse_proxy_companion_hostname }};
  112. server_tokens off;
  113. root /dev/null;
  114. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_client_api_client_max_body_size_mb }}M;
  115. client_body_buffer_size {{ matrix_synapse_reverse_proxy_companion_client_api_client_body_buffer_size_mb }}M;
  116. proxy_buffering on;
  117. proxy_max_temp_file_size 0;
  118. proxy_set_header Host $host;
  119. {% if matrix_synapse_reverse_proxy_companion_whoami_sync_worker_router_enabled %}
  120. # Internal location for whoami-based sync worker routing.
  121. # This is called via auth_request from sync worker locations.
  122. # The njs handler calls the whoami endpoint to resolve access tokens to usernames,
  123. # then returns the username in the X-User-Identifier header for upstream hashing.
  124. location = /_whoami_sync_worker_router {
  125. internal;
  126. js_content whoami_sync_worker_router.handleAuthRequest;
  127. }
  128. {% endif %}
  129. {% if matrix_synapse_reverse_proxy_companion_synapse_workers_enabled %}
  130. # Client-server overrides — These locations must go to the main Synapse process
  131. location ~ {{ matrix_synapse_reverse_proxy_companion_client_server_main_override_locations_regex }} {
  132. {# FIXME: This block was copied from the main Synapse fallback below. It would be better to have it in one place and avoid duplication. #}
  133. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  134. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  135. set $backend "{{ matrix_synapse_reverse_proxy_companion_client_api_addr }}";
  136. proxy_pass http://$backend;
  137. }
  138. # Client-server SSO overrides — These locations must go to the main Synapse process
  139. location ~ {{ matrix_synapse_reverse_proxy_companion_client_server_sso_override_locations_regex }} {
  140. {# FIXME: This block was copied from the main Synapse fallback below. It would be better to have it in one place and avoid duplication. #}
  141. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  142. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  143. set $backend "{{ matrix_synapse_reverse_proxy_companion_client_api_addr }}";
  144. proxy_pass http://$backend;
  145. }
  146. # QR code login (`rendezvous`) locations need to go to the same Synapse process.
  147. # It doesn't necessarily need to be the main process, but it needs to be consistent.
  148. # For simplicity, we'll send them to the main process though.
  149. location ~ {{ matrix_synapse_reverse_proxy_companion_client_server_qr_code_login_locations_regex }} {
  150. {# FIXME: This block was copied from the main Synapse fallback below. It would be better to have it in one place and avoid duplication. #}
  151. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  152. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  153. set $backend "{{ matrix_synapse_reverse_proxy_companion_client_api_addr }}";
  154. proxy_pass http://$backend;
  155. }
  156. {# Workers redirects BEGIN #}
  157. {% if generic_workers | length > 0 %}
  158. # https://matrix-org.github.io/synapse/latest/workers.html#synapseappgeneric_worker
  159. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_generic_worker_client_server_locations, 'generic_workers_upstream') }}
  160. {% endif %}
  161. {% if stream_writer_typing_stream_workers | length > 0 %}
  162. # https://matrix-org.github.io/synapse/latest/workers.html#the-typing-stream
  163. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_stream_writer_typing_stream_worker_client_server_locations, 'stream_writer_typing_stream_workers_upstream') }}
  164. {% endif %}
  165. {% if stream_writer_to_device_stream_workers | length > 0 %}
  166. # https://matrix-org.github.io/synapse/latest/workers.html#the-to_device-stream
  167. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_stream_writer_to_device_stream_worker_client_server_locations, 'stream_writer_to_device_stream_workers_upstream') }}
  168. {% endif %}
  169. {% if stream_writer_account_data_stream_workers | length > 0 %}
  170. # https://matrix-org.github.io/synapse/latest/workers.html#the-account_data-stream
  171. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_stream_writer_account_data_stream_worker_client_server_locations, 'stream_writer_account_data_stream_workers_upstream') }}
  172. {% endif %}
  173. {% if stream_writer_receipts_stream_workers | length > 0 %}
  174. # https://matrix-org.github.io/synapse/latest/workers.html#the-receipts-stream
  175. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_stream_writer_receipts_stream_worker_client_server_locations, 'stream_writer_receipts_stream_workers_upstream') }}
  176. {% endif %}
  177. {% if stream_writer_presence_stream_workers | length > 0 %}
  178. # https://matrix-org.github.io/synapse/latest/workers.html#the-presence-stream
  179. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_stream_writer_presence_stream_worker_client_server_locations, 'stream_writer_presence_stream_workers_upstream') }}
  180. {% endif %}
  181. {% if room_workers | length > 0 %}
  182. # room workers
  183. # https://tcpipuk.github.io/synapse/deployment/workers.html
  184. # https://tcpipuk.github.io/synapse/deployment/nginx.html#locationsconf
  185. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_room_worker_client_server_locations, 'room_workers_upstream') }}
  186. {% endif %}
  187. {% if sync_workers | length > 0 %}
  188. # sync workers
  189. # https://tcpipuk.github.io/synapse/deployment/workers.html
  190. # https://tcpipuk.github.io/synapse/deployment/nginx.html#locationsconf
  191. {{ render_locations_to_upstream_with_whoami_sync_worker_router(matrix_synapse_reverse_proxy_companion_synapse_sync_worker_client_server_locations, 'sync_workers_upstream') }}
  192. {% endif %}
  193. {% if client_reader_workers | length > 0 %}
  194. # client_reader workers
  195. # https://tcpipuk.github.io/synapse/deployment/workers.html
  196. # https://tcpipuk.github.io/synapse/deployment/nginx.html#locationsconf
  197. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_client_reader_client_server_locations, 'client_reader_workers_upstream') }}
  198. {% endif %}
  199. {% if media_repository_workers | length > 0 %}
  200. # https://matrix-org.github.io/synapse/latest/workers.html#synapseappmedia_repository
  201. {% for location in matrix_synapse_reverse_proxy_companion_synapse_media_repository_locations %}
  202. location ~ {{ location }} {
  203. proxy_pass http://media_repository_workers_upstream$request_uri;
  204. {% if matrix_synapse_reverse_proxy_companion_synapse_cache_enabled %}
  205. proxy_cache {{ matrix_synapse_reverse_proxy_companion_synapse_cache_keys_zone_name }};
  206. proxy_cache_valid any {{ matrix_synapse_reverse_proxy_companion_synapse_cache_proxy_cache_valid_time }};
  207. proxy_force_ranges on;
  208. add_header X-Cache-Status $upstream_cache_status;
  209. {% endif %}
  210. }
  211. {% endfor %}
  212. {% endif %}
  213. {% if user_dir_workers | length > 0 %}
  214. # https://matrix-org.github.io/synapse/latest/workers.html#updating-the-user-directory
  215. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_user_dir_locations, 'user_dir_workers_upstream') }}
  216. {% endif %}
  217. {# Workers redirects END #}
  218. {% endif %}
  219. {% for configuration_block in matrix_synapse_reverse_proxy_companion_synapse_client_api_additional_server_configuration_blocks %}
  220. {{- configuration_block }}
  221. {% endfor %}
  222. {# Everything else just goes to the API server ##}
  223. location / {
  224. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  225. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  226. set $backend "{{ matrix_synapse_reverse_proxy_companion_client_api_addr }}";
  227. proxy_pass http://$backend;
  228. }
  229. }
  230. {% if matrix_synapse_reverse_proxy_companion_federation_api_enabled %}
  231. server {
  232. listen 8048;
  233. server_name {{ matrix_synapse_reverse_proxy_companion_hostname }};
  234. server_tokens off;
  235. root /dev/null;
  236. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_federation_api_client_max_body_size_mb }}M;
  237. client_body_buffer_size {{ matrix_synapse_reverse_proxy_companion_federation_api_client_body_buffer_size_mb }}M;
  238. proxy_buffering on;
  239. proxy_max_temp_file_size 0;
  240. proxy_set_header Host $host;
  241. {% if matrix_synapse_reverse_proxy_companion_synapse_workers_enabled %}
  242. # Federation overrides — These locations must go to the main Synapse process
  243. location ~ {{ matrix_synapse_reverse_proxy_companion_federation_override_locations_regex }} {
  244. {# FIXME: This block was copied from the fallback location below. It would be better to have it in one place and avoid duplication. #}
  245. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  246. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  247. set $backend "{{ matrix_synapse_reverse_proxy_companion_federation_api_addr }}";
  248. proxy_pass http://$backend;
  249. }
  250. {% if room_workers | length > 0 %}
  251. # https://tcpipuk.github.io/synapse/deployment/workers.html
  252. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_room_worker_federation_locations, 'room_workers_upstream') }}
  253. {% endif %}
  254. {% if generic_workers | length > 0 %}
  255. # https://matrix-org.github.io/synapse/latest/workers.html#synapseappgeneric_worker
  256. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_generic_worker_federation_locations, 'generic_workers_upstream') }}
  257. {% endif %}
  258. {% if media_repository_workers | length > 0 %}
  259. # https://matrix-org.github.io/synapse/latest/workers.html#synapseappmedia_repository
  260. {% for location in matrix_synapse_reverse_proxy_companion_synapse_media_repository_locations %}
  261. location ~ {{ location }} {
  262. proxy_pass http://media_repository_workers_upstream$request_uri;
  263. {% if matrix_synapse_reverse_proxy_companion_synapse_cache_enabled %}
  264. proxy_buffering on;
  265. proxy_cache {{ matrix_synapse_reverse_proxy_companion_synapse_cache_keys_zone_name }};
  266. proxy_cache_valid any {{ matrix_synapse_reverse_proxy_companion_synapse_cache_proxy_cache_valid_time }};
  267. proxy_force_ranges on;
  268. add_header X-Cache-Status $upstream_cache_status;
  269. {% endif %}
  270. }
  271. {% endfor %}
  272. {% endif %}
  273. {#
  274. This is last, because we'd like more-specific requests (e.g. `/_matrix/federation/v1/media/` that may be handled by a media repository worker, if enabled)
  275. to be routed to more specialized workers via their respective `locations` defined earlier (above).
  276. As https://nginx.org/en/docs/http/ngx_http_core_module.html#location says about location matching:
  277. > .. Then regular expressions are checked, in the order of their appearance in the configuration file.
  278. See: https://github.com/spantaleev/matrix-docker-ansible-deploy/issues/3918
  279. #}
  280. {% if federation_reader_workers | length > 0 %}
  281. # https://tcpipuk.github.io/synapse/deployment/workers.html
  282. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_federation_reader_federation_locations, 'federation_reader_workers_upstream') }}
  283. {% endif %}
  284. {% endif %}
  285. {% for configuration_block in matrix_synapse_reverse_proxy_companion_synapse_federation_api_additional_server_configuration_blocks %}
  286. {{- configuration_block }}
  287. {% endfor %}
  288. location / {
  289. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  290. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  291. set $backend "{{ matrix_synapse_reverse_proxy_companion_federation_api_addr }}";
  292. proxy_pass http://$backend;
  293. }
  294. }
  295. {% endif %}