nginx_nc.j2 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. ################################################################################
  2. # This file was generated by Ansible for {{ansible_fqdn}}
  3. # Do NOT modify this file by hand!
  4. ################################################################################
  5. {% if nextcloud_install_tls and nextcloud_tls_enforce %}
  6. server {
  7. listen 80;
  8. {% if nextcloud_ipv6 %}
  9. listen [::]:80;
  10. {% endif %}
  11. server_name {{ nextcloud_trusted_domain | ansible.utils.ipwrap | join(' ') }};
  12. # Prevent nginx HTTP Server Detection
  13. server_tokens off;
  14. # Enforce HTTPS
  15. return 301 https://$server_name$request_uri;
  16. }
  17. {% endif %}
  18. server {
  19. server_name {{ nextcloud_trusted_domain | ansible.utils.ipwrap | join(' ') }};
  20. {% if not nextcloud_install_tls or not nextcloud_tls_enforce %}
  21. listen 80;
  22. {% if nextcloud_ipv6 %}
  23. listen [::]:80;
  24. {% endif %}
  25. {% endif %}
  26. {% if nextcloud_install_tls %}
  27. listen 443 ssl http2;
  28. {% if nextcloud_ipv6 %}
  29. listen [::]:443 ssl http2;
  30. {% endif %}
  31. ssl_certificate {{ nextcloud_tls_cert_file }};
  32. ssl_certificate_key {{ nextcloud_tls_cert_key_file }};
  33. # Prevent nginx HTTP Server Detection
  34. server_tokens off;
  35. ssl_session_timeout 1d;
  36. ssl_session_cache shared:SSL:{{ nextcloud_tls_session_cache_size }};
  37. # ssl_session_tickets off;
  38. # OCSP stapling
  39. ssl_stapling on;
  40. ssl_stapling_verify on;
  41. # Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
  42. ssl_dhparam {{ nextcloud_tls_dhparam }};
  43. # Use Mozilla's guidelines for SSL/TLS settings
  44. # https://mozilla.github.io/server-side-tls/ssl-config-generator/
  45. {% if nextcloud_mozilla_modern_ssl_profile %}
  46. # modern configuration. tweak to your needs.
  47. ssl_protocols TLSv1.3;
  48. {% else %}
  49. # intermediate configuration. tweak to your needs.
  50. ssl_protocols TLSv1.2 TLSv1.3;
  51. ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
  52. {% endif %}
  53. ssl_prefer_server_ciphers off;
  54. # HSTS settings
  55. # WARNING: Only add the preload option once you read about
  56. # the consequences in https://hstspreload.org/. This option
  57. # will add the domain to a hardcoded list that is shipped
  58. # in all major browsers and getting removed from this list
  59. # could take several months.
  60. {% if nextcloud_hsts is string %}
  61. add_header Strict-Transport-Security "{{ nextcloud_hsts }}";
  62. {% endif %}
  63. {% endif %}
  64. # set max upload size and increase upload timeout:
  65. client_max_body_size {{ nextcloud_max_upload_size }};
  66. client_body_timeout 300s;
  67. fastcgi_buffers 64 4K;
  68. # Enable gzip but do not remove ETag headers
  69. gzip on;
  70. gzip_vary on;
  71. gzip_comp_level 4;
  72. gzip_min_length 256;
  73. gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
  74. gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/wasm application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;
  75. # Pagespeed is not supported by Nextcloud, so if your server is built
  76. # with the `ngx_pagespeed` module, uncomment this line to disable it.
  77. # pagespeed off;
  78. # HTTP response headers borrowed from Nextcloud `.htaccess`
  79. add_header Referrer-Policy "no-referrer" always;
  80. add_header X-Content-Type-Options "nosniff" always;
  81. add_header X-Download-Options "noopen" always;
  82. add_header X-Frame-Options "SAMEORIGIN" always;
  83. add_header X-Permitted-Cross-Domain-Policies "none" always;
  84. add_header X-Robots-Tag "none" always;
  85. add_header X-XSS-Protection "1; mode=block" always;
  86. # Remove X-Powered-By, which is an information leak
  87. fastcgi_hide_header X-Powered-By;
  88. # Path to the root of your installation
  89. root {{ nextcloud_webroot }};
  90. # Specify how to handle directories -- specifying `/index.php$request_uri`
  91. # here as the fallback means that Nginx always exhibits the desired behaviour
  92. # when a client requests a path that corresponds to a directory that exists
  93. # on the server. In particular, if that directory contains an index.php file,
  94. # that file is correctly served; if it doesn't, then the request is passed to
  95. # the front-end controller. This consistent behaviour means that we don't need
  96. # to specify custom rules for certain paths (e.g. images and other assets,
  97. # `/updater`, `/ocm-provider`, `/ocs-provider`), and thus
  98. # `try_files $uri $uri/ /index.php$request_uri`
  99. # always provides the desired behaviour.
  100. index index.php index.html /index.php$request_uri;
  101. # Rule borrowed from `.htaccess` to handle Microsoft DAV clients
  102. location = / {
  103. if ( $http_user_agent ~ ^DavClnt ) {
  104. return 302 /remote.php/webdav/$is_args$args;
  105. }
  106. }
  107. location = /robots.txt {
  108. allow all;
  109. log_not_found off;
  110. access_log off;
  111. }
  112. # Make a regex exception for `/.well-known` so that clients can still
  113. # access it despite the existence of the regex rule
  114. # `location ~ /(\.|autotest|...)` which would otherwise handle requests
  115. # for `/.well-known`.
  116. location ^~ /.well-known {
  117. # The rules in this block are an adaptation of the rules
  118. # in `.htaccess` that concern `/.well-known`.
  119. location = /.well-known/carddav { return 301 /remote.php/dav/; }
  120. location = /.well-known/caldav { return 301 /remote.php/dav/; }
  121. location /.well-known/acme-challenge { try_files $uri $uri/ =404; }
  122. location /.well-known/pki-validation { try_files $uri $uri/ =404; }
  123. # Let Nextcloud's API for `/.well-known` URIs handle all other
  124. # requests by passing them to the front-end controller.
  125. return 301 /index.php$request_uri;
  126. }
  127. # Rules borrowed from `.htaccess` to hide certain paths from clients
  128. location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/) { return 404; }
  129. location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) { return 404; }
  130. # Ensure this block, which passes PHP files to the PHP process, is above the blocks
  131. # which handle static assets (as seen below). If this block is not declared first,
  132. # then Nginx will encounter an infinite rewriting loop when it prepends `/index.php`
  133. # to the URI, resulting in a HTTP 500 error response.
  134. location ~ \.php(?:$|/) {
  135. # Required for legacy support
  136. rewrite ^/(?!index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+|.+\/richdocumentscode\/proxy) /index.php$request_uri;
  137. fastcgi_split_path_info ^(.+?\.php)(/.*)$;
  138. set $path_info $fastcgi_path_info;
  139. try_files $fastcgi_script_name =404;
  140. include fastcgi_params;
  141. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  142. fastcgi_param PATH_INFO $path_info;
  143. fastcgi_param HTTPS on;
  144. fastcgi_param modHeadersAvailable true; # Avoid sending the security headers twice
  145. fastcgi_param front_controller_active true; # Enable pretty urls
  146. fastcgi_pass php-handler;
  147. fastcgi_intercept_errors on;
  148. fastcgi_request_buffering off;
  149. }
  150. location ~ \.(?:css|js|svg|gif|png|jpg|ico|wasm|tflite)$ {
  151. try_files $uri /index.php$request_uri;
  152. expires 6M; # Cache-Control policy borrowed from `.htaccess`
  153. access_log off; # Optional: Don't log access to assets
  154. location ~ \.wasm$ {
  155. default_type application/wasm;
  156. }
  157. }
  158. location ~ \.woff2?$ {
  159. try_files $uri /index.php$request_uri;
  160. expires 7d; # Cache-Control policy borrowed from `.htaccess`
  161. access_log off; # Optional: Don't log access to assets
  162. }
  163. # Rule borrowed from `.htaccess`
  164. location /remote {
  165. return 301 /remote.php$request_uri;
  166. }
  167. location / {
  168. try_files $uri $uri/ /index.php$request_uri;
  169. }
  170. }