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.
 
 

399 line
15 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. {% if matrix_nginx_proxy_floc_optout_enabled %}
  17. add_header Permissions-Policy interest-cohort=() always;
  18. {% endif %}
  19. {% if matrix_nginx_proxy_hsts_preload_enabled %}
  20. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
  21. {% else %}
  22. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
  23. {% endif %}
  24. add_header X-XSS-Protection "{{ matrix_nginx_proxy_xss_protection }}";
  25. location /.well-known/matrix {
  26. root {{ matrix_static_files_base_path }};
  27. {#
  28. A somewhat long expires value is used to prevent outages
  29. in case this is unreachable due to network failure or
  30. due to the base domain's server completely dying.
  31. #}
  32. expires 4h;
  33. default_type application/json;
  34. add_header Access-Control-Allow-Origin *;
  35. }
  36. {% if matrix_nginx_proxy_proxy_matrix_nginx_status_enabled %}
  37. {{ render_nginx_status_location_block(matrix_nginx_proxy_proxy_matrix_nginx_status_allowed_addresses) }}
  38. {% endif %}
  39. {% if matrix_nginx_proxy_proxy_matrix_metrics_enabled %}
  40. location /metrics {
  41. {% if matrix_nginx_proxy_proxy_matrix_metrics_basic_auth_enabled %}
  42. auth_basic "protected";
  43. auth_basic_user_file {{ matrix_nginx_proxy_proxy_matrix_metrics_basic_auth_path }};
  44. {% endif %}
  45. {% for configuration_block in matrix_nginx_proxy_proxy_matrix_metrics_additional_location_configuration_blocks %}
  46. {{- configuration_block }}
  47. {% endfor %}
  48. }
  49. {% endif %}
  50. {% if matrix_nginx_proxy_proxy_matrix_corporal_api_enabled %}
  51. location ^~ /_matrix/corporal {
  52. {% if matrix_nginx_proxy_enabled %}
  53. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  54. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  55. set $backend "{{ matrix_nginx_proxy_proxy_matrix_corporal_api_addr_with_container }}";
  56. proxy_pass http://$backend;
  57. {% else %}
  58. {# Generic configuration for use outside of our container setup #}
  59. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_corporal_api_addr_sans_container }};
  60. {% endif %}
  61. proxy_set_header Host $host;
  62. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  63. proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
  64. }
  65. {% endif %}
  66. {% if matrix_nginx_proxy_proxy_matrix_identity_api_enabled %}
  67. location ^~ /_matrix/identity {
  68. {% if matrix_nginx_proxy_enabled %}
  69. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  70. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  71. set $backend "{{ matrix_nginx_proxy_proxy_matrix_identity_api_addr_with_container }}";
  72. proxy_pass http://$backend;
  73. {% else %}
  74. {# Generic configuration for use outside of our container setup #}
  75. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_identity_api_addr_sans_container }};
  76. {% endif %}
  77. proxy_set_header Host $host;
  78. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  79. proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
  80. }
  81. {% endif %}
  82. {% if matrix_nginx_proxy_proxy_media_repo_enabled %}
  83. # Redirect all media endpoints to the media-repo
  84. location ^~ /_matrix/media {
  85. {% if matrix_nginx_proxy_enabled %}
  86. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  87. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  88. set $backend "{{ matrix_nginx_proxy_proxy_media_repo_addr_with_container }}";
  89. proxy_pass http://$backend;
  90. {% else %}
  91. {# Generic configuration for use outside of our container setup #}
  92. proxy_pass http://{{ matrix_nginx_proxy_proxy_media_repo_addr_sans_container }};
  93. {% endif %}
  94. # Make sure this matches your homeserver in media-repo.yaml
  95. # You may have to manually specify it if using delegation or the
  96. # incoming Host doesn't match.
  97. proxy_set_header Host $host;
  98. proxy_set_header X-Real-IP $remote_addr;
  99. proxy_set_header X-Forwarded-For $remote_addr;
  100. }
  101. # Redirect other endpoints registered by the media-repo to its container
  102. # /_matrix/client/r0/logout
  103. # /_matrix/client/r0/logout/all
  104. location ^~ /_matrix/client/(r0|v1|v3|unstable)/(logout|logout/all) {
  105. {% if matrix_nginx_proxy_enabled %}
  106. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  107. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  108. set $backend "{{ matrix_nginx_proxy_proxy_media_repo_addr_with_container }}";
  109. proxy_pass http://$backend;
  110. {% else %}
  111. {# Generic configuration for use outside of our container setup #}
  112. proxy_pass http://{{ matrix_nginx_proxy_proxy_media_repo_addr_sans_container }};
  113. {% endif %}
  114. # Make sure this matches your homeserver in media-repo.yaml
  115. # You may have to manually specify it if using delegation or the
  116. # incoming Host doesn't match.
  117. proxy_set_header Host $host;
  118. proxy_set_header X-Real-IP $remote_addr;
  119. proxy_set_header X-Forwarded-For $remote_addr;
  120. }
  121. # Redirect other endpoints registered by the media-repo to its container
  122. # /_matrix/client/r0/admin/purge_media_cache
  123. # /_matrix/client/r0/admin/quarantine_media/{roomId:[^/]+}
  124. location ^~ /_matrix/client/(r0|v1|v3|unstable)/admin/(purge_media_cache|quarantine_media/.*) {
  125. {% if matrix_nginx_proxy_enabled %}
  126. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  127. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  128. set $backend "{{ matrix_nginx_proxy_proxy_media_repo_addr_with_container }}";
  129. proxy_pass http://$backend;
  130. {% else %}
  131. {# Generic configuration for use outside of our container setup #}
  132. proxy_pass http://{{ matrix_nginx_proxy_proxy_media_repo_addr_sans_container }};
  133. {% endif %}
  134. # Make sure this matches your homeserver in media-repo.yaml
  135. # You may have to manually specify it if using delegation or the
  136. # incoming Host doesn't match.
  137. proxy_set_header Host $host;
  138. proxy_set_header X-Real-IP $remote_addr;
  139. proxy_set_header X-Forwarded-For $remote_addr;
  140. }
  141. # Redirect other endpoints registered by the media-repo to its container
  142. location ^~ /_matrix/client/unstable/io.t2bot.media {
  143. {% if matrix_nginx_proxy_enabled %}
  144. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  145. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  146. set $backend "{{ matrix_nginx_proxy_proxy_media_repo_addr_with_container }}";
  147. proxy_pass http://$backend;
  148. {% else %}
  149. {# Generic configuration for use outside of our container setup #}
  150. proxy_pass http://{{ matrix_nginx_proxy_proxy_media_repo_addr_sans_container }};
  151. {% endif %}
  152. # Make sure this matches your homeserver in media-repo.yaml
  153. # You may have to manually specify it if using delegation or the
  154. # incoming Host doesn't match.
  155. proxy_set_header Host $host;
  156. proxy_set_header X-Real-IP $remote_addr;
  157. proxy_set_header X-Forwarded-For $remote_addr;
  158. }
  159. {% endif %}
  160. {% if matrix_nginx_proxy_proxy_matrix_user_directory_search_enabled %}
  161. location ^~ /_matrix/client/r0/user_directory/search {
  162. {% if matrix_nginx_proxy_enabled %}
  163. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  164. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  165. set $backend "{{ matrix_nginx_proxy_proxy_matrix_user_directory_search_addr_with_container }}";
  166. proxy_pass http://$backend;
  167. {% else %}
  168. {# Generic configuration for use outside of our container setup #}
  169. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_user_directory_search_addr_sans_container }};
  170. {% endif %}
  171. proxy_set_header Host $host;
  172. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  173. }
  174. {% endif %}
  175. {% if matrix_nginx_proxy_proxy_matrix_3pid_registration_enabled %}
  176. location ~ ^/_matrix/client/r0/register/(email|msisdn)/requestToken$ {
  177. {% if matrix_nginx_proxy_enabled %}
  178. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  179. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  180. set $backend "{{ matrix_nginx_proxy_proxy_matrix_3pid_registration_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_3pid_registration_addr_sans_container }};
  185. {% endif %}
  186. proxy_set_header Host $host;
  187. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  188. proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
  189. }
  190. {% endif %}
  191. {% for configuration_block in matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks %}
  192. {{- configuration_block }}
  193. {% endfor %}
  194. {#
  195. This handles the Matrix Client API only.
  196. The Matrix Federation API is handled by a separate vhost.
  197. #}
  198. location ~* ^({{ matrix_nginx_proxy_proxy_matrix_client_api_forwarded_location_prefix_regexes|join('|') }}) {
  199. {% if matrix_nginx_proxy_enabled %}
  200. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  201. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  202. set $backend "{{ matrix_nginx_proxy_proxy_matrix_client_api_addr_with_container }}";
  203. proxy_pass http://$backend;
  204. {% else %}
  205. {# Generic configuration for use outside of our container setup #}
  206. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_client_api_addr_sans_container }};
  207. {% endif %}
  208. proxy_set_header Host $host;
  209. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  210. proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
  211. client_body_buffer_size 25M;
  212. client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_client_api_client_max_body_size_mb }}M;
  213. proxy_max_temp_file_size 0;
  214. }
  215. {#
  216. We only handle the root URI for this redirect or homepage serving.
  217. Unhandled URIs (mostly by `matrix_nginx_proxy_proxy_matrix_client_api_forwarded_location_prefix_regexes` above) should result in a 404,
  218. instead of causing a redirect.
  219. See: https://github.com/spantaleev/matrix-docker-ansible-deploy/issues/1058
  220. #}
  221. location ~* ^/$ {
  222. {% if matrix_nginx_proxy_proxy_matrix_client_redirect_root_uri_to_domain %}
  223. return 302 {{ matrix_nginx_proxy_x_forwarded_proto_value }}://{{ matrix_nginx_proxy_proxy_matrix_client_redirect_root_uri_to_domain }}$request_uri;
  224. {% else %}
  225. rewrite ^/$ /_matrix/static/ last;
  226. {% endif %}
  227. }
  228. {% endmacro %}
  229. server {
  230. listen {{ 8080 if matrix_nginx_proxy_enabled else 80 }};
  231. listen [::]:{{ 8080 if matrix_nginx_proxy_enabled else 80 }};
  232. server_name {{ matrix_nginx_proxy_proxy_matrix_hostname }};
  233. server_tokens off;
  234. root /dev/null;
  235. {% if matrix_nginx_proxy_https_enabled %}
  236. location /.well-known/acme-challenge {
  237. {% if matrix_nginx_proxy_enabled %}
  238. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  239. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  240. set $backend "matrix-certbot:8080";
  241. proxy_pass http://$backend;
  242. {% else %}
  243. {# Generic configuration for use outside of our container setup #}
  244. proxy_pass http://127.0.0.1:{{ matrix_ssl_lets_encrypt_certbot_standalone_http_port }};
  245. {% endif %}
  246. }
  247. {% if matrix_nginx_proxy_proxy_matrix_nginx_status_enabled %}
  248. {{ render_nginx_status_location_block(matrix_nginx_proxy_proxy_matrix_nginx_status_allowed_addresses) }}
  249. {% endif %}
  250. location / {
  251. return 301 https://$http_host$request_uri;
  252. }
  253. {% else %}
  254. {{ render_vhost_directives() }}
  255. {% endif %}
  256. }
  257. {% if matrix_nginx_proxy_https_enabled %}
  258. server {
  259. listen {{ 8443 if matrix_nginx_proxy_enabled else 443 }} ssl http2;
  260. listen [::]:{{ 8443 if matrix_nginx_proxy_enabled else 443 }} ssl http2;
  261. server_name {{ matrix_nginx_proxy_proxy_matrix_hostname }};
  262. server_tokens off;
  263. root /dev/null;
  264. ssl_certificate {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/fullchain.pem;
  265. ssl_certificate_key {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/privkey.pem;
  266. ssl_protocols {{ matrix_nginx_proxy_ssl_protocols }};
  267. {% if matrix_nginx_proxy_ssl_ciphers != '' %}
  268. ssl_ciphers {{ matrix_nginx_proxy_ssl_ciphers }};
  269. {% endif %}
  270. ssl_prefer_server_ciphers {{ matrix_nginx_proxy_ssl_prefer_server_ciphers }};
  271. {% if matrix_nginx_proxy_ocsp_stapling_enabled %}
  272. ssl_stapling on;
  273. ssl_stapling_verify on;
  274. ssl_trusted_certificate {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/chain.pem;
  275. {% endif %}
  276. {% if matrix_nginx_proxy_ssl_session_tickets_off %}
  277. ssl_session_tickets off;
  278. {% endif %}
  279. ssl_session_cache {{ matrix_nginx_proxy_ssl_session_cache }};
  280. ssl_session_timeout {{ matrix_nginx_proxy_ssl_session_timeout }};
  281. {{ render_vhost_directives() }}
  282. }
  283. {% endif %}
  284. {% if matrix_nginx_proxy_proxy_matrix_federation_api_enabled %}
  285. {#
  286. This federation vhost is a little special.
  287. It serves federation over HTTP or HTTPS, depending on `matrix_nginx_proxy_https_enabled`.
  288. #}
  289. server {
  290. {% if matrix_nginx_proxy_https_enabled %}
  291. listen {{ matrix_nginx_proxy_proxy_matrix_federation_port }} ssl http2;
  292. listen [::]:{{ matrix_nginx_proxy_proxy_matrix_federation_port }} ssl http2;
  293. {% else %}
  294. listen {{ matrix_nginx_proxy_proxy_matrix_federation_port }};
  295. {% endif %}
  296. server_name {{ matrix_nginx_proxy_proxy_matrix_federation_hostname }};
  297. server_tokens off;
  298. root /dev/null;
  299. gzip on;
  300. gzip_types text/plain application/json;
  301. {% if matrix_nginx_proxy_https_enabled %}
  302. ssl_certificate {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_certificate }};
  303. ssl_certificate_key {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_certificate_key }};
  304. ssl_protocols {{ matrix_nginx_proxy_ssl_protocols }};
  305. {% if matrix_nginx_proxy_ssl_ciphers != '' %}
  306. ssl_ciphers {{ matrix_nginx_proxy_ssl_ciphers }};
  307. {% endif %}
  308. ssl_prefer_server_ciphers {{ matrix_nginx_proxy_ssl_prefer_server_ciphers }};
  309. {% if matrix_nginx_proxy_ocsp_stapling_enabled %}
  310. ssl_stapling on;
  311. ssl_stapling_verify on;
  312. ssl_trusted_certificate {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_trusted_certificate }};
  313. {% endif %}
  314. {% if matrix_nginx_proxy_ssl_session_tickets_off %}
  315. ssl_session_tickets off;
  316. {% endif %}
  317. ssl_session_cache {{ matrix_nginx_proxy_ssl_session_cache }};
  318. ssl_session_timeout {{ matrix_nginx_proxy_ssl_session_timeout }};
  319. {% endif %}
  320. location / {
  321. {% if matrix_nginx_proxy_enabled %}
  322. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  323. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  324. set $backend "{{ matrix_nginx_proxy_proxy_matrix_federation_api_addr_with_container }}";
  325. proxy_pass http://$backend;
  326. {% else %}
  327. {# Generic configuration for use outside of our container setup #}
  328. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_federation_api_addr_sans_container }};
  329. {% endif %}
  330. proxy_set_header Host $host;
  331. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  332. proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
  333. client_body_buffer_size 25M;
  334. client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_federation_api_client_max_body_size_mb }}M;
  335. proxy_max_temp_file_size 0;
  336. }
  337. }
  338. {% endif %}