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.
 
 

333 lines
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. {% 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. {% if matrix_synapse_reverse_proxy_companion_synapse_workers_enabled %}
  81. # Client-server overrides — These locations must go to the main Synapse process
  82. location ~ {{ matrix_synapse_reverse_proxy_companion_client_server_main_override_locations_regex }} {
  83. {# FIXME: This block was copied from the main Synapse fallback below. It would be better to have it in one place and avoid duplication. #}
  84. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  85. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  86. set $backend "{{ matrix_synapse_reverse_proxy_companion_client_api_addr }}";
  87. proxy_pass http://$backend;
  88. proxy_set_header Host $host;
  89. client_body_buffer_size 25M;
  90. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_client_api_client_max_body_size_mb }}M;
  91. proxy_max_temp_file_size 0;
  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. proxy_set_header Host $host;
  101. client_body_buffer_size 25M;
  102. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_client_api_client_max_body_size_mb }}M;
  103. proxy_max_temp_file_size 0;
  104. }
  105. # QR code login (`rendezvous`) locations need to go to the same Synapse process.
  106. # It doesn't necessarily need to be the main process, but it needs to be consistent.
  107. # For simplicity, we'll send them to the main process though.
  108. location ~ {{ matrix_synapse_reverse_proxy_companion_client_server_qr_code_login_locations_regex }} {
  109. {# FIXME: This block was copied from the main Synapse fallback below. It would be better to have it in one place and avoid duplication. #}
  110. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  111. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  112. set $backend "{{ matrix_synapse_reverse_proxy_companion_client_api_addr }}";
  113. proxy_pass http://$backend;
  114. proxy_set_header Host $host;
  115. client_body_buffer_size 25M;
  116. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_client_api_client_max_body_size_mb }}M;
  117. proxy_max_temp_file_size 0;
  118. }
  119. {# Workers redirects BEGIN #}
  120. {% if generic_workers | length > 0 %}
  121. # https://matrix-org.github.io/synapse/latest/workers.html#synapseappgeneric_worker
  122. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_generic_worker_client_server_locations, 'generic_workers_upstream') }}
  123. {% endif %}
  124. {% if stream_writer_typing_stream_workers | length > 0 %}
  125. # https://matrix-org.github.io/synapse/latest/workers.html#the-typing-stream
  126. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_stream_writer_typing_stream_worker_client_server_locations, 'stream_writer_typing_stream_workers_upstream') }}
  127. {% endif %}
  128. {% if stream_writer_to_device_stream_workers | length > 0 %}
  129. # https://matrix-org.github.io/synapse/latest/workers.html#the-to_device-stream
  130. {{ 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') }}
  131. {% endif %}
  132. {% if stream_writer_account_data_stream_workers | length > 0 %}
  133. # https://matrix-org.github.io/synapse/latest/workers.html#the-account_data-stream
  134. {{ 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') }}
  135. {% endif %}
  136. {% if stream_writer_receipts_stream_workers | length > 0 %}
  137. # https://matrix-org.github.io/synapse/latest/workers.html#the-receipts-stream
  138. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_stream_writer_receipts_stream_worker_client_server_locations, 'stream_writer_receipts_stream_workers_upstream') }}
  139. {% endif %}
  140. {% if stream_writer_presence_stream_workers | length > 0 %}
  141. # https://matrix-org.github.io/synapse/latest/workers.html#the-presence-stream
  142. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_stream_writer_presence_stream_worker_client_server_locations, 'stream_writer_presence_stream_workers_upstream') }}
  143. {% endif %}
  144. {% if room_workers | length > 0 %}
  145. # room workers
  146. # https://tcpipuk.github.io/synapse/deployment/workers.html
  147. # https://tcpipuk.github.io/synapse/deployment/nginx.html#locationsconf
  148. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_room_worker_client_server_locations, 'room_workers_upstream') }}
  149. {% endif %}
  150. {% if sync_workers | length > 0 %}
  151. # sync workers
  152. # https://tcpipuk.github.io/synapse/deployment/workers.html
  153. # https://tcpipuk.github.io/synapse/deployment/nginx.html#locationsconf
  154. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_sync_worker_client_server_locations, 'sync_workers_upstream') }}
  155. {% endif %}
  156. {% if client_reader_workers | length > 0 %}
  157. # client_reader workers
  158. # https://tcpipuk.github.io/synapse/deployment/workers.html
  159. # https://tcpipuk.github.io/synapse/deployment/nginx.html#locationsconf
  160. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_client_reader_client_server_locations, 'client_reader_workers_upstream') }}
  161. {% endif %}
  162. {% if media_repository_workers | length > 0 %}
  163. # https://matrix-org.github.io/synapse/latest/workers.html#synapseappmedia_repository
  164. {% for location in matrix_synapse_reverse_proxy_companion_synapse_media_repository_locations %}
  165. location ~ {{ location }} {
  166. proxy_pass http://media_repository_workers_upstream$request_uri;
  167. proxy_set_header Host $host;
  168. client_body_buffer_size 25M;
  169. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_client_api_client_max_body_size_mb }}M;
  170. proxy_max_temp_file_size 0;
  171. {% if matrix_synapse_reverse_proxy_companion_synapse_cache_enabled %}
  172. proxy_buffering on;
  173. proxy_cache {{ matrix_synapse_reverse_proxy_companion_synapse_cache_keys_zone_name }};
  174. proxy_cache_valid any {{ matrix_synapse_reverse_proxy_companion_synapse_cache_proxy_cache_valid_time }};
  175. proxy_force_ranges on;
  176. add_header X-Cache-Status $upstream_cache_status;
  177. {% endif %}
  178. }
  179. {% endfor %}
  180. {% endif %}
  181. {% if user_dir_workers | length > 0 %}
  182. # https://matrix-org.github.io/synapse/latest/workers.html#updating-the-user-directory
  183. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_user_dir_locations, 'user_dir_workers_upstream') }}
  184. {% endif %}
  185. {# Workers redirects END #}
  186. {% endif %}
  187. {% for configuration_block in matrix_synapse_reverse_proxy_companion_synapse_client_api_additional_server_configuration_blocks %}
  188. {{- configuration_block }}
  189. {% endfor %}
  190. {# Everything else just goes to the API server ##}
  191. location / {
  192. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  193. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  194. set $backend "{{ matrix_synapse_reverse_proxy_companion_client_api_addr }}";
  195. proxy_pass http://$backend;
  196. proxy_set_header Host $host;
  197. client_body_buffer_size 25M;
  198. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_client_api_client_max_body_size_mb }}M;
  199. proxy_max_temp_file_size 0;
  200. }
  201. }
  202. {% if matrix_synapse_reverse_proxy_companion_federation_api_enabled %}
  203. server {
  204. listen 8048;
  205. server_name {{ matrix_synapse_reverse_proxy_companion_hostname }};
  206. server_tokens off;
  207. root /dev/null;
  208. {% if matrix_synapse_reverse_proxy_companion_synapse_workers_enabled %}
  209. # Federation overrides — These locations must go to the main Synapse process
  210. location ~ {{ matrix_synapse_reverse_proxy_companion_federation_override_locations_regex }} {
  211. {# FIXME: This block was copied from the fallback location below. It would be better to have it in one place and avoid duplication. #}
  212. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  213. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  214. set $backend "{{ matrix_synapse_reverse_proxy_companion_federation_api_addr }}";
  215. proxy_pass http://$backend;
  216. proxy_set_header Host $host;
  217. client_body_buffer_size 25M;
  218. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_federation_api_client_max_body_size_mb }}M;
  219. proxy_max_temp_file_size 0;
  220. }
  221. {% if room_workers | length > 0 %}
  222. # https://tcpipuk.github.io/synapse/deployment/workers.html
  223. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_room_worker_federation_locations, 'room_workers_upstream') }}
  224. {% endif %}
  225. {% if generic_workers | length > 0 %}
  226. # https://matrix-org.github.io/synapse/latest/workers.html#synapseappgeneric_worker
  227. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_generic_worker_federation_locations, 'generic_workers_upstream') }}
  228. {% endif %}
  229. {% if media_repository_workers | length > 0 %}
  230. # https://matrix-org.github.io/synapse/latest/workers.html#synapseappmedia_repository
  231. {% for location in matrix_synapse_reverse_proxy_companion_synapse_media_repository_locations %}
  232. location ~ {{ location }} {
  233. proxy_pass http://media_repository_workers_upstream$request_uri;
  234. proxy_set_header Host $host;
  235. client_body_buffer_size 25M;
  236. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_federation_api_client_max_body_size_mb }}M;
  237. proxy_max_temp_file_size 0;
  238. {% if matrix_synapse_reverse_proxy_companion_synapse_cache_enabled %}
  239. proxy_buffering on;
  240. proxy_cache {{ matrix_synapse_reverse_proxy_companion_synapse_cache_keys_zone_name }};
  241. proxy_cache_valid any {{ matrix_synapse_reverse_proxy_companion_synapse_cache_proxy_cache_valid_time }};
  242. proxy_force_ranges on;
  243. add_header X-Cache-Status $upstream_cache_status;
  244. {% endif %}
  245. }
  246. {% endfor %}
  247. {% endif %}
  248. {#
  249. 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)
  250. to be routed to more specialized workers via their respective `locations` defined earlier (above).
  251. As https://nginx.org/en/docs/http/ngx_http_core_module.html#location says about location matching:
  252. > .. Then regular expressions are checked, in the order of their appearance in the configuration file.
  253. See: https://github.com/spantaleev/matrix-docker-ansible-deploy/issues/3918
  254. #}
  255. {% if federation_reader_workers | length > 0 %}
  256. # https://tcpipuk.github.io/synapse/deployment/workers.html
  257. {{ render_locations_to_upstream(matrix_synapse_reverse_proxy_companion_synapse_federation_reader_federation_locations, 'federation_reader_workers_upstream') }}
  258. {% endif %}
  259. {% endif %}
  260. {% for configuration_block in matrix_synapse_reverse_proxy_companion_synapse_federation_api_additional_server_configuration_blocks %}
  261. {{- configuration_block }}
  262. {% endfor %}
  263. location / {
  264. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  265. resolver {{ matrix_synapse_reverse_proxy_companion_http_level_resolver }} valid=5s;
  266. set $backend "{{ matrix_synapse_reverse_proxy_companion_federation_api_addr }}";
  267. proxy_pass http://$backend;
  268. proxy_set_header Host $host;
  269. client_body_buffer_size 25M;
  270. client_max_body_size {{ matrix_synapse_reverse_proxy_companion_federation_api_client_max_body_size_mb }}M;
  271. proxy_max_temp_file_size 0;
  272. }
  273. }
  274. {% endif %}