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.
 
 

409 lines
14 KiB

  1. #jinja2: lstrip_blocks: "True"
  2. {% macro render_nginx_status_location_block(addresses) %}
  3. {# Empty first line to make indentation prettier. #}
  4. location /nginx_status {
  5. stub_status on;
  6. access_log off;
  7. {% for address in addresses %}
  8. allow {{ address }};
  9. {% endfor %}
  10. deny all;
  11. }
  12. {% endmacro %}
  13. {% macro render_vhost_directives() %}
  14. gzip on;
  15. gzip_types text/plain application/json;
  16. location /.well-known/matrix {
  17. root {{ matrix_static_files_base_path }};
  18. {#
  19. A somewhat long expires value is used to prevent outages
  20. in case this is unreachable due to network failure or
  21. due to the base domain's server completely dying.
  22. #}
  23. expires 4h;
  24. default_type application/json;
  25. add_header Access-Control-Allow-Origin *;
  26. }
  27. {% if matrix_nginx_proxy_proxy_matrix_nginx_status_enabled %}
  28. {{ render_nginx_status_location_block(matrix_nginx_proxy_proxy_matrix_nginx_status_allowed_addresses) }}
  29. {% endif %}
  30. {% if matrix_nginx_proxy_proxy_matrix_corporal_api_enabled %}
  31. location ^~ /_matrix/corporal {
  32. {% if matrix_nginx_proxy_enabled %}
  33. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  34. resolver 127.0.0.11 valid=5s;
  35. set $backend "{{ matrix_nginx_proxy_proxy_matrix_corporal_api_addr_with_container }}";
  36. proxy_pass http://$backend;
  37. {% else %}
  38. {# Generic configuration for use outside of our container setup #}
  39. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_corporal_api_addr_sans_container }};
  40. {% endif %}
  41. proxy_set_header Host $host;
  42. proxy_set_header X-Forwarded-For $remote_addr;
  43. }
  44. {% endif %}
  45. {% if matrix_nginx_proxy_proxy_matrix_identity_api_enabled %}
  46. location ^~ /_matrix/identity {
  47. {% if matrix_nginx_proxy_enabled %}
  48. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  49. resolver 127.0.0.11 valid=5s;
  50. set $backend "{{ matrix_nginx_proxy_proxy_matrix_identity_api_addr_with_container }}";
  51. proxy_pass http://$backend;
  52. {% else %}
  53. {# Generic configuration for use outside of our container setup #}
  54. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_identity_api_addr_sans_container }};
  55. {% endif %}
  56. proxy_set_header Host $host;
  57. proxy_set_header X-Forwarded-For $remote_addr;
  58. }
  59. {% endif %}
  60. {% if matrix_nginx_proxy_proxy_matrix_user_directory_search_enabled %}
  61. # NOTE: This redirects user lookup requests to the identity server instead of
  62. # synapse, so user_dir_workers endpoints listed further down in this file will
  63. # not be reached and workers of this kind should be disabled for consistency.
  64. location ^~ /_matrix/client/r0/user_directory/search {
  65. {% if matrix_nginx_proxy_enabled %}
  66. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  67. resolver 127.0.0.11 valid=5s;
  68. set $backend "{{ matrix_nginx_proxy_proxy_matrix_user_directory_search_addr_with_container }}";
  69. proxy_pass http://$backend;
  70. {% else %}
  71. {# Generic configuration for use outside of our container setup #}
  72. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_user_directory_search_addr_sans_container }};
  73. {% endif %}
  74. proxy_set_header Host $host;
  75. proxy_set_header X-Forwarded-For $remote_addr;
  76. }
  77. {% endif %}
  78. {% if matrix_nginx_proxy_proxy_matrix_3pid_registration_enabled %}
  79. location ~ ^/_matrix/client/r0/register/(email|msisdn)/requestToken$ {
  80. {% if matrix_nginx_proxy_enabled %}
  81. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  82. resolver 127.0.0.11 valid=5s;
  83. set $backend "{{ matrix_nginx_proxy_proxy_matrix_3pid_registration_addr_with_container }}";
  84. proxy_pass http://$backend;
  85. {% else %}
  86. {# Generic configuration for use outside of our container setup #}
  87. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_3pid_registration_addr_sans_container }};
  88. {% endif %}
  89. proxy_set_header Host $host;
  90. proxy_set_header X-Forwarded-For $remote_addr;
  91. }
  92. {% endif %}
  93. {% if matrix_nginx_proxy_synapse_workers_enabled %}
  94. {# Workers redirects BEGIN #}
  95. {% if generic_workers %}
  96. # https://github.com/matrix-org/synapse/blob/master/docs/workers.md#synapseappgeneric_worker
  97. {% for location in matrix_nginx_proxy_synapse_generic_worker_locations %}
  98. location ~ {{ location }} {
  99. proxy_pass http://generic_worker_upstream$request_uri;
  100. proxy_set_header Host $host;
  101. proxy_set_header X-Forwarded-For $remote_addr;
  102. }
  103. {% endfor %}
  104. # FIXME: add GET ^/_matrix/federation/v1/groups/
  105. {% endif %}
  106. {% if media_repository_workers %}
  107. # https://github.com/matrix-org/synapse/blob/master/docs/workers.md#synapseappmedia_repository
  108. {% for location in matrix_nginx_proxy_synapse_media_repository_locations %}
  109. location ~ {{ location }} {
  110. proxy_pass http://media_repository_upstream$request_uri;
  111. proxy_set_header Host $host;
  112. proxy_set_header X-Forwarded-For $remote_addr;
  113. client_body_buffer_size 25M;
  114. client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_client_api_client_max_body_size_mb }}M;
  115. proxy_max_temp_file_size 0;
  116. }
  117. {% endfor %}
  118. {% endif %}
  119. {% if user_dir_workers %}
  120. # FIXME: obsolete if matrix_nginx_proxy_proxy_matrix_user_directory_search_enabled is set
  121. # https://github.com/matrix-org/synapse/blob/master/docs/workers.md#synapseappuser_dir
  122. {% for location in matrix_nginx_proxy_synapse_user_dir_locations %}
  123. location ~ {{ location }} {
  124. proxy_pass http://user_dir_upstream$request_uri;
  125. proxy_set_header Host $host;
  126. proxy_set_header X-Forwarded-For $remote_addr;
  127. }
  128. {% endfor %}
  129. {% endif %}
  130. {% if frontend_proxy_workers %}
  131. # https://github.com/matrix-org/synapse/blob/master/docs/workers.md#synapseappfrontend_proxy
  132. {% for location in matrix_nginx_proxy_synapse_frontend_proxy_locations %}
  133. location ~ {{ location }} {
  134. proxy_pass http://frontend_proxy_upstream$request_uri;
  135. proxy_set_header Host $host;
  136. proxy_set_header X-Forwarded-For $remote_addr;
  137. }
  138. {% endfor %}
  139. {% if matrix_nginx_proxy_synapse_presence_disabled %}
  140. # FIXME: keep in sync with synapse workers documentation manually
  141. location ~ ^/_matrix/client/(api/v1|r0|unstable)/presence/[^/]+/status {
  142. proxy_pass http://frontend_proxy_upstream$request_uri;
  143. proxy_set_header Host $host;
  144. proxy_set_header X-Forwarded-For $remote_addr;
  145. }
  146. {% endif %}
  147. {% endif %}
  148. {# Workers redirects END #}
  149. {% endif %}
  150. {% for configuration_block in matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks %}
  151. {{- configuration_block }}
  152. {% endfor %}
  153. {% if matrix_nginx_proxy_proxy_synapse_metrics %}
  154. location /_synapse/metrics {
  155. {% if matrix_nginx_proxy_enabled %}
  156. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  157. resolver 127.0.0.11 valid=5s;
  158. set $backend "{{ matrix_nginx_proxy_proxy_synapse_metrics_addr_with_container }}";
  159. proxy_pass http://$backend;
  160. {% else %}
  161. {# Generic configuration for use outside of our container setup #}
  162. proxy_pass http://{{ matrix_nginx_proxy_proxy_synapse_metrics_addr_sans_container }};
  163. {% endif %}
  164. proxy_set_header Host $host;
  165. proxy_set_header X-Forwarded-For $remote_addr;
  166. {% if matrix_nginx_proxy_proxy_synapse_metrics_basic_auth_enabled %}
  167. auth_basic "protected";
  168. auth_basic_user_file /nginx-data/matrix-synapse-metrics-htpasswd;
  169. {% endif %}
  170. }
  171. {% endif %}
  172. {#
  173. This handles the Matrix Client API only.
  174. The Matrix Federation API is handled by a separate vhost.
  175. #}
  176. location ~* ^({{ matrix_nginx_proxy_proxy_matrix_client_api_forwarded_location_prefix_regexes|join('|') }}) {
  177. {% if matrix_nginx_proxy_enabled %}
  178. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  179. resolver 127.0.0.11 valid=5s;
  180. set $backend "{{ matrix_nginx_proxy_proxy_matrix_client_api_addr_with_container }}";
  181. proxy_pass http://$backend;
  182. {% else %}
  183. {# Generic configuration for use outside of our container setup #}
  184. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_client_api_addr_sans_container }};
  185. {% endif %}
  186. proxy_set_header Host $host;
  187. proxy_set_header X-Forwarded-For $remote_addr;
  188. client_body_buffer_size 25M;
  189. client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_client_api_client_max_body_size_mb }}M;
  190. proxy_max_temp_file_size 0;
  191. }
  192. location / {
  193. {% if matrix_nginx_proxy_proxy_matrix_client_redirect_root_uri_to_domain %}
  194. return 302 $scheme://{{ matrix_nginx_proxy_proxy_matrix_client_redirect_root_uri_to_domain }}$request_uri;
  195. {% else %}
  196. rewrite ^/$ /_matrix/static/ last;
  197. {% endif %}
  198. }
  199. {% endmacro %}
  200. {% set generic_workers = matrix_nginx_proxy_synapse_workers_list|selectattr('type', 'equalto', 'generic_worker')|list %}
  201. {% set media_repository_workers = matrix_nginx_proxy_synapse_workers_list|selectattr('type', 'equalto', 'media_repository')|list %}
  202. {% set user_dir_workers = matrix_nginx_proxy_synapse_workers_list|selectattr('type', 'equalto', 'user_dir')|list %}
  203. {% set frontend_proxy_workers = matrix_nginx_proxy_synapse_workers_list|selectattr('type', 'equalto', 'frontend_proxy')|list %}
  204. {% if matrix_nginx_proxy_synapse_workers_enabled %}
  205. # Round Robin "upstream" pools for workers
  206. {% if generic_workers %}
  207. upstream generic_worker_upstream {
  208. # ensures that requests from the same client will always be passed
  209. # to the same server (except when this server is unavailable)
  210. ip_hash;
  211. {% for worker in generic_workers %}
  212. server "matrix-synapse:{{ worker.port }}";
  213. {% endfor %}
  214. }
  215. {% endif %}
  216. {% if frontend_proxy_workers %}
  217. upstream frontend_proxy_upstream {
  218. {% for worker in frontend_proxy_workers %}
  219. server "matrix-synapse:{{ worker.port }}";
  220. {% endfor %}
  221. }
  222. {% endif %}
  223. {% if media_repository_workers %}
  224. upstream media_repository_upstream {
  225. {% for worker in media_repository_workers %}
  226. server "matrix-synapse:{{ worker.port }}";
  227. {% endfor %}
  228. }
  229. {% endif %}
  230. {% if user_dir_workers %}
  231. upstream user_dir_upstream {
  232. {% for worker in user_dir_workers %}
  233. server "matrix-synapse:{{ worker.port }}";
  234. {% endfor %}
  235. }
  236. {% endif %}
  237. {% endif %}
  238. server {
  239. listen {{ 8080 if matrix_nginx_proxy_enabled else 80 }};
  240. server_name {{ matrix_nginx_proxy_proxy_matrix_hostname }};
  241. server_tokens off;
  242. root /dev/null;
  243. {% if matrix_nginx_proxy_https_enabled %}
  244. location /.well-known/acme-challenge {
  245. {% if matrix_nginx_proxy_enabled %}
  246. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  247. resolver 127.0.0.11 valid=5s;
  248. set $backend "matrix-certbot:8080";
  249. proxy_pass http://$backend;
  250. {% else %}
  251. {# Generic configuration for use outside of our container setup #}
  252. proxy_pass http://127.0.0.1:{{ matrix_ssl_lets_encrypt_certbot_standalone_http_port }};
  253. {% endif %}
  254. }
  255. {% if matrix_nginx_proxy_proxy_matrix_nginx_status_enabled %}
  256. {{ render_nginx_status_location_block(matrix_nginx_proxy_proxy_matrix_nginx_status_allowed_addresses) }}
  257. {% endif %}
  258. location / {
  259. return 301 https://$http_host$request_uri;
  260. }
  261. {% else %}
  262. {{ render_vhost_directives() }}
  263. {% endif %}
  264. }
  265. {% if matrix_nginx_proxy_https_enabled %}
  266. server {
  267. listen {{ 8443 if matrix_nginx_proxy_enabled else 443 }} ssl http2;
  268. listen [::]:{{ 8443 if matrix_nginx_proxy_enabled else 443 }} ssl http2;
  269. server_name {{ matrix_nginx_proxy_proxy_matrix_hostname }};
  270. server_tokens off;
  271. root /dev/null;
  272. ssl_certificate {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/fullchain.pem;
  273. ssl_certificate_key {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/privkey.pem;
  274. ssl_protocols {{ matrix_nginx_proxy_ssl_protocols }};
  275. {% if matrix_nginx_proxy_ssl_ciphers != '' %}
  276. ssl_ciphers {{ matrix_nginx_proxy_ssl_ciphers }};
  277. {% endif %}
  278. ssl_prefer_server_ciphers {{ matrix_nginx_proxy_ssl_prefer_server_ciphers }};
  279. {{ render_vhost_directives() }}
  280. }
  281. {% endif %}
  282. {% if matrix_nginx_proxy_proxy_matrix_federation_api_enabled %}
  283. {#
  284. This federation vhost is a little special.
  285. It serves federation over HTTP or HTTPS, depending on `matrix_nginx_proxy_https_enabled`.
  286. #}
  287. server {
  288. {% if matrix_nginx_proxy_https_enabled %}
  289. listen 8448 ssl http2;
  290. listen [::]:8448 ssl http2;
  291. {% else %}
  292. listen 8448;
  293. {% endif %}
  294. server_name {{ matrix_nginx_proxy_proxy_matrix_hostname }};
  295. server_tokens off;
  296. root /dev/null;
  297. gzip on;
  298. gzip_types text/plain application/json;
  299. {% if matrix_nginx_proxy_https_enabled %}
  300. ssl_certificate {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_certificate }};
  301. ssl_certificate_key {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_certificate_key }};
  302. ssl_protocols {{ matrix_nginx_proxy_ssl_protocols }};
  303. {% if matrix_nginx_proxy_ssl_ciphers != '' %}
  304. ssl_ciphers {{ matrix_nginx_proxy_ssl_ciphers }};
  305. {% endif %}
  306. ssl_prefer_server_ciphers {{ matrix_nginx_proxy_ssl_prefer_server_ciphers }};
  307. {% endif %}
  308. {% if matrix_nginx_proxy_synapse_workers_enabled %}
  309. {% if generic_workers %}
  310. # https://github.com/matrix-org/synapse/blob/master/docs/workers.md#synapseappgeneric_worker
  311. {% for location in matrix_nginx_proxy_synapse_generic_worker_locations %}
  312. location ~ {{ location }} {
  313. proxy_pass http://generic_worker_upstream$request_uri;
  314. proxy_set_header Host $host;
  315. proxy_set_header X-Forwarded-For $remote_addr;
  316. }
  317. {% endfor %}
  318. # FIXME: add GET ^/_matrix/federation/v1/groups/
  319. {% endif %}
  320. {% if media_repository_workers %}
  321. # https://github.com/matrix-org/synapse/blob/master/docs/workers.md#synapseappmedia_repository
  322. {% for location in matrix_nginx_proxy_synapse_media_repository_locations %}
  323. location ~ {{ location }} {
  324. proxy_pass http://media_repository_upstream$request_uri;
  325. proxy_set_header Host $host;
  326. proxy_set_header X-Forwarded-For $remote_addr;
  327. client_body_buffer_size 25M;
  328. client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_federation_api_client_max_body_size_mb }}M;
  329. proxy_max_temp_file_size 0;
  330. }
  331. {% endfor %}
  332. {% endif %}
  333. {% endif %}
  334. location / {
  335. {% if matrix_nginx_proxy_enabled %}
  336. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  337. resolver 127.0.0.11 valid=5s;
  338. set $backend "{{ matrix_nginx_proxy_proxy_matrix_federation_api_addr_with_container }}";
  339. proxy_pass http://$backend;
  340. {% else %}
  341. {# Generic configuration for use outside of our container setup #}
  342. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_federation_api_addr_sans_container }};
  343. {% endif %}
  344. proxy_set_header Host $host;
  345. proxy_set_header X-Forwarded-For $remote_addr;
  346. client_body_buffer_size 25M;
  347. client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_federation_api_client_max_body_size_mb }}M;
  348. proxy_max_temp_file_size 0;
  349. }
  350. }
  351. {% endif %}