Enabling Https with Nginx

Here we suggest you use Let’s Encrypt to get a certificate from a Certificate Authority (CA). If you use a paid ssl certificate from some authority, just skip the first step.

Generate SSL certificate

For users who use Let’s Encrypt, you can obtain a valid certificate via Certbot ACME client

On Ubuntu systems, the Certbot team maintains a PPA. Once you add it to your list of repositories all you’ll need to do is apt-get the following packages.

  1. sudo apt-get update
  2. sudo apt-get install software-properties-common
  3. sudo add-apt-repository ppa:certbot/certbot
  4. sudo apt-get update
  5. sudo apt-get install python-certbot-nginx

Certbot has an Nginx plugin, which is supported on many platforms, and automates both obtaining and installing certs:

  1. sudo certbot --nginx

Running this command will get a certificate for you and have Certbot edit your Nginx configuration automatically to serve it. If you’re feeling more conservative and would like to make the changes to your Nginx configuration by hand, you can use the certonly subcommand:

  1. sudo certbot --nginx certonly

To learn more about how to use Certbot you can read threir documentation.

If you’re using a custom CA to sign your SSL certificate, you have to enable certificate revocation list (CRL) in your certificate. Otherwise http syncing on Windows client may not work. See this thread for more information.

Enable SSL module of Nginx (optional)

If your Nginx does not support SSL, you need to recompile it, the commands are as follows:

  1. ./configure --with-http_stub_status_module --with-http_ssl_module
  2. make && make install

Modify Nginx configuration file

Assume you have configured nginx as Deploy-Seafile-with-nginx. To use https, you need to modify your nginx configuration file.

  1. server {
  2. listen 80;
  3. server_name seafile.example.com;
  4. rewrite ^ https://$http_host$request_uri? permanent; # force redirect http to https
  5. # Enables or disables emitting nginx version on error pages and in the "Server" response header field.
  6. server_tokens off;
  7. }
  8. server {
  9. listen 443;
  10. ssl on;
  11. ssl_certificate /etc/ssl/cacert.pem; # path to your cacert.pem
  12. ssl_certificate_key /etc/ssl/privkey.pem; # path to your privkey.pem
  13. server_name seafile.example.com;
  14. server_tokens off;
  15. # ......
  16. proxy_pass http://127.0.0.1:8000;
  17. proxy_set_header Host $host;
  18. proxy_set_header X-Real-IP $remote_addr;
  19. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  20. proxy_set_header X-Forwarded-Host $server_name;
  21. proxy_set_header X-Forwarded-Proto https;
  22. proxy_read_timeout 1200s;
  23. }

Sample configuration file

Generate DH params

(this takes some time)

  1. openssl dhparam 2048 > /etc/nginx/dhparam.pem

Here is the sample configuration file:

  1. server {
  2. listen 80;
  3. server_name seafile.example.com;
  4. rewrite ^ https://$http_host$request_uri? permanent; # force redirect http to https
  5. server_tokens off;
  6. }
  7. server {
  8. listen 443;
  9. ssl on;
  10. ssl_certificate /etc/ssl/cacert.pem; # path to your cacert.pem
  11. ssl_certificate_key /etc/ssl/privkey.pem; # path to your privkey.pem
  12. server_name seafile.example.com;
  13. ssl_session_timeout 5m;
  14. ssl_session_cache shared:SSL:5m;
  15. # Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
  16. ssl_dhparam /etc/nginx/dhparam.pem;
  17. # secure settings (A+ at SSL Labs ssltest at time of writing)
  18. # see https://wiki.mozilla.org/Security/Server_Side_TLS#Nginx
  19. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  20. ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-CAMELLIA256-SHA:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-SEED-SHA:DHE-RSA-CAMELLIA128-SHA:HIGH:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS';
  21. ssl_prefer_server_ciphers on;
  22. proxy_set_header X-Forwarded-For $remote_addr;
  23. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
  24. server_tokens off;
  25. location / {
  26. proxy_pass http://127.0.0.1:8000;
  27. proxy_set_header Host $host;
  28. proxy_set_header X-Real-IP $remote_addr;
  29. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  30. proxy_set_header X-Forwarded-Host $server_name;
  31. proxy_set_header X-Forwarded-Proto https;
  32. access_log /var/log/nginx/seahub.access.log;
  33. error_log /var/log/nginx/seahub.error.log;
  34. proxy_read_timeout 1200s;
  35. client_max_body_size 0;
  36. }
  37. # If you are using [FastCGI](http://en.wikipedia.org/wiki/FastCGI),
  38. # which is not recommended, you should use the following config for location `/`.
  39. #
  40. # location / {
  41. # fastcgi_pass 127.0.0.1:8000;
  42. # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  43. # fastcgi_param PATH_INFO $fastcgi_script_name;
  44. #
  45. # fastcgi_param SERVER_PROTOCOL $server_protocol;
  46. # fastcgi_param QUERY_STRING $query_string;
  47. # fastcgi_param REQUEST_METHOD $request_method;
  48. # fastcgi_param CONTENT_TYPE $content_type;
  49. # fastcgi_param CONTENT_LENGTH $content_length;
  50. # fastcgi_param SERVER_ADDR $server_addr;
  51. # fastcgi_param SERVER_PORT $server_port;
  52. # fastcgi_param SERVER_NAME $server_name;
  53. # fastcgi_param REMOTE_ADDR $remote_addr;
  54. # fastcgi_read_timeout 36000;
  55. #
  56. # client_max_body_size 0;
  57. #
  58. # access_log /var/log/nginx/seahub.access.log;
  59. # error_log /var/log/nginx/seahub.error.log;
  60. # }
  61. location /seafhttp {
  62. rewrite ^/seafhttp(.*)$ $1 break;
  63. proxy_pass http://127.0.0.1:8082;
  64. client_max_body_size 0;
  65. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  66. proxy_connect_timeout 36000s;
  67. proxy_read_timeout 36000s;
  68. proxy_send_timeout 36000s;
  69. send_timeout 36000s;
  70. }
  71. location /media {
  72. root /home/user/haiwen/seafile-server-latest/seahub;
  73. }
  74. }

Large file uploads

Tip for uploading very large files (> 4GB): By default Nginx will buffer large request body in temp file. After the body is completely received, Nginx will send the body to the upstream server (seaf-server in our case). But it seems when file size is very large, the buffering mechanism dosen’t work well. It may stop proxying the body in the middle. So if you want to support file upload larger for 4GB, we suggest you install Nginx version >= 1.8.0 and add the following options to Nginx config file:

  1. location /seafhttp {
  2. ... ...
  3. proxy_request_buffering off;
  4. }

If you have WebDAV enabled it is recommended to add the same:

  1. location /seafdav {
  2. ... ...
  3. proxy_request_buffering off;
  4. }

Reload Nginx

  1. nginx -s reload

Modify settings to use https

ccnet conf

Since you changed from http to https, you need to modify the value of SERVICE_URL in ccnet.conf. You can also modify SERVICE_URL via web UI in “System Admin->Settings”. (Warning: If you set the value both via Web UI and ccnet.conf, the setting via Web UI will take precedence.)

  1. SERVICE_URL = https://seafile.example.com

seahub_settings.py

You need to add a line in seahub_settings.py to set the value of FILE_SERVER_ROOT. You can also modify FILE_SERVER_ROOT via web UI in “System Admin->Settings”. (Warning: If you set the value both via Web UI and seahub_settings.py, the setting via Web UI will take precedence.)

  1. FILE_SERVER_ROOT = 'https://seafile.example.com/seafhttp'

Change Seafile config

Update the configuration of seafile fileserver is in the [fileserver] section of the file seafile.conf to local ip 127.0.0.1

  1. [fileserver]
  2. # bind address for fileserver
  3. # default to 0.0.0.0, if deployed without proxy: no access restriction
  4. # set to 127.0.0.1, if used with local proxy: only access by local
  5. host = 127.0.0.1

Start Seafile and Seahub

  1. ./seafile.sh start
  2. ./seahub.sh start # or "./seahub.sh start-fastcgi" if you're using fastcgi

Additional modern settings for nginx (optional)

Activate IPv6

Require IPv6 on server otherwise the server will not start! Also the AAAA dns record is required for IPv6 usage.

  1. listen 443;
  2. listen [::]:443;

Activate HTTP2

Activate HTTP2 for more performance. Only available for SSL and nginx version>=1.9.5. Simply add http2.

  1. listen 443 http2;
  2. listen [::]:443 http2;

Additional security settings for nginx (optional)

Force https on next visit

Add the HSTS header. If you already visited the https version the next time your browser will directly visit the https site and not the http one. Prevent man-in-the-middle-attacks:

  1. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

Obfuscate nginx version

Disable exact server version in header. Prevent scans for vulnerable server.
This should be added to every server block, as it shall obfuscate the version of nginx.

  1. server_tokens off;

Test your server

To check your configuration you can use the service from ssllabs: https://www.ssllabs.com/ssltest/index.html .