nginx.conf 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. {% macro nginx_headers(values) -%}
  2. add_header Referrer-Policy "no-referrer" always;
  3. add_header X-Content-Type-Options "nosniff" always;
  4. add_header X-Download-Options "noopen" always;
  5. add_header X-Frame-Options "SAMEORIGIN" always;
  6. add_header X-Permitted-Cross-Domain-Policies "none" always;
  7. add_header X-Robots-Tag "noindex, nofollow" always;
  8. add_header X-XSS-Protection "1; mode=block" always;
  9. {%- endmacro %}
  10. {% macro nginx_conf(values) -%}
  11. worker_processes auto;
  12. error_log /var/log/nginx/error.log warn;
  13. # Set to /tmp so it can run as non-root
  14. pid /tmp/nginx.pid;
  15. events {
  16. worker_connections 1024;
  17. }
  18. http {
  19. # Set to /tmp so it can run as non-root
  20. client_body_temp_path /tmp/client_temp;
  21. proxy_temp_path /tmp/proxy_temp_path;
  22. fastcgi_temp_path /tmp/fastcgi_temp;
  23. uwsgi_temp_path /tmp/uwsgi_temp;
  24. scgi_temp_path /tmp/scgi_temp;
  25. include mime.types;
  26. default_type application/octet-stream;
  27. types {
  28. text/javascript mjs;
  29. }
  30. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  31. '$status $body_bytes_sent "$http_referer" '
  32. '"$http_user_agent" "$http_x_forwarded_for"';
  33. access_log /var/log/nginx/access.log main;
  34. sendfile on;
  35. #tcp_nopush on;
  36. # Prevent nginx HTTP Server Detection
  37. server_tokens off;
  38. keepalive_timeout 65;
  39. # Set the `immutable` cache control options only for assets with a cache busting `v` argument
  40. map $arg_v $asset_immutable {
  41. "" "";
  42. default ", immutable";
  43. }
  44. #gzip on;
  45. upstream php-handler {
  46. server {{ "%s:%d" | format(values.consts.nextcloud_container_name, values.consts.internal_nextcloud_port) }};
  47. }
  48. server {
  49. {%- if values.network.certificate_id %}
  50. listen {{ values.network.web_port.port_number }} ssl;
  51. listen [::]:{{ values.network.web_port.port_number }} ssl;
  52. ssl_certificate {{ values.consts.ssl_cert_path }};
  53. ssl_certificate_key {{ values.consts.ssl_key_path }};
  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. # add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;
  61. add_header Strict-Transport-Security "max-age=15768000; includeSubDomains;" always;
  62. {%- else %}
  63. listen {{ values.network.web_port.port_number }};
  64. listen [::]:{{ values.network.web_port.port_number }};
  65. {%- endif %}
  66. absolute_redirect off;
  67. # set max upload size and increase upload timeout:
  68. client_max_body_size 10G;
  69. client_body_timeout 300s;
  70. fastcgi_buffers 64 4K;
  71. # The settings allows you to optimize the HTTP2 bandwidth.
  72. # See https://blog.cloudflare.com/delivering-http-2-upload-speed-improvements/
  73. # for tuning hints
  74. client_body_buffer_size 512k;
  75. # Enable gzip but do not remove ETag headers
  76. gzip on;
  77. gzip_vary on;
  78. gzip_comp_level 4;
  79. gzip_min_length 256;
  80. gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
  81. gzip_types application/atom+xml text/javascript 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;
  82. # Pagespeed is not supported by Nextcloud, so if your server is built
  83. # with the `ngx_pagespeed` module, uncomment this line to disable it.
  84. #pagespeed off;
  85. # HTTP response headers borrowed from Nextcloud `.htaccess`
  86. include /etc/nginx/nginx-headers.conf;
  87. # Remove X-Powered-By, which is an information leak
  88. fastcgi_hide_header X-Powered-By;
  89. # Path to the root of your installation
  90. root {{ values.consts.html_path }};
  91. # Specify how to handle directories -- specifying `/index.php$request_uri`
  92. # here as the fallback means that Nginx always exhibits the desired behaviour
  93. # when a client requests a path that corresponds to a directory that exists
  94. # on the server. In particular, if that directory contains an index.php file,
  95. # that file is correctly served; if it doesn't, then the request is passed to
  96. # the front-end controller. This consistent behaviour means that we don't need
  97. # to specify custom rules for certain paths (e.g. images and other assets,
  98. # `/updater`, `/ocs-provider`), and thus
  99. # `try_files $uri $uri/ /index.php$request_uri`
  100. # always provides the desired behaviour.
  101. index index.php index.html /index.php$request_uri;
  102. # Rule borrowed from `.htaccess` to handle Microsoft DAV clients
  103. location = / {
  104. if ( $http_user_agent ~ ^DavClnt ) {
  105. return 302 /remote.php/webdav/$is_args$args;
  106. }
  107. }
  108. location = /robots.txt {
  109. allow all;
  110. log_not_found off;
  111. access_log off;
  112. }
  113. {%- if values.nextcloud.notify_push.enabled %}
  114. # Forward Notify_Push "High Performance Backend" to it's own container
  115. location ^~ /push/ {
  116. # The trailing "/" is important!
  117. proxy_pass {{ "http://%s:%d/" | format(values.consts.notify_push_container_name, values.consts.internal_notify_push_port) }};
  118. proxy_http_version 1.1;
  119. proxy_set_header Upgrade $http_upgrade;
  120. proxy_set_header Connection "Upgrade";
  121. proxy_set_header Host $host;
  122. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  123. }
  124. {%- endif %}
  125. # Make a regex exception for `/.well-known` so that clients can still
  126. # access it despite the existence of the regex rule
  127. # `location ~ /(\.|autotest|...)` which would otherwise handle requests
  128. # for `/.well-known`.
  129. location ^~ /.well-known {
  130. # The rules in this block are an adaptation of the rules
  131. # in `.htaccess` that concern `/.well-known`.
  132. location = /.well-known/carddav { return 301 /remote.php/dav/; }
  133. location = /.well-known/caldav { return 301 /remote.php/dav/; }
  134. location = /.well-known/webfinger { return 301 /index.php$uri; }
  135. location = /.well-known/nodeinfo { return 301 /index.php$uri; }
  136. location /.well-known/acme-challenge { try_files $uri $uri/ =404; }
  137. location /.well-known/pki-validation { try_files $uri $uri/ =404; }
  138. # Let Nextcloud's API for `/.well-known` URIs handle all other
  139. # requests by passing them to the front-end controller.
  140. return 301 /index.php$request_uri;
  141. }
  142. # Rules borrowed from `.htaccess` to hide certain paths from clients
  143. location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/) { return 404; }
  144. location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) { return 404; }
  145. # Ensure this block, which passes PHP files to the PHP process, is above the blocks
  146. # which handle static assets (as seen below). If this block is not declared first,
  147. # then Nginx will encounter an infinite rewriting loop when it prepends `/index.php`
  148. # to the URI, resulting in a HTTP 500 error response.
  149. location ~ \.php(?:$|/) {
  150. # Required for legacy support
  151. rewrite ^/(?!index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|ocs-provider\/.+|.+\/richdocumentscode(_arm64)?\/proxy) /index.php$request_uri;
  152. fastcgi_split_path_info ^(.+?\.php)(/.*)$;
  153. set $path_info $fastcgi_path_info;
  154. try_files $fastcgi_script_name =404;
  155. include fastcgi_params;
  156. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  157. fastcgi_param PATH_INFO $path_info;
  158. #fastcgi_param HTTPS on;
  159. fastcgi_param modHeadersAvailable true; # Avoid sending the security headers twice
  160. fastcgi_param front_controller_active true; # Enable pretty urls
  161. fastcgi_pass php-handler;
  162. fastcgi_intercept_errors on;
  163. fastcgi_request_buffering off;
  164. fastcgi_max_temp_file_size 0;
  165. proxy_send_timeout 3600s;
  166. proxy_read_timeout 3600s;
  167. fastcgi_send_timeout 3600s;
  168. fastcgi_read_timeout 3600s;
  169. }
  170. # Serve static files
  171. location ~ \.(?:css|js|mjs|svg|gif|ico|jpg|png|webp|wasm|tflite|map|ogg|flac)$ {
  172. try_files $uri /index.php$request_uri;
  173. add_header Cache-Control "public, max-age=15778463$asset_immutable";
  174. include /etc/nginx/nginx-headers.conf;
  175. access_log off; # Optional: Don't log access to assets
  176. location ~ \.wasm$ {
  177. default_type application/wasm;
  178. }
  179. }
  180. location ~ \.(otf|woff2?)$ {
  181. try_files $uri /index.php$request_uri;
  182. expires 7d; # Cache-Control policy borrowed from `.htaccess`
  183. access_log off; # Optional: Don't log access to assets
  184. }
  185. # Rule borrowed from `.htaccess`
  186. location /remote {
  187. return 301 /remote.php$request_uri;
  188. }
  189. location / {
  190. try_files $uri $uri/ /index.php$request_uri;
  191. }
  192. }
  193. }
  194. {%- endmacro %}