Guides

Reverse proxy with nginx

nginx terminates HTTPS and passes requests to the server on 127.0.0.1:8080. The example below is tuned for Home Space: large uploads, long downloads and the live-update websocket.

This guide assumes nginx runs on the same machine as the containers from Install the server, and that files.example.com already points at that machine.

HTTPS on port 443, not http on port 80

It may be tempting to skip the certificate and serve Home Space on port 80. Don't: over plain http, sign-in passwords, tokens and files travel unencrypted and anyone on the network path can read or alter them. The configuration below serves Home Space on 443 with a certificate and uses port 80 only to redirect browsers to HTTPS and for certificate renewal. The server also requires an https:// address in HOMESPACE_PUBLIC_URL and for single sign-on.

1. Install nginx and certbot

Debian / Ubuntu
sudo apt install nginx certbot python3-certbot-nginx
Fedora / RHEL
sudo dnf install nginx certbot python3-certbot-nginx
sudo systemctl enable --now nginx
# SELinux: allow nginx to connect to the server on 127.0.0.1:8080
sudo setsebool -P httpd_can_network_connect 1

2. Get a certificate

Ask Let's Encrypt for a certificate before adding the site, so the configuration below finds it. certbot renews it on its own and reloads nginx afterwards:

shell
sudo certbot certonly --nginx -d files.example.com \
  --deploy-hook "systemctl reload nginx"

The certificate lands in /etc/letsencrypt/live/files.example.com/. If you use another source, such as a wildcard certificate or Cloudflare origin certificates, point ssl_certificate and ssl_certificate_key at those files instead.

3. Add the site

Download homespace.nginx.conf, put in your domain and enable it:

shell
sudo curl -fsSL https://home-space.app/docs/homespace.nginx.conf \
  -o /etc/nginx/conf.d/homespace.conf
sudo sed -i 's/files\.example\.com/your.domain.com/g' /etc/nginx/conf.d/homespace.conf
sudo nginx -t && sudo systemctl reload nginx

On Debian and Ubuntu, also remove the default site if it claims port 80 for every name: sudo rm /etc/nginx/sites-enabled/default.

homespace.nginx.conf
# 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;
    }
}

4. Tell the server it is behind a proxy

Two settings in /opt/homespace/.env belong to the proxy. The .env from the install guide already has both:

.env
HOMESPACE_PUBLIC_URL=https://files.example.com
HOMESPACE_TRUST_FORWARDED_FOR=true

After changing .env, recreate the server: sudo docker compose up -d --force-recreate server.

What the configuration does

PartWhy
client_max_body_size 64m on upload pathsFiles up to HOMESPACE_UPLOAD_CHUNK_BYTES (32 MiB) go up in one request, bigger ones in chunks of that size. The limit must stay above it; everything else is held to 1 MB.
proxy_request_buffering off, proxy_buffering offUploads and downloads stream through instead of being spooled to nginx's temporary directory.
/api/v1/ws with Upgrade headersThe websocket that pushes changes to every open client. The one-hour read timeout keeps idle connections open.
Host $hostThe server checks the browser's Origin against Host. On a port other than 443 use $http_host, which keeps the port.
homespace_no_query log formatDownloads and the websocket carry a one-time ticket in the query string; it stays out of the access log.
Strict-Transport-SecurityBrowsers that have visited once only use HTTPS. The server sets the other security headers itself.

Different port or upload size

Check it

shell
curl -fsS https://files.example.com/api/v1/health

Then open https://files.example.com and sign in. If uploads fail with 413 Request Entity Too Large, a client_max_body_size is below the chunk size. If files do not update live in other open windows, check the /api/v1/ws location.