Matrix Docker Ansible eploy
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

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