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.

200 lines
9.8 KiB

  1. # This file is included strictly as an example of how Nginx can be configured
  2. # to work with CryptPad. This example WILL NOT WORK AS IS. For best results,
  3. # compare the sections of this configuration file against a working CryptPad
  4. # installation (http server by the Nodejs process). If you are using CryptPad
  5. # in production, contact sales@cryptpad.fr
  6. server {
  7. listen 443 ssl http2;
  8. # CryptPad serves static assets over these two domains.
  9. # `main_domain` is what users will enter in their address bar.
  10. # Privileged computation such as key management is handled in this scope
  11. # UI content is loaded via the `sandbox_domain`.
  12. # "Content Security Policy" headers prevent content loaded via the sandbox
  13. # from accessing privileged information.
  14. # These variables must be different to take advantage of CryptPad's sandboxing techniques.
  15. # In the event of an XSS vulnerability in CryptPad's front-end code
  16. # this will limit the amount of information accessible to attackers.
  17. set $main_domain "your-main-domain.com";
  18. set $sandbox_domain "your-sandbox-domain.com";
  19. # CryptPad's dynamic content (websocket traffic and encrypted blobs)
  20. # can be served over separate domains. Using dedicated domains (or subdomains)
  21. # for these purposes allows you to move them to a separate machine at a later date
  22. # if you find that a single machine cannot handle all of your users.
  23. # If you don't use dedicated domains, this can be the same as $main_domain
  24. # If you do, they'll be added as exceptions to any rules which block connections to remote domains.
  25. set $api_domain "api.your-main-domain.com";
  26. set $files_domain "files.your-main-domain.com";
  27. # nginx doesn't let you set server_name via variables, so you need to hardcode your domains here
  28. server_name your-main-domain.com your-sandbox-domain.com;
  29. # You'll need to Set the path to your certificates and keys here
  30. ssl_certificate /home/cryptpad/.acme.sh/your-main-domain.com/fullchain.cer;
  31. ssl_certificate_key /home/cryptpad/.acme.sh/your-main-domain.com/your-main-domain.com.key;
  32. ssl_trusted_certificate /home/cryptpad/.acme.sh/your-main-domain.com/ca.cer;
  33. # diffie-hellman parameters are used to negotiate keys for your session
  34. # generate strong parameters using the following command
  35. ssl_dhparam /etc/nginx/dhparam.pem; # openssl dhparam -out /etc/nginx/dhparam.pem 4096
  36. # Speeds things up a little bit when resuming a session
  37. ssl_session_timeout 5m;
  38. ssl_session_cache shared:SSL:5m;
  39. # You'll need nginx 1.13.0 or better to support TLSv1.3
  40. ssl_protocols TLSv1.2 TLSv1.3;
  41. # https://cipherli.st/
  42. ssl_ciphers EECDH+AESGCM:EDH+AESGCM;
  43. ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0
  44. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
  45. add_header X-XSS-Protection "1; mode=block";
  46. add_header X-Content-Type-Options nosniff;
  47. add_header Access-Control-Allow-Origin "*";
  48. # add_header X-Frame-Options "SAMEORIGIN";
  49. # Insert the path to your CryptPad repository root here
  50. root /home/cryptpad/cryptpad;
  51. index index.html;
  52. error_page 404 /customize.dist/404.html;
  53. # any static assets loaded with "ver=" in their URL will be cached for a year
  54. if ($args ~ ver=) {
  55. set $cacheControl max-age=31536000;
  56. }
  57. # Will not set any header if it is emptystring
  58. add_header Cache-Control $cacheControl;
  59. # CSS can be dynamically set inline, loaded from the same domain, or from $main_domain
  60. set $styleSrc "'unsafe-inline' 'self' ${main_domain}";
  61. # connect-src restricts URLs which can be loaded using script interfaces
  62. set $connectSrc "'self' https://${main_domain} ${main_domain} https://${api_domain} blob: wss://${api_domain} ${api_domain} ${files_domain}";
  63. # fonts can be loaded from data-URLs or the main domain
  64. set $fontSrc "'self' data: ${main_domain}";
  65. # images can be loaded from anywhere, though we'd like to deprecate this as it allows the use of images for tracking
  66. set $imgSrc "'self' data: * blob: ${main_domain}";
  67. # frame-src specifies valid sources for nested browsing contexts.
  68. # this prevents loading any iframes from anywhere other than the sandbox domain
  69. set $frameSrc "'self' ${sandbox_domain} blob:";
  70. # specifies valid sources for loading media using video or audio
  71. set $mediaSrc "'self' data: * blob: ${main_domain}";
  72. # defines valid sources for webworkers and nested browser contexts
  73. # deprecated in favour of worker-src and frame-src
  74. set $childSrc "https://${main_domain}";
  75. # specifies valid sources for Worker, SharedWorker, or ServiceWorker scripts.
  76. # supercedes child-src but is unfortunately not yet universally supported.
  77. set $workerSrc "https://${main_domain}";
  78. # script-src specifies valid sources for javascript, including inline handlers
  79. set $scriptSrc "'self' resource: ${main_domain}";
  80. set $unsafe 0;
  81. # the following assets are loaded via the sandbox domain
  82. # they unfortunately still require exceptions to the sandboxing to work correctly.
  83. if ($uri = "/pad/inner.html") { set $unsafe 1; }
  84. if ($uri = "/sheet/inner.html") { set $unsafe 1; }
  85. if ($uri ~ ^\/common\/onlyoffice\/.*\/index\.html.*$) { set $unsafe 1; }
  86. # everything except the sandbox domain is a privileged scope, as they might be used to handle keys
  87. if ($host != $sandbox_domain) { set $unsafe 0; }
  88. # privileged contexts allow a few more rights than unprivileged contexts, though limits are still applied
  89. if ($unsafe) {
  90. set $scriptSrc "'self' 'unsafe-eval' 'unsafe-inline' resource: ${main_domain}";
  91. }
  92. # Finally, set all the rules you composed above.
  93. add_header Content-Security-Policy "default-src 'none'; child-src $childSrc; worker-src $workerSrc; media-src $mediaSrc; style-src $styleSrc; script-src $scriptSrc; connect-src $connectSrc; font-src $fontSrc; img-src $imgSrc; frame-src $frameSrc;";
  94. # The nodejs process can handle all traffic whether accessed over websocket or as static assets
  95. # We prefer to serve static content from nginx directly and to leave the API server to handle
  96. # the dynamic content that only it can manage. This is primarily an optimization
  97. location ^~ /cryptpad_websocket {
  98. proxy_pass http://localhost:3000;
  99. proxy_set_header X-Real-IP $remote_addr;
  100. proxy_set_header Host $host;
  101. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  102. # WebSocket support (nginx 1.4)
  103. proxy_http_version 1.1;
  104. proxy_set_header Upgrade $http_upgrade;
  105. proxy_set_header Connection upgrade;
  106. }
  107. location ^~ /customize.dist/ {
  108. # This is needed in order to prevent infinite recursion between /customize/ and the root
  109. }
  110. # try to load customizeable content via /customize/ and fall back to the default content
  111. # located at /customize.dist/
  112. # This is what allows you to override behaviour.
  113. location ^~ /customize/ {
  114. rewrite ^/customize/(.*)$ $1 break;
  115. try_files /customize/$uri /customize.dist/$uri;
  116. }
  117. # /api/config is loaded once per page load and is used to retrieve
  118. # the caching variable which is applied to every other resource
  119. # which is loaded during that session.
  120. location = /api/config {
  121. proxy_pass http://localhost:3000;
  122. proxy_set_header X-Real-IP $remote_addr;
  123. proxy_set_header Host $host;
  124. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  125. }
  126. # encrypted blobs are immutable and are thus cached for a year
  127. location ^~ /blob/ {
  128. if ($request_method = 'OPTIONS') {
  129. add_header 'Access-Control-Allow-Origin' '*';
  130. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  131. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
  132. add_header 'Access-Control-Max-Age' 1728000;
  133. add_header 'Content-Type' 'application/octet-stream; charset=utf-8';
  134. add_header 'Content-Length' 0;
  135. return 204;
  136. }
  137. add_header Cache-Control max-age=31536000;
  138. add_header 'Access-Control-Allow-Origin' '*';
  139. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  140. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
  141. add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
  142. try_files $uri =404;
  143. }
  144. # the "block-store" serves encrypted payloads containing users' drive keys
  145. # these payloads are unlocked via login credentials. They are mutable
  146. # and are thus never cached. They're small enough that it doesn't matter, in any case.
  147. location ^~ /block/ {
  148. add_header Cache-Control max-age=0;
  149. try_files $uri =404;
  150. }
  151. # This block provides an alternative means of loading content
  152. # otherwise only served via websocket. This is solely for debugging purposes,
  153. # and is thus not allowed by default.
  154. #location ^~ /datastore/ {
  155. #add_header Cache-Control max-age=0;
  156. #try_files $uri =404;
  157. #}
  158. # The nodejs server has some built-in forwarding rules to prevent
  159. # URLs like /pad from resulting in a 404. This simply adds a trailing slash
  160. # to a variety of applications.
  161. location ~ ^/(register|login|settings|user|pad|drive|poll|slide|code|whiteboard|file|media|profile|contacts|todo|filepicker|debug|kanban|sheet|support|admin|notifications|teams)$ {
  162. rewrite ^(.*)$ $1/ redirect;
  163. }
  164. # Finally, serve anything the above exceptions don't govern.
  165. try_files /www/$uri /www/$uri/index.html /customize/$uri;
  166. }