# nginx site for Home Space. Guide: https://home-space.app/docs/nginx/ # # sudo cp homespace.nginx.conf /etc/nginx/conf.d/homespace.conf # # replace files.example.com with your domain, then: # sudo nginx -t && sudo systemctl reload nginx # # Server settings in .env: HOMESPACE_PUBLIC_URL=https://files.example.com # HOMESPACE_TRUST_FORWARDED_FOR=true # If nginx.conf already defines "map $http_upgrade $connection_upgrade", delete the one below. upstream homespace { server 127.0.0.1:8080; # HOMESPACE_PORT keepalive 16; } # Access log line without the query string: downloads and the websocket carry a one-time # ticket there (?ticket=...), which has no business in a log file. log_format homespace_no_query '$remote_addr - $remote_user [$time_local] ' '"$request_method $uri $server_protocol" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"'; # WebSocket support for /api/v1/ws map $http_upgrade $connection_upgrade { default upgrade; '' close; } # Everything goes over TLS: plain http is only ever redirected. # Do not proxy Home Space on port 80: passwords, tokens and files would travel unencrypted. server { listen 80; listen [::]:80; server_name files.example.com; return 301 https://$host$request_uri; } server { listen 443 ssl; listen [::]:443 ssl; http2 on; # nginx older than 1.25.1: drop this line, use "listen 443 ssl http2;" server_name files.example.com; ssl_certificate /etc/letsencrypt/live/files.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/files.example.com/privkey.pem; # Browsers that have been here once refuse plain http for a year. The other security # headers (Content-Security-Policy and so on) come from the app itself. add_header Strict-Transport-Security "max-age=31536000" always; proxy_http_version 1.1; # The app compares the browser's Origin with Host, so pass it on unchanged. On a port # other than 443 use $http_host, which keeps the port. proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; client_max_body_size 1m; location / { proxy_pass http://homespace; } # Uploads: clients send files up to HOMESPACE_UPLOAD_CHUNK_BYTES (from /api/v1/capabilities) # in one request, larger ones in chunks through /api/v1/uploads. location /api/v1/files { proxy_pass http://homespace; client_max_body_size 64m; # above HOMESPACE_UPLOAD_CHUNK_BYTES proxy_request_buffering off; client_body_timeout 300s; proxy_send_timeout 300s; proxy_read_timeout 300s; } # Resumable uploads: files over HOMESPACE_UPLOAD_CHUNK_BYTES arrive as chunks of that size. location /api/v1/uploads { proxy_pass http://homespace; client_max_body_size 64m; # above HOMESPACE_UPLOAD_CHUNK_BYTES proxy_request_buffering off; client_body_timeout 300s; proxy_send_timeout 300s; proxy_read_timeout 300s; # the last chunk also stores the file } # File content: GET downloads, PUT replaces an existing file. This regex location wins # over the /api/v1/files prefix above, so it needs the upload limits too. location ~ ^/api/v1/files/.+/content$ { proxy_pass http://homespace; access_log /var/log/nginx/access.log homespace_no_query; proxy_buffering off; # don't spool big downloads to nginx's temp dir client_max_body_size 64m; # above HOMESPACE_UPLOAD_CHUNK_BYTES proxy_request_buffering off; client_body_timeout 300s; proxy_send_timeout 300s; proxy_read_timeout 300s; } # Long-lived WebSocket for live updates location /api/v1/ws { proxy_pass http://homespace; access_log /var/log/nginx/access.log homespace_no_query; proxy_read_timeout 3600s; } }