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.
 
 

411 lines
16 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|v3)/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. {% if matrix_nginx_proxy_proxy_matrix_user_directory_search_v3_to_r0_redirect_enabled %}
  167. rewrite ^(.*?)/v3/(.*?)$ $1/r0/$2 break;
  168. {% endif %}
  169. proxy_pass http://$backend;
  170. {% else %}
  171. {% if matrix_nginx_proxy_proxy_matrix_user_directory_search_v3_to_r0_redirect_enabled %}
  172. rewrite ^(.*?)/v3/(.*?)$ $1/r0/$2 break;
  173. {% endif %}
  174. {# Generic configuration for use outside of our container setup #}
  175. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_user_directory_search_addr_sans_container }};
  176. {% endif %}
  177. proxy_set_header Host $host;
  178. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  179. }
  180. {% endif %}
  181. {% if matrix_nginx_proxy_proxy_matrix_3pid_registration_enabled %}
  182. location ~ ^/_matrix/client/(r0|v3)/register/(email|msisdn)/requestToken$ {
  183. {% if matrix_nginx_proxy_enabled %}
  184. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  185. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  186. set $backend "{{ matrix_nginx_proxy_proxy_matrix_3pid_registration_addr_with_container }}";
  187. {% if matrix_nginx_proxy_proxy_matrix_3pid_registration_v3_to_r0_redirect_enabled %}
  188. rewrite ^(.*?)/v3/(.*?)$ $1/r0/$2 break;
  189. {% endif %}
  190. proxy_pass http://$backend;
  191. {% else %}
  192. {% if matrix_nginx_proxy_proxy_matrix_3pid_registration_v3_to_r0_redirect_enabled %}
  193. rewrite ^(.*?)/v3/(.*?)$ $1/r0/$2 break;
  194. {% endif %}
  195. {# Generic configuration for use outside of our container setup #}
  196. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_3pid_registration_addr_sans_container }};
  197. {% endif %}
  198. proxy_set_header Host $host;
  199. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  200. proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
  201. }
  202. {% endif %}
  203. {% for configuration_block in matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks %}
  204. {{- configuration_block }}
  205. {% endfor %}
  206. {#
  207. This handles the Matrix Client API only.
  208. The Matrix Federation API is handled by a separate vhost.
  209. #}
  210. location ~* ^({{ matrix_nginx_proxy_proxy_matrix_client_api_forwarded_location_prefix_regexes|join('|') }}) {
  211. {% if matrix_nginx_proxy_enabled %}
  212. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  213. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  214. set $backend "{{ matrix_nginx_proxy_proxy_matrix_client_api_addr_with_container }}";
  215. proxy_pass http://$backend;
  216. {% else %}
  217. {# Generic configuration for use outside of our container setup #}
  218. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_client_api_addr_sans_container }};
  219. {% endif %}
  220. proxy_set_header Host $host;
  221. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  222. proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
  223. client_body_buffer_size 25M;
  224. client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_client_api_client_max_body_size_mb }}M;
  225. proxy_max_temp_file_size 0;
  226. }
  227. {#
  228. We only handle the root URI for this redirect or homepage serving.
  229. Unhandled URIs (mostly by `matrix_nginx_proxy_proxy_matrix_client_api_forwarded_location_prefix_regexes` above) should result in a 404,
  230. instead of causing a redirect.
  231. See: https://github.com/spantaleev/matrix-docker-ansible-deploy/issues/1058
  232. #}
  233. location ~* ^/$ {
  234. {% if matrix_nginx_proxy_proxy_matrix_client_redirect_root_uri_to_domain %}
  235. return 302 {{ matrix_nginx_proxy_x_forwarded_proto_value }}://{{ matrix_nginx_proxy_proxy_matrix_client_redirect_root_uri_to_domain }}$request_uri;
  236. {% else %}
  237. rewrite ^/$ /_matrix/static/ last;
  238. {% endif %}
  239. }
  240. {% endmacro %}
  241. server {
  242. listen {{ 8080 if matrix_nginx_proxy_enabled else 80 }};
  243. listen [::]:{{ 8080 if matrix_nginx_proxy_enabled else 80 }};
  244. server_name {{ matrix_nginx_proxy_proxy_matrix_hostname }};
  245. server_tokens off;
  246. root /dev/null;
  247. {% if matrix_nginx_proxy_https_enabled %}
  248. location /.well-known/acme-challenge {
  249. {% if matrix_nginx_proxy_enabled %}
  250. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  251. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  252. set $backend "matrix-certbot:8080";
  253. proxy_pass http://$backend;
  254. {% else %}
  255. {# Generic configuration for use outside of our container setup #}
  256. proxy_pass http://127.0.0.1:{{ matrix_ssl_lets_encrypt_certbot_standalone_http_port }};
  257. {% endif %}
  258. }
  259. {% if matrix_nginx_proxy_proxy_matrix_nginx_status_enabled %}
  260. {{ render_nginx_status_location_block(matrix_nginx_proxy_proxy_matrix_nginx_status_allowed_addresses) }}
  261. {% endif %}
  262. location / {
  263. return 301 https://$http_host$request_uri;
  264. }
  265. {% else %}
  266. {{ render_vhost_directives() }}
  267. {% endif %}
  268. }
  269. {% if matrix_nginx_proxy_https_enabled %}
  270. server {
  271. listen {{ 8443 if matrix_nginx_proxy_enabled else 443 }} ssl http2;
  272. listen [::]:{{ 8443 if matrix_nginx_proxy_enabled else 443 }} ssl http2;
  273. server_name {{ matrix_nginx_proxy_proxy_matrix_hostname }};
  274. server_tokens off;
  275. root /dev/null;
  276. ssl_certificate {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/fullchain.pem;
  277. ssl_certificate_key {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/privkey.pem;
  278. ssl_protocols {{ matrix_nginx_proxy_ssl_protocols }};
  279. {% if matrix_nginx_proxy_ssl_ciphers != '' %}
  280. ssl_ciphers {{ matrix_nginx_proxy_ssl_ciphers }};
  281. {% endif %}
  282. ssl_prefer_server_ciphers {{ matrix_nginx_proxy_ssl_prefer_server_ciphers }};
  283. {% if matrix_nginx_proxy_ocsp_stapling_enabled %}
  284. ssl_stapling on;
  285. ssl_stapling_verify on;
  286. ssl_trusted_certificate {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/chain.pem;
  287. {% endif %}
  288. {% if matrix_nginx_proxy_ssl_session_tickets_off %}
  289. ssl_session_tickets off;
  290. {% endif %}
  291. ssl_session_cache {{ matrix_nginx_proxy_ssl_session_cache }};
  292. ssl_session_timeout {{ matrix_nginx_proxy_ssl_session_timeout }};
  293. {{ render_vhost_directives() }}
  294. }
  295. {% endif %}
  296. {% if matrix_nginx_proxy_proxy_matrix_federation_api_enabled %}
  297. {#
  298. This federation vhost is a little special.
  299. It serves federation over HTTP or HTTPS, depending on `matrix_nginx_proxy_https_enabled`.
  300. #}
  301. server {
  302. {% if matrix_nginx_proxy_https_enabled %}
  303. listen {{ matrix_nginx_proxy_proxy_matrix_federation_port }} ssl http2;
  304. listen [::]:{{ matrix_nginx_proxy_proxy_matrix_federation_port }} ssl http2;
  305. {% else %}
  306. listen {{ matrix_nginx_proxy_proxy_matrix_federation_port }};
  307. {% endif %}
  308. server_name {{ matrix_nginx_proxy_proxy_matrix_federation_hostname }};
  309. server_tokens off;
  310. root /dev/null;
  311. gzip on;
  312. gzip_types text/plain application/json;
  313. {% if matrix_nginx_proxy_https_enabled %}
  314. ssl_certificate {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_certificate }};
  315. ssl_certificate_key {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_certificate_key }};
  316. ssl_protocols {{ matrix_nginx_proxy_ssl_protocols }};
  317. {% if matrix_nginx_proxy_ssl_ciphers != '' %}
  318. ssl_ciphers {{ matrix_nginx_proxy_ssl_ciphers }};
  319. {% endif %}
  320. ssl_prefer_server_ciphers {{ matrix_nginx_proxy_ssl_prefer_server_ciphers }};
  321. {% if matrix_nginx_proxy_ocsp_stapling_enabled %}
  322. ssl_stapling on;
  323. ssl_stapling_verify on;
  324. ssl_trusted_certificate {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_trusted_certificate }};
  325. {% endif %}
  326. {% if matrix_nginx_proxy_ssl_session_tickets_off %}
  327. ssl_session_tickets off;
  328. {% endif %}
  329. ssl_session_cache {{ matrix_nginx_proxy_ssl_session_cache }};
  330. ssl_session_timeout {{ matrix_nginx_proxy_ssl_session_timeout }};
  331. {% endif %}
  332. location / {
  333. {% if matrix_nginx_proxy_enabled %}
  334. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  335. resolver {{ matrix_nginx_proxy_http_level_resolver }} valid=5s;
  336. set $backend "{{ matrix_nginx_proxy_proxy_matrix_federation_api_addr_with_container }}";
  337. proxy_pass http://$backend;
  338. {% else %}
  339. {# Generic configuration for use outside of our container setup #}
  340. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_federation_api_addr_sans_container }};
  341. {% endif %}
  342. proxy_set_header Host $host;
  343. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  344. proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
  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 %}