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.
 
 

296 lines
11 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_corporal_api_enabled %}
  40. location ^~ /_matrix/corporal {
  41. {% if matrix_nginx_proxy_enabled %}
  42. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  43. resolver 127.0.0.11 valid=5s;
  44. set $backend "{{ matrix_nginx_proxy_proxy_matrix_corporal_api_addr_with_container }}";
  45. proxy_pass http://$backend;
  46. {% else %}
  47. {# Generic configuration for use outside of our container setup #}
  48. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_corporal_api_addr_sans_container }};
  49. {% endif %}
  50. proxy_set_header Host $host;
  51. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  52. proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
  53. }
  54. {% endif %}
  55. {% if matrix_nginx_proxy_proxy_matrix_identity_api_enabled %}
  56. location ^~ /_matrix/identity {
  57. {% if matrix_nginx_proxy_enabled %}
  58. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  59. resolver 127.0.0.11 valid=5s;
  60. set $backend "{{ matrix_nginx_proxy_proxy_matrix_identity_api_addr_with_container }}";
  61. proxy_pass http://$backend;
  62. {% else %}
  63. {# Generic configuration for use outside of our container setup #}
  64. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_identity_api_addr_sans_container }};
  65. {% endif %}
  66. proxy_set_header Host $host;
  67. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  68. proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
  69. }
  70. {% endif %}
  71. {% if matrix_nginx_proxy_proxy_matrix_user_directory_search_enabled %}
  72. location ^~ /_matrix/client/r0/user_directory/search {
  73. {% if matrix_nginx_proxy_enabled %}
  74. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  75. resolver 127.0.0.11 valid=5s;
  76. set $backend "{{ matrix_nginx_proxy_proxy_matrix_user_directory_search_addr_with_container }}";
  77. proxy_pass http://$backend;
  78. {% else %}
  79. {# Generic configuration for use outside of our container setup #}
  80. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_user_directory_search_addr_sans_container }};
  81. {% endif %}
  82. proxy_set_header Host $host;
  83. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  84. }
  85. {% endif %}
  86. {% if matrix_nginx_proxy_proxy_matrix_3pid_registration_enabled %}
  87. location ~ ^/_matrix/client/r0/register/(email|msisdn)/requestToken$ {
  88. {% if matrix_nginx_proxy_enabled %}
  89. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  90. resolver 127.0.0.11 valid=5s;
  91. set $backend "{{ matrix_nginx_proxy_proxy_matrix_3pid_registration_addr_with_container }}";
  92. proxy_pass http://$backend;
  93. {% else %}
  94. {# Generic configuration for use outside of our container setup #}
  95. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_3pid_registration_addr_sans_container }};
  96. {% endif %}
  97. proxy_set_header Host $host;
  98. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  99. proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
  100. }
  101. {% endif %}
  102. {% for configuration_block in matrix_nginx_proxy_proxy_matrix_additional_server_configuration_blocks %}
  103. {{- configuration_block }}
  104. {% endfor %}
  105. {#
  106. This handles the Matrix Client API only.
  107. The Matrix Federation API is handled by a separate vhost.
  108. #}
  109. location ~* ^({{ matrix_nginx_proxy_proxy_matrix_client_api_forwarded_location_prefix_regexes|join('|') }}) {
  110. {% if matrix_nginx_proxy_enabled %}
  111. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  112. resolver 127.0.0.11 valid=5s;
  113. set $backend "{{ matrix_nginx_proxy_proxy_matrix_client_api_addr_with_container }}";
  114. proxy_pass http://$backend;
  115. {% else %}
  116. {# Generic configuration for use outside of our container setup #}
  117. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_client_api_addr_sans_container }};
  118. {% endif %}
  119. proxy_set_header Host $host;
  120. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  121. proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
  122. client_body_buffer_size 25M;
  123. client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_client_api_client_max_body_size_mb }}M;
  124. proxy_max_temp_file_size 0;
  125. }
  126. {#
  127. We only handle the root URI for this redirect or homepage serving.
  128. Unhandled URIs (mostly by `matrix_nginx_proxy_proxy_matrix_client_api_forwarded_location_prefix_regexes` above) should result in a 404,
  129. instead of causing a redirect.
  130. See: https://github.com/spantaleev/matrix-docker-ansible-deploy/issues/1058
  131. #}
  132. location ~* ^/$ {
  133. {% if matrix_nginx_proxy_proxy_matrix_client_redirect_root_uri_to_domain %}
  134. return 302 {{ matrix_nginx_proxy_x_forwarded_proto_value }}://{{ matrix_nginx_proxy_proxy_matrix_client_redirect_root_uri_to_domain }}$request_uri;
  135. {% else %}
  136. rewrite ^/$ /_matrix/static/ last;
  137. {% endif %}
  138. }
  139. {% endmacro %}
  140. server {
  141. listen {{ 8080 if matrix_nginx_proxy_enabled else 80 }};
  142. listen [::]:{{ 8080 if matrix_nginx_proxy_enabled else 80 }};
  143. server_name {{ matrix_nginx_proxy_proxy_matrix_hostname }};
  144. server_tokens off;
  145. root /dev/null;
  146. {% if matrix_nginx_proxy_https_enabled %}
  147. location /.well-known/acme-challenge {
  148. {% if matrix_nginx_proxy_enabled %}
  149. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  150. resolver 127.0.0.11 valid=5s;
  151. set $backend "matrix-certbot:8080";
  152. proxy_pass http://$backend;
  153. {% else %}
  154. {# Generic configuration for use outside of our container setup #}
  155. proxy_pass http://127.0.0.1:{{ matrix_ssl_lets_encrypt_certbot_standalone_http_port }};
  156. {% endif %}
  157. }
  158. {% if matrix_nginx_proxy_proxy_matrix_nginx_status_enabled %}
  159. {{ render_nginx_status_location_block(matrix_nginx_proxy_proxy_matrix_nginx_status_allowed_addresses) }}
  160. {% endif %}
  161. location / {
  162. return 301 https://$http_host$request_uri;
  163. }
  164. {% else %}
  165. {{ render_vhost_directives() }}
  166. {% endif %}
  167. }
  168. {% if matrix_nginx_proxy_https_enabled %}
  169. server {
  170. listen {{ 8443 if matrix_nginx_proxy_enabled else 443 }} ssl http2;
  171. listen [::]:{{ 8443 if matrix_nginx_proxy_enabled else 443 }} ssl http2;
  172. server_name {{ matrix_nginx_proxy_proxy_matrix_hostname }};
  173. server_tokens off;
  174. root /dev/null;
  175. ssl_certificate {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/fullchain.pem;
  176. ssl_certificate_key {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/privkey.pem;
  177. ssl_protocols {{ matrix_nginx_proxy_ssl_protocols }};
  178. {% if matrix_nginx_proxy_ssl_ciphers != '' %}
  179. ssl_ciphers {{ matrix_nginx_proxy_ssl_ciphers }};
  180. {% endif %}
  181. ssl_prefer_server_ciphers {{ matrix_nginx_proxy_ssl_prefer_server_ciphers }};
  182. {% if matrix_nginx_proxy_ocsp_stapling_enabled %}
  183. ssl_stapling on;
  184. ssl_stapling_verify on;
  185. ssl_trusted_certificate {{ matrix_ssl_config_dir_path }}/live/{{ matrix_nginx_proxy_proxy_matrix_hostname }}/chain.pem;
  186. {% endif %}
  187. {% if matrix_nginx_proxy_ssl_session_tickets_off %}
  188. ssl_session_tickets off;
  189. {% endif %}
  190. ssl_session_cache {{ matrix_nginx_proxy_ssl_session_cache }};
  191. ssl_session_timeout {{ matrix_nginx_proxy_ssl_session_timeout }};
  192. {{ render_vhost_directives() }}
  193. }
  194. {% endif %}
  195. {% if matrix_nginx_proxy_proxy_matrix_federation_api_enabled %}
  196. {#
  197. This federation vhost is a little special.
  198. It serves federation over HTTP or HTTPS, depending on `matrix_nginx_proxy_https_enabled`.
  199. #}
  200. server {
  201. {% if matrix_nginx_proxy_https_enabled %}
  202. listen {{ matrix_nginx_proxy_proxy_matrix_federation_port }} ssl http2;
  203. listen [::]:{{ matrix_nginx_proxy_proxy_matrix_federation_port }} ssl http2;
  204. {% else %}
  205. listen {{ matrix_nginx_proxy_proxy_matrix_federation_port }};
  206. {% endif %}
  207. server_name {{ matrix_nginx_proxy_proxy_matrix_federation_hostname }};
  208. server_tokens off;
  209. root /dev/null;
  210. gzip on;
  211. gzip_types text/plain application/json;
  212. {% if matrix_nginx_proxy_https_enabled %}
  213. ssl_certificate {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_certificate }};
  214. ssl_certificate_key {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_certificate_key }};
  215. ssl_protocols {{ matrix_nginx_proxy_ssl_protocols }};
  216. {% if matrix_nginx_proxy_ssl_ciphers != '' %}
  217. ssl_ciphers {{ matrix_nginx_proxy_ssl_ciphers }};
  218. {% endif %}
  219. ssl_prefer_server_ciphers {{ matrix_nginx_proxy_ssl_prefer_server_ciphers }};
  220. {% if matrix_nginx_proxy_ocsp_stapling_enabled %}
  221. ssl_stapling on;
  222. ssl_stapling_verify on;
  223. ssl_trusted_certificate {{ matrix_nginx_proxy_proxy_matrix_federation_api_ssl_trusted_certificate }};
  224. {% endif %}
  225. {% if matrix_nginx_proxy_ssl_session_tickets_off %}
  226. ssl_session_tickets off;
  227. {% endif %}
  228. ssl_session_cache {{ matrix_nginx_proxy_ssl_session_cache }};
  229. ssl_session_timeout {{ matrix_nginx_proxy_ssl_session_timeout }};
  230. {% endif %}
  231. location / {
  232. {% if matrix_nginx_proxy_enabled %}
  233. {# Use the embedded DNS resolver in Docker containers to discover the service #}
  234. resolver 127.0.0.11 valid=5s;
  235. set $backend "{{ matrix_nginx_proxy_proxy_matrix_federation_api_addr_with_container }}";
  236. proxy_pass http://$backend;
  237. {% else %}
  238. {# Generic configuration for use outside of our container setup #}
  239. proxy_pass http://{{ matrix_nginx_proxy_proxy_matrix_federation_api_addr_sans_container }};
  240. {% endif %}
  241. proxy_set_header Host $host;
  242. proxy_set_header X-Forwarded-For {{ matrix_nginx_proxy_x_forwarded_for }};
  243. proxy_set_header X-Forwarded-Proto {{ matrix_nginx_proxy_x_forwarded_proto_value }};
  244. client_body_buffer_size 25M;
  245. client_max_body_size {{ matrix_nginx_proxy_proxy_matrix_federation_api_client_max_body_size_mb }}M;
  246. proxy_max_temp_file_size 0;
  247. }
  248. }
  249. {% endif %}