Matrix Docker Ansible eploy
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

408 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_client_server_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. {% endif %}
  105. {% if media_repository_workers %}
  106. # https://github.com/matrix-org/synapse/blob/master/docs/workers.md#synapseappmedia_repository
  107. {% for location in matrix_nginx_proxy_synapse_media_repository_locations %}
  108. location ~ {{ location }} {
  109. proxy_pass http://media_repository_upstream$request_uri;
  110. proxy_set_header Host $host;
  111. proxy_set_header X-Forwarded-For $remote_addr;
  112. client_body_buffer_size 25M;
  113. client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_client_api_client_max_body_size_mb }}M;
  114. proxy_max_temp_file_size 0;
  115. }
  116. {% endfor %}
  117. {% endif %}
  118. {% if user_dir_workers %}
  119. # FIXME: obsolete if matrix_nginx_proxy_proxy_matrix_user_directory_search_enabled is set
  120. # https://github.com/matrix-org/synapse/blob/master/docs/workers.md#synapseappuser_dir
  121. {% for location in matrix_nginx_proxy_synapse_user_dir_locations %}
  122. location ~ {{ location }} {
  123. proxy_pass http://user_dir_upstream$request_uri;
  124. proxy_set_header Host $host;
  125. proxy_set_header X-Forwarded-For $remote_addr;
  126. }
  127. {% endfor %}
  128. {% endif %}
  129. {% if frontend_proxy_workers %}
  130. # https://github.com/matrix-org/synapse/blob/master/docs/workers.md#synapseappfrontend_proxy
  131. {% for location in matrix_nginx_proxy_synapse_frontend_proxy_locations %}
  132. location ~ {{ location }} {
  133. proxy_pass http://frontend_proxy_upstream$request_uri;
  134. proxy_set_header Host $host;
  135. proxy_set_header X-Forwarded-For $remote_addr;
  136. }
  137. {% endfor %}
  138. {% if matrix_nginx_proxy_synapse_presence_disabled %}
  139. # FIXME: keep in sync with synapse workers documentation manually
  140. location ~ ^/_matrix/client/(api/v1|r0|unstable)/presence/[^/]+/status {
  141. proxy_pass http://frontend_proxy_upstream$request_uri;
  142. proxy_set_header Host $host;
  143. proxy_set_header X-Forwarded-For $remote_addr;
  144. }
  145. {% endif %}
  146. {% endif %}
  147. {# Workers redirects END #}
  148. {% endif %}
  149. {% for configuration_block in matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks %}
  150. {{- configuration_block }}
  151. {% endfor %}
  152. {% if matrix_nginx_proxy_proxy_synapse_metrics %}
  153. location /_synapse/metrics {
  154. {% if matrix_nginx_proxy_enabled %}
  155. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  156. resolver 127.0.0.11 valid=5s;
  157. set $backend "{{ matrix_nginx_proxy_proxy_synapse_metrics_addr_with_container }}";
  158. proxy_pass http://$backend;
  159. {% else %}
  160. {# Generic configuration for use outside of our container setup #}
  161. proxy_pass http://{{ matrix_nginx_proxy_proxy_synapse_metrics_addr_sans_container }};
  162. {% endif %}
  163. proxy_set_header Host $host;
  164. proxy_set_header X-Forwarded-For $remote_addr;
  165. {% if matrix_nginx_proxy_proxy_synapse_metrics_basic_auth_enabled %}
  166. auth_basic "protected";
  167. auth_basic_user_file /nginx-data/matrix-synapse-metrics-htpasswd;
  168. {% endif %}
  169. }
  170. {% endif %}
  171. {#
  172. This handles the Matrix Client API only.
  173. The Matrix Federation API is handled by a separate vhost.
  174. #}
  175. location ~* ^({{ matrix_nginx_proxy_proxy_matrix_client_api_forwarded_location_prefix_regexes|join('|') }}) {
  176. {% if matrix_nginx_proxy_enabled %}
  177. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  178. resolver 127.0.0.11 valid=5s;
  179. set $backend "{{ matrix_nginx_proxy_proxy_matrix_client_api_addr_with_container }}";
  180. proxy_pass http://$backend;
  181. {% else %}
  182. {# Generic configuration for use outside of our container setup #}
  183. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_client_api_addr_sans_container }};
  184. {% endif %}
  185. proxy_set_header Host $host;
  186. proxy_set_header X-Forwarded-For $remote_addr;
  187. client_body_buffer_size 25M;
  188. client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_client_api_client_max_body_size_mb }}M;
  189. proxy_max_temp_file_size 0;
  190. }
  191. location / {
  192. {% if matrix_nginx_proxy_proxy_matrix_client_redirect_root_uri_to_domain %}
  193. return 302 $scheme://{{ matrix_nginx_proxy_proxy_matrix_client_redirect_root_uri_to_domain }}$request_uri;
  194. {% else %}
  195. rewrite ^/$ /_matrix/static/ last;
  196. {% endif %}
  197. }
  198. {% endmacro %}
  199. {% set generic_workers = matrix_nginx_proxy_synapse_workers_list|selectattr('type', 'equalto', 'generic_worker')|list %}
  200. {% set media_repository_workers = matrix_nginx_proxy_synapse_workers_list|selectattr('type', 'equalto', 'media_repository')|list %}
  201. {% set user_dir_workers = matrix_nginx_proxy_synapse_workers_list|selectattr('type', 'equalto', 'user_dir')|list %}
  202. {% set frontend_proxy_workers = matrix_nginx_proxy_synapse_workers_list|selectattr('type', 'equalto', 'frontend_proxy')|list %}
  203. {% if matrix_nginx_proxy_synapse_workers_enabled %}
  204. # Round Robin "upstream" pools for workers
  205. {% if generic_workers %}
  206. upstream generic_worker_upstream {
  207. # ensures that requests from the same client will always be passed
  208. # to the same server (except when this server is unavailable)
  209. ip_hash;
  210. {% for worker in generic_workers %}
  211. server "matrix-synapse:{{ worker.port }}";
  212. {% endfor %}
  213. }
  214. {% endif %}
  215. {% if frontend_proxy_workers %}
  216. upstream frontend_proxy_upstream {
  217. {% for worker in frontend_proxy_workers %}
  218. server "matrix-synapse:{{ worker.port }}";
  219. {% endfor %}
  220. }
  221. {% endif %}
  222. {% if media_repository_workers %}
  223. upstream media_repository_upstream {
  224. {% for worker in media_repository_workers %}
  225. server "matrix-synapse:{{ worker.port }}";
  226. {% endfor %}
  227. }
  228. {% endif %}
  229. {% if user_dir_workers %}
  230. upstream user_dir_upstream {
  231. {% for worker in user_dir_workers %}
  232. server "matrix-synapse:{{ worker.port }}";
  233. {% endfor %}
  234. }
  235. {% endif %}
  236. {% endif %}
  237. server {
  238. listen {{ 8080 if matrix_nginx_proxy_enabled else 80 }};
  239. server_name {{ matrix_nginx_proxy_proxy_matrix_hostname }};
  240. server_tokens off;
  241. root /dev/null;
  242. {% if matrix_nginx_proxy_https_enabled %}
  243. location /.well-known/acme-challenge {
  244. {% if matrix_nginx_proxy_enabled %}
  245. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  246. resolver 127.0.0.11 valid=5s;
  247. set $backend "matrix-certbot:8080";
  248. proxy_pass http://$backend;
  249. {% else %}
  250. {# Generic configuration for use outside of our container setup #}
  251. proxy_pass http://127.0.0.1:{{ matrix_ssl_lets_encrypt_certbot_standalone_http_port }};
  252. {% endif %}
  253. }
  254. {% if matrix_nginx_proxy_proxy_matrix_nginx_status_enabled %}
  255. {{ render_nginx_status_location_block(matrix_nginx_proxy_proxy_matrix_nginx_status_allowed_addresses) }}
  256. {% endif %}
  257. location / {
  258. return 301 https://$http_host$request_uri;
  259. }
  260. {% else %}
  261. {{ render_vhost_directives() }}
  262. {% endif %}
  263. }
  264. {% if matrix_nginx_proxy_https_enabled %}
  265. server {
  266. listen {{ 8443 if matrix_nginx_proxy_enabled else 443 }} ssl http2;
  267. listen [::]:{{ 8443 if matrix_nginx_proxy_enabled else 443 }} ssl http2;
  268. server_name {{ matrix_nginx_proxy_proxy_matrix_hostname }};
  269. server_tokens off;
  270. root /dev/null;
  271. ssl_certificate {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/fullchain.pem;
  272. ssl_certificate_key {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/privkey.pem;
  273. ssl_protocols {{ matrix_nginx_proxy_ssl_protocols }};
  274. {% if matrix_nginx_proxy_ssl_ciphers != '' %}
  275. ssl_ciphers {{ matrix_nginx_proxy_ssl_ciphers }};
  276. {% endif %}
  277. ssl_prefer_server_ciphers {{ matrix_nginx_proxy_ssl_prefer_server_ciphers }};
  278. {{ render_vhost_directives() }}
  279. }
  280. {% endif %}
  281. {% if matrix_nginx_proxy_proxy_matrix_federation_api_enabled %}
  282. {#
  283. This federation vhost is a little special.
  284. It serves federation over HTTP or HTTPS, depending on `matrix_nginx_proxy_https_enabled`.
  285. #}
  286. server {
  287. {% if matrix_nginx_proxy_https_enabled %}
  288. listen 8448 ssl http2;
  289. listen [::]:8448 ssl http2;
  290. {% else %}
  291. listen 8448;
  292. {% endif %}
  293. server_name {{ matrix_nginx_proxy_proxy_matrix_hostname }};
  294. server_tokens off;
  295. root /dev/null;
  296. gzip on;
  297. gzip_types text/plain application/json;
  298. {% if matrix_nginx_proxy_https_enabled %}
  299. ssl_certificate {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_certificate }};
  300. ssl_certificate_key {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_certificate_key }};
  301. ssl_protocols {{ matrix_nginx_proxy_ssl_protocols }};
  302. {% if matrix_nginx_proxy_ssl_ciphers != '' %}
  303. ssl_ciphers {{ matrix_nginx_proxy_ssl_ciphers }};
  304. {% endif %}
  305. ssl_prefer_server_ciphers {{ matrix_nginx_proxy_ssl_prefer_server_ciphers }};
  306. {% endif %}
  307. {% if matrix_nginx_proxy_synapse_workers_enabled %}
  308. {% if generic_workers %}
  309. # https://github.com/matrix-org/synapse/blob/master/docs/workers.md#synapseappgeneric_worker
  310. {% for location in matrix_nginx_proxy_synapse_generic_worker_federation_locations %}
  311. location ~ {{ location }} {
  312. proxy_pass http://generic_worker_upstream$request_uri;
  313. proxy_set_header Host $host;
  314. proxy_set_header X-Forwarded-For $remote_addr;
  315. }
  316. {% endfor %}
  317. # FIXME: add GET ^/_matrix/federation/v1/groups/
  318. {% endif %}
  319. {% if media_repository_workers %}
  320. # https://github.com/matrix-org/synapse/blob/master/docs/workers.md#synapseappmedia_repository
  321. {% for location in matrix_nginx_proxy_synapse_media_repository_locations %}
  322. location ~ {{ location }} {
  323. proxy_pass http://media_repository_upstream$request_uri;
  324. proxy_set_header Host $host;
  325. proxy_set_header X-Forwarded-For $remote_addr;
  326. client_body_buffer_size 25M;
  327. client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_federation_api_client_max_body_size_mb }}M;
  328. proxy_max_temp_file_size 0;
  329. }
  330. {% endfor %}
  331. {% endif %}
  332. {% endif %}
  333. location / {
  334. {% if matrix_nginx_proxy_enabled %}
  335. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  336. resolver 127.0.0.11 valid=5s;
  337. set $backend "{{ matrix_nginx_proxy_proxy_matrix_federation_api_addr_with_container }}";
  338. proxy_pass http://$backend;
  339. {% else %}
  340. {# Generic configuration for use outside of our container setup #}
  341. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_federation_api_addr_sans_container }};
  342. {% endif %}
  343. proxy_set_header Host $host;
  344. proxy_set_header X-Forwarded-For $remote_addr;
  345. client_body_buffer_size 25M;
  346. client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_federation_api_client_max_body_size_mb }}M;
  347. proxy_max_temp_file_size 0;
  348. }
  349. }
  350. {% endif %}