Matrix Docker Ansible eploy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

327 rivejä
17 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. {% if workers | length > 0 %}
  16. upstream {{ name }} {
  17. {{ load_balance }}
  18. keepalive {{ ((workers | length) * 2) | string }};
  19. {% for worker in workers %}
  20. server "{{ worker.name }}:{{ worker.port }}";
  21. {% endfor %}
  22. }
  23. {% endif %}
  24. {% endmacro %}
  25. {% macro render_locations_to_upstream(locations, upstream_name) %}
  26. {% for location in locations %}
  27. location ~ {{ location }} {
  28. proxy_pass http://{{ upstream_name }}$request_uri;
  29. proxy_set_header Host $host;
  30. proxy_http_version 1.1;
  31. proxy_set_header Connection "";
  32. }
  33. {% endfor %}
  34. {% endmacro %}
  35. {% if matrix_synapse_reverse_proxy_companion_synapse_workers_enabled %}
  36. # Maps from https://tcpipuk.github.io/synapse/deployment/nginx.html#mapsconf
  37. # Client username from access token
  38. map $arg_access_token $accesstoken_from_urlparam {
  39. default $arg_access_token;
  40. "~syt_(?<username>.*?)_.*" $username;
  41. }
  42. # Client username from MXID
  43. map $http_authorization $mxid_localpart {
  44. default $http_authorization;
  45. "~Bearer syt_(?<username>.*?)_.*" $username;
  46. "" $accesstoken_from_urlparam;
  47. }
  48. # Whether to upgrade HTTP connection
  49. map $http_upgrade $connection_upgrade {
  50. default upgrade;
  51. '' close;
  52. }
  53. #Extract room name from URI
  54. map $request_uri $room_name {
  55. ~^/_matrix/(client|federation)/.*?(?:%21|!)(?<room>[A-Za-z0-9._=\-\/]+)(?::|%3A)[A-Za-z0-9._=\-\/]+ $room;
  56. }
  57. # End maps
  58. {% if matrix_synapse_reverse_proxy_companion_synapse_cache_enabled %}
  59. 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;
  60. {% endif %}
  61. # Round Robin "upstream" pools for workers
  62. {{ render_worker_upstream('room_workers_upstream', room_workers, 'hash $room_name consistent;') }}
  63. {{ render_worker_upstream('sync_workers_upstream', sync_workers, 'hash $mxid_localpart consistent;') }}
  64. {{ render_worker_upstream('client_reader_workers_upstream', client_reader_workers, 'least_conn;') }}
  65. {{ render_worker_upstream('federation_reader_workers_upstream', federation_reader_workers, 'hash $http_x_forwarded_for;') }}
  66. {{ render_worker_upstream('generic_workers_upstream', generic_workers, 'hash $http_x_forwarded_for;') }}
  67. {{ render_worker_upstream('stream_writer_typing_stream_workers_upstream', stream_writer_typing_stream_workers, '') }}
  68. {{ render_worker_upstream('stream_writer_to_device_stream_workers_upstream', stream_writer_to_device_stream_workers, '') }}
  69. {{ render_worker_upstream('stream_writer_account_data_stream_workers_upstream', stream_writer_account_data_stream_workers, '') }}
  70. {{ render_worker_upstream('stream_writer_receipts_stream_workers_upstream', stream_writer_receipts_stream_workers, '') }}
  71. {{ render_worker_upstream('stream_writer_presence_stream_workers_upstream', stream_writer_presence_stream_workers, '') }}
  72. {{ render_worker_upstream('media_repository_workers_upstream', media_repository_workers, 'least_conn;') }}
  73. {{ render_worker_upstream('user_dir_workers_upstream', user_dir_workers, '') }}
  74. {% endif %}
  75. server {
  76. listen 8008;
  77. server_name {{ matrix_synapse_reverse_proxy_companion_hostname }};
  78. server_tokens off;
  79. root /dev/null;
  80. gzip on;
  81. gzip_types text/plain application/json;
  82. {% if matrix_synapse_reverse_proxy_companion_synapse_workers_enabled %}
  83. # Client-server overrides -- These locations must go to the main Synapse process
  84. location ~ {{ matrix_synapse_reverse_proxy_companion_client_server_main_override_locations_regex }} {
  85. {# FIXME: This block was copied from the main Synapse fallback below. It would be better to have it in one place and avoid duplication. #}
  86. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  87. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  88. set $backend "{{ matrix_synapse_reverse_proxy_companion_client_api_addr }}";
  89. proxy_pass http://$backend;
  90. proxy_set_header Host $host;
  91. client_body_buffer_size 25M;
  92. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_client_api_client_max_body_size_mb }}M;
  93. proxy_max_temp_file_size 0;
  94. }
  95. # Client-server SSO overrides -- These locations must go to the main Synapse process
  96. location ~ {{ matrix_synapse_reverse_proxy_companion_client_server_sso_override_locations_regex }} {
  97. {# FIXME: This block was copied from the main Synapse fallback below. It would be better to have it in one place and avoid duplication. #}
  98. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  99. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  100. set $backend "{{ matrix_synapse_reverse_proxy_companion_client_api_addr }}";
  101. proxy_pass http://$backend;
  102. proxy_set_header Host $host;
  103. client_body_buffer_size 25M;
  104. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_client_api_client_max_body_size_mb }}M;
  105. proxy_max_temp_file_size 0;
  106. }
  107. # QR code login (`rendezvous`) locations need to go to the same Synapse process.
  108. # It doesn't necessarily need to be the main process, but it needs to be consistent.
  109. # For simplicity, we'll send them to the main process though.
  110. location ~ {{ matrix_synapse_reverse_proxy_companion_client_server_qr_code_login_locations_regex }} {
  111. {# FIXME: This block was copied from the main Synapse fallback below. It would be better to have it in one place and avoid duplication. #}
  112. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  113. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  114. set $backend "{{ matrix_synapse_reverse_proxy_companion_client_api_addr }}";
  115. proxy_pass http://$backend;
  116. proxy_set_header Host $host;
  117. client_body_buffer_size 25M;
  118. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_client_api_client_max_body_size_mb }}M;
  119. proxy_max_temp_file_size 0;
  120. }
  121. {# Workers redirects BEGIN #}
  122. {% if generic_workers | length > 0 %}
  123. # https://matrix-org.github.io/synapse/latest/workers.html#synapseappgeneric_worker
  124. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_generic_worker_client_server_locations, 'generic_workers_upstream') }}
  125. {% endif %}
  126. {% if stream_writer_typing_stream_workers | length > 0 %}
  127. # https://matrix-org.github.io/synapse/latest/workers.html#the-typing-stream
  128. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_stream_writer_typing_stream_worker_client_server_locations, 'stream_writer_typing_stream_workers_upstream') }}
  129. {% endif %}
  130. {% if stream_writer_to_device_stream_workers | length > 0 %}
  131. # https://matrix-org.github.io/synapse/latest/workers.html#the-to_device-stream
  132. {{ 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') }}
  133. {% endif %}
  134. {% if stream_writer_account_data_stream_workers | length > 0 %}
  135. # https://matrix-org.github.io/synapse/latest/workers.html#the-account_data-stream
  136. {{ 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') }}
  137. {% endif %}
  138. {% if stream_writer_receipts_stream_workers | length > 0 %}
  139. # https://matrix-org.github.io/synapse/latest/workers.html#the-receipts-stream
  140. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_stream_writer_receipts_stream_worker_client_server_locations, 'stream_writer_receipts_stream_workers_upstream') }}
  141. {% endif %}
  142. {% if stream_writer_presence_stream_workers | length > 0 %}
  143. # https://matrix-org.github.io/synapse/latest/workers.html#the-presence-stream
  144. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_stream_writer_presence_stream_worker_client_server_locations, 'stream_writer_presence_stream_workers_upstream') }}
  145. {% endif %}
  146. {% if room_workers | length > 0 %}
  147. # room workers
  148. # https://tcpipuk.github.io/synapse/deployment/workers.html
  149. # https://tcpipuk.github.io/synapse/deployment/nginx.html#locationsconf
  150. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_room_worker_client_server_locations, 'room_workers_upstream') }}
  151. {% endif %}
  152. {% if sync_workers | length > 0 %}
  153. # sync workers
  154. # https://tcpipuk.github.io/synapse/deployment/workers.html
  155. # https://tcpipuk.github.io/synapse/deployment/nginx.html#locationsconf
  156. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_sync_worker_client_server_locations, 'sync_workers_upstream') }}
  157. {% endif %}
  158. {% if client_reader_workers | length > 0 %}
  159. # client_reader workers
  160. # https://tcpipuk.github.io/synapse/deployment/workers.html
  161. # https://tcpipuk.github.io/synapse/deployment/nginx.html#locationsconf
  162. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_client_reader_client_server_locations, 'client_reader_workers_upstream') }}
  163. {% endif %}
  164. {% if media_repository_workers | length > 0 %}
  165. # https://matrix-org.github.io/synapse/latest/workers.html#synapseappmedia_repository
  166. {% for location in matrix_synapse_reverse_proxy_companion_synapse_media_repository_locations %}
  167. location ~ {{ location }} {
  168. proxy_pass http://media_repository_workers_upstream$request_uri;
  169. proxy_set_header Host $host;
  170. client_body_buffer_size 25M;
  171. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_client_api_client_max_body_size_mb }}M;
  172. proxy_max_temp_file_size 0;
  173. {% if matrix_synapse_reverse_proxy_companion_synapse_cache_enabled %}
  174. proxy_buffering on;
  175. proxy_cache {{ matrix_synapse_reverse_proxy_companion_synapse_cache_keys_zone_name }};
  176. proxy_cache_valid any {{ matrix_synapse_reverse_proxy_companion_synapse_cache_proxy_cache_valid_time }};
  177. proxy_force_ranges on;
  178. add_header X-Cache-Status $upstream_cache_status;
  179. {% endif %}
  180. }
  181. {% endfor %}
  182. {% endif %}
  183. {% if user_dir_workers | length > 0 %}
  184. # https://matrix-org.github.io/synapse/latest/workers.html#updating-the-user-directory
  185. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_user_dir_locations, 'user_dir_workers_upstream') }}
  186. {% endif %}
  187. {# Workers redirects END #}
  188. {% endif %}
  189. {% for configuration_block in matrix_synapse_reverse_proxy_companion_synapse_client_api_additional_server_configuration_blocks %}
  190. {{- configuration_block }}
  191. {% endfor %}
  192. {# Everything else just goes to the API server ##}
  193. location / {
  194. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  195. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  196. set $backend "{{ matrix_synapse_reverse_proxy_companion_client_api_addr }}";
  197. proxy_pass http://$backend;
  198. proxy_set_header Host $host;
  199. client_body_buffer_size 25M;
  200. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_client_api_client_max_body_size_mb }}M;
  201. proxy_max_temp_file_size 0;
  202. }
  203. }
  204. {% if matrix_synapse_reverse_proxy_companion_federation_api_enabled %}
  205. server {
  206. listen 8048;
  207. server_name {{ matrix_synapse_reverse_proxy_companion_hostname }};
  208. server_tokens off;
  209. root /dev/null;
  210. gzip on;
  211. gzip_types text/plain application/json;
  212. {% if matrix_synapse_reverse_proxy_companion_synapse_workers_enabled %}
  213. # Federation overrides -- These locations must go to the main Synapse process
  214. location ~ {{ matrix_synapse_reverse_proxy_companion_federation_override_locations_regex }} {
  215. {# FIXME: This block was copied from the fallback location below. It would be better to have it in one place and avoid duplication. #}
  216. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  217. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  218. set $backend "{{ matrix_synapse_reverse_proxy_companion_federation_api_addr }}";
  219. proxy_pass http://$backend;
  220. proxy_set_header Host $host;
  221. client_body_buffer_size 25M;
  222. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_federation_api_client_max_body_size_mb }}M;
  223. proxy_max_temp_file_size 0;
  224. }
  225. {% if room_workers | length > 0 %}
  226. # https://tcpipuk.github.io/synapse/deployment/workers.html
  227. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_room_worker_federation_locations, 'room_workers_upstream') }}
  228. {% endif %}
  229. {% if federation_reader_workers | length > 0 %}
  230. # https://tcpipuk.github.io/synapse/deployment/workers.html
  231. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_federation_reader_federation_locations, 'federation_reader_workers_upstream') }}
  232. {% endif %}
  233. {% if generic_workers | length > 0 %}
  234. # https://matrix-org.github.io/synapse/latest/workers.html#synapseappgeneric_worker
  235. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_generic_worker_federation_locations, 'generic_workers_upstream') }}
  236. {% endif %}
  237. {% if media_repository_workers | length > 0 %}
  238. # https://matrix-org.github.io/synapse/latest/workers.html#synapseappmedia_repository
  239. {% for location in matrix_synapse_reverse_proxy_companion_synapse_media_repository_locations %}
  240. location ~ {{ location }} {
  241. proxy_pass http://media_repository_workers_upstream$request_uri;
  242. proxy_set_header Host $host;
  243. client_body_buffer_size 25M;
  244. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_federation_api_client_max_body_size_mb }}M;
  245. proxy_max_temp_file_size 0;
  246. {% if matrix_synapse_reverse_proxy_companion_synapse_cache_enabled %}
  247. proxy_buffering on;
  248. proxy_cache {{ matrix_synapse_reverse_proxy_companion_synapse_cache_keys_zone_name }};
  249. proxy_cache_valid any {{ matrix_synapse_reverse_proxy_companion_synapse_cache_proxy_cache_valid_time }};
  250. proxy_force_ranges on;
  251. add_header X-Cache-Status $upstream_cache_status;
  252. {% endif %}
  253. }
  254. {% endfor %}
  255. {% endif %}
  256. {% endif %}
  257. {% for configuration_block in matrix_synapse_reverse_proxy_companion_synapse_federation_api_additional_server_configuration_blocks %}
  258. {{- configuration_block }}
  259. {% endfor %}
  260. location / {
  261. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  262. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  263. set $backend "{{ matrix_synapse_reverse_proxy_companion_federation_api_addr }}";
  264. proxy_pass http://$backend;
  265. proxy_set_header Host $host;
  266. client_body_buffer_size 25M;
  267. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_federation_api_client_max_body_size_mb }}M;
  268. proxy_max_temp_file_size 0;
  269. }
  270. }
  271. {% endif %}