Matrix Docker Ansible eploy
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

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