Matrix Docker Ansible eploy
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 

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