Skip to content

Self-hosting

Run a built Hamur app on your own machine: an image, a reverse proxy, a shared cache and a clean stop. The bun and node presets build a server you start with one command; this page puts it behind Docker and a proxy. The Docker page describes hamur build --docker; the production checklist lists what to set before the first visitor.

The image

Both Dockerfiles have two stages. The build stage installs with bun install --frozen-lockfile and runs hamur build. The run stage copies .output/ and nothing else, sets PORT=3000 and starts the server entry, so the image holds no source, no node_modules and no .env. Packages the build traced travel inside .output/server/node_modules (Traced externals).

hamur build --docker writes this file for you. These two differ from it in one line: the HEALTHCHECK asks the health endpoint instead of /. The run stage ends in a non-root user (bun in the Bun image, node in the Node image) that owns .output; the server listens on port 3000, so it needs no privilege.

Dockerfile
FROM oven/bun:1.4 AS build
WORKDIR /app
COPY package.json bun.lock* ./
RUN bun install --frozen-lockfile
COPY . .
RUN bun run hamur build --preset bun

FROM oven/bun:1.4-slim
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
COPY --from=build --chown=bun:bun /app/.output ./.output
USER bun
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 CMD ["bun","-e","fetch('http://127.0.0.1:'+(process.env.PORT||3000)+'/_hamur/health').then(r=>process.exit(r.status<400?0:1),()=>process.exit(1))"]
CMD ["bun", ".output/server/index.mjs"]
Dockerfile
FROM oven/bun:1.4 AS build
WORKDIR /app
COPY package.json bun.lock* ./
RUN bun install --frozen-lockfile
COPY . .
RUN bun run hamur build --preset node

FROM node:24-slim
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
COPY --from=build --chown=node:node /app/.output ./.output
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 CMD ["node","-e","fetch('http://127.0.0.1:'+(process.env.PORT||3000)+'/_hamur/health').then(r=>process.exit(r.status<400?0:1),()=>process.exit(1))"]
CMD ["node", ".output/server/index.mjs"]

Build either with docker build -f bun.Dockerfile -t my-app .. Copy your bun.lock next to package.json; the install is frozen. A test in the docs site compares both files with the generator, so they stay the same as hamur build --docker writes.

Run-time settings

The image has no secrets. Pass them when the container starts:

VariableWhat it sets
HAMUR_KEYthe app key of sessions, seal, csrf and signed cookies; make one with hamur create-key --env and give every instance the same value
PORT, HOSTwhere the server listens (3000 by default); see Bun
HAMUR_ORIGINthe public origin, replacing app.origin; see Configuration
REDIS_URLthe redis storage driver's address when the mount names no url
the names of app/env.tsread when the server starts, by #hamur/env/* and every env("NAME") of the config; see Environment

HAMUR_SSL_KEY and HAMUR_SSL_CERT make the server serve TLS itself, and HAMUR_UNIX_SOCKET makes it listen on a socket; a proxy in front usually makes both unnecessary (HTTPS and Unix sockets).

Behind a proxy

Set server.trustProxy when a proxy terminates TLS. Without it getRequestURL ignores X-Forwarded-Proto and X-Forwarded-Host, so absolute URLs, redirects and the origin check see the proxy's address and http. server.requestIP makes getRequestIP read the client address from X-Forwarded-For; trustedHops is the place of the entry from the end of the list (1, the default, is the last: the address your proxy saw). server.allowedHosts limits which forwarded hosts count. To pin the origin instead of trusting headers, set app.origin: no header changes it, even with trustProxy.

ts
import { defineConfig } from "hamur"
import { redis } from "hamur/storage"

export default defineConfig({
  framework: "react",
  preset: "node",
  server: {
    trustProxy: true,
    requestIP: { xForwardedFor: true, trustedHops: 1 },
    health: true,
  },
  storage: {
    cache: redis(),
  },
})

The proxy must pass the headers on, must not buffer streamed answers, and must let WebSocket upgrades through. Hamur already sends X-Accel-Buffering: no on event streams and createStream, which nginx honours; proxy_buffering off also covers a streamed page and an answer of your own.

nginx:

conf
map $http_upgrade $connection_upgrade {
  default upgrade;
  ""      "";
}

upstream hamur {
  server app:3000;
  keepalive 32;
}

server {
  listen 80;
  server_name example.com;

  location / {
    proxy_pass http://hamur;
    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;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_buffering off;
    proxy_read_timeout 1h;
  }
}

Caddy sends the forwarded headers itself and upgrades WebSockets on its own. flush_interval -1 writes each chunk at once, and health_uri takes an instance out of rotation while it answers 503:

text
example.com {
    reverse_proxy app:3000 {
        flush_interval -1
        health_uri /_hamur/health
    }
}

Health and the HEALTHCHECK

server.health: true answers GET and HEAD at /_hamur/health below app.baseURL with 200 {"status":"ok"}, 503 {"status":"starting"} before the server plugins have run and 503 {"status":"draining"} after the first SIGTERM. Authentication middleware cannot block it, and it writes no log line. The Dockerfiles above probe it, so Docker marks a container unhealthy while it starts or drains. With app.baseURL other than /, or a server.health.path of your own, change the URL in the HEALTHCHECK. See Health endpoint and server.health.

Stopping

The bun and node servers drain on the first SIGTERM or SIGINT: they stop accepting connections, give requests in flight up to 10 seconds, run the close server hook for up to 10 more and exit with code 0; a second signal exits with code 1 at once (Shutdown). Docker sends SIGTERM and kills the container after 10 seconds, which cuts the close hook short, so give it more time: stop_grace_period: 30s in Compose, or docker stop -t 30. The entry runs as the container's main process in every Dockerfile above, so it gets the signal itself.

Several instances

Memory storage and the default cache are per process. With more than one instance, mount a driver all of them reach under cache, so a page, a handler's answer and a tag revalidation are the same everywhere (Running several instances, Where entries live). The redis driver of the config above reads REDIS_URL. Keep other shared state, such as sessions you store server-side, in a driver too. A node build can also fork workers inside one container with node.cluster.

This Compose file runs the app, Redis and Caddy; every instance of app gets the same HAMUR_KEY:

yaml
services:
  app:
    build: .
    restart: unless-stopped
    environment:
      HAMUR_KEY: ${HAMUR_KEY:?set HAMUR_KEY}
      REDIS_URL: redis://cache:6379
    depends_on:
      - cache
    stop_grace_period: 30s

  cache:
    image: redis:7
    restart: unless-stopped
    volumes:
      - cache-data:/data

  proxy:
    image: caddy:2
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile:ro
      - caddy-data:/data
    depends_on:
      - app

volumes:
  cache-data:
  caddy-data:
sh
hamur create-key --env
docker compose up --build --scale app=2 -d
docker compose ps
docker compose stop app

Deploying a new build

Build a new image, start it beside the old one, wait for its health check and then stop the old one; the drain above lets the requests in flight finish. Browsers that kept a tab open across the deploy are covered by Deploys and version skew. After a deploy, hamur warm <origin> fills the page caches (CLI).

The Dockerfiles were written on 2026-11-30, changed on 2026-12-01 to run as a non-root user, and equal the generator's output with the one HEALTHCHECK change.