Self-hosting

HTTPS and a domain

Putting Hubstruct behind a reverse proxy with HTTPS, with examples for Caddy and nginx, and what the proxy must allow.

To reach Hubstruct from outside your home network, or just at a nice address with HTTPS, put a reverse proxy in front of the app. Only the console service needs to be behind it: the app passes the browser's API calls on to the API itself.

What the proxy must allow

  • Long-lived responses. The live stream (/api/events) stays open for as long as a page is, sending a ping every so often. The proxy must not buffer it or time it out quickly.
  • Large uploads. Uploads are streamed as the request body; a proxy's default body limit (1 MB in nginx) would refuse most videos.
  • Range requests, so videos can seek. Proxies pass these through by default.
  • The original Host, and X-Forwarded-Proto: https, so links and Google's redirect address are built correctly.

Caddy

Caddy gets and renews certificates by itself, and handles streaming responses without extra settings:

Caddyfile
photos.example.com {
  reverse_proxy localhost:4173
  request_body {
    max_size 10GB
  }
}

nginx

hubstruct.conf
server {
  listen 443 ssl http2;
  server_name photos.example.com;

  ssl_certificate     /etc/letsencrypt/live/photos.example.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/photos.example.com/privkey.pem;

  client_max_body_size 10G;

  location / {
    proxy_pass http://127.0.0.1:4173;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    # The live stream and uploads: no buffering, long timeouts.
    proxy_buffering off;
    proxy_request_buffering off;
    proxy_read_timeout 1h;
  }
}

Keep the other ports private

Once the proxy is in place, publish the app only on 127.0.0.1 so it can't be reached around the proxy, and remove the other published ports you don't use. In the Compose file:

YAML
  console:
    ports:
      - "127.0.0.1:4173:3000"

Then tell the app it's behind a proxy, so it takes each browser's address from the X-Forwarded-For header the proxy adds, rather than the proxy's own (sign-in attempts are limited per address):

YAML
  console:
    environment:
      NUXT_TRUST_PROXY: "true"

See Architecture for what each port is for.

Google sign-in at the new address

If you use Google sign-in, its redirect address changes with the domain. Add the new redirect URI, shown in Settings → Sign-in at the new address, to your OAuth client in Google Cloud. See Users and sign-in.

Calling the API from another site

Pages served from another origin that call Hubstruct's API with a session cookie need to be allowed, for CORS and the API's cross-site request check. List their origins, comma-separated, in APP_ORIGINS:

.env
APP_ORIGINS=https://dashboard.example.com

Scripts using an API key don't need this, nor does the app itself.