On this page
  1. Try it locally (two minutes)
  2. Production
    1. Server configuration
    2. Docker Compose
    3. Reverse proxy (Nginx)
    4. Kubernetes
  3. Operations
    1. Health, shutdown, logs, metrics
    2. Backups
    3. Upgrades
    4. Scaling
  4. Security checklist

Self-hosting

Tunnex is one server process plus PostgreSQL. The official images are multi-arch (amd64, arm64):

Image What
registry.gitlab.com/homycoder/tunnex/server:<version> The server: edge, inboxes, API, console. Runs migrations on start.
registry.gitlab.com/homycoder/tunnex/client:<version> The client (tunnex start by default) for sidecars and CI.

Tags: the release version (26.10.14.41), the commit SHA, and latest. Pin a version in production.

Try it locally (two minutes)

examples/self-hosted/docker-compose.yml runs a server and PostgreSQL on your machine. *.lvh.me resolves to 127.0.0.1, so no DNS or TLS is needed.

docker compose -f examples/self-hosted/docker-compose.yml up -d
tunnex login --server ws://connect.lvh.me:8081       # sign up in the browser at http://connect.lvh.me:8081
tunnex inboxes create local-stripe                   # the server is remembered from login
curl -X POST http://local-stripe.lvh.me:8081/webhooks -d '{"type":"test"}'

Production

You need:

  • a host with a public IP and Docker (or a Kubernetes cluster);
  • a domain with DNS records for example.com, connect.example.com and *.example.com pointing to it;
  • a wildcard TLS certificate for *.example.com (Let's Encrypt with a DNS-01 challenge);
  • a reverse proxy that terminates TLS and streams (Nginx below; Caddy or Traefik work the same way);
  • PostgreSQL 14 or newer.

Server configuration

Start from tunnex_server/config.yaml.example; the full reference is Configuration. The minimum is the domain, the database and a secret key:

TUNNEX_DOMAIN=example.com
TUNNEX_DATABASE_URL=postgresql+asyncpg://tunnex:<password>@db:5432/tunnex
TUNNEX_SECRET_KEY=$(openssl rand -base64 48)   # keep it: it also encrypts inbox signing secrets

For a company-internal server, also set auth.anonymous_tunnels: false (every client signs in), consider auth.signup_enabled: false (create accounts, then turn signup off), the webhooks.* limits, and tcp.enabled: false if you do not need raw TCP.

Docker Compose

deploy/docker-compose.yml is the stack that runs tunnex.org: server (host networking, so the TCP port range binds directly), PostgreSQL bound to localhost, and optional Prometheus + Grafana (--profile monitoring). Copy it with deploy/server-config.yaml (change the domain) and an .env:

POSTGRES_PASSWORD=...
TUNNEX_SECRET_KEY=...
GRAFANA_PASSWORD=...
TUNNEX_IMAGE=registry.gitlab.com/homycoder/tunnex/server:<version>
docker compose up -d                                  # add --profile monitoring for Prometheus/Grafana
curl -fsS http://127.0.0.1:8081/readyz                # {"status":"ok",...} once migrations ran

deploy/bootstrap-server.sh automates all of it on a fresh Ubuntu host for tunnex.org (Nginx, certbot with Cloudflare DNS, ufw, cron backups); read it before reusing it for your domain.

Reverse proxy (Nginx)

The proxy must pass WebSockets, must not buffer (server-sent events, LLM streams and large bodies stream end to end), and must keep long-lived connections open:

server {
    listen 443 ssl http2;
    server_name connect.example.com *.example.com;
    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8081;
        proxy_http_version 1.1;
        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;   # map $http_upgrade $connection_upgrade {...}
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
        proxy_buffering off;
        proxy_request_buffering off;
        client_max_body_size 1g;
    }
}

The full file, with the landing page and HTTP→HTTPS redirects: deploy/nginx/tunnex.conf. Keep server.trusted_proxies to the proxy's address: the server replaces X-Forwarded-For from anyone else.

Open the firewall for 80/443 and, if TCP tunnels are on, the tcp.port_min–tcp.port_max range (TCP only).

Kubernetes

examples/kubernetes/tunnex-server.yaml: a ConfigMap, a single-replica Deployment with the Recreate strategy, liveness (/healthz) and readiness (/readyz) probes, and a Service. Route connect.<domain> and *.<domain> to it from your ingress; the controller must allow WebSockets and long streaming responses (for ingress-nginx: proxy-buffering: "off", proxy-read-timeout: "3600"). Raw TCP tunnels are disabled there: they need host ports or a load balancer per port.

Operations

Health, shutdown, logs, metrics

  • GET /healthz — the process is alive. GET /readyz — the database answers and the server is not draining; use it for load balancers and deploy checks.
  • SIGTERM drains: new requests get 503, in-flight requests have server.drain_seconds (10 s) to finish, then every client gets GOAWAY and reconnects to the next process by itself. Give the container at least 30 s (stop_grace_period, terminationGracePeriodSeconds).
  • Logs are JSON lines on stdout (logging.format: text for development) with correlation fields such as request_id, subdomain, inbox, event_id. Authorization headers, cookies, tokens and secrets in URLs are redacted.
  • Prometheus metrics on 127.0.0.1:9101/metrics: tunnels and listeners online, requests by status, policy denials, webhook events by provider and verification result, deliveries by kind and outcome, delivery latency. deploy/monitoring/ provisions Prometheus and a Grafana dashboard.

Backups

All state is in PostgreSQL: accounts, teams, inboxes (signing secrets encrypted with the secret key), stored events and their deliveries. Back up the database and keep TUNNEX_SECRET_KEY — without it inbox secrets cannot be decrypted and sessions are invalidated.

deploy/backup.sh writes a compressed pg_dump to backups/, checks the archive is readable, and deletes dumps older than 14 days (KEEP). The bootstrap installs it as a daily cron job, and the CI deploy runs it before every upgrade.

/opt/tunnex/backup.sh                                  # backups/tunnex-YYYYmmdd-HHMMSS.dump
docker compose stop server                             # restore
docker compose exec -T postgres pg_restore -U tunnex -d tunnex --clean --if-exists < backups/<file>.dump
docker compose start server

Database size is dominated by inbox events: about the event body size × events per day × retention days.

Upgrades

  1. Back up (backup.sh).
  2. Change the image tag and docker compose up -d (or roll the Deployment).
  3. Wait for /readyz. Migrations run at start in one transaction (PostgreSQL has transactional DDL): if one fails, the schema stays as it was and the server exits instead of becoming ready.

Migrations follow expand/contract: a release only adds tables and nullable columns, so the previous release keeps working against the new schema and a rollback is "deploy the previous tag". Destructive changes ship in a later release, after nothing reads the old columns. To roll back past a migration that changed data, restore the backup taken in step 1.

Clients. Servers keep accepting clients from earlier releases (see Protocol): the CI runs the previous PyPI release against every server build. Clients that are too old get a clear unsupported_protocol error telling them to upgrade.

Scaling

Tunnex runs as one server process (ADR 0003): live tunnels and listeners are kept in memory, so you scale up, not out. One process on loopback forwards a few hundred small requests per second per tunnel and multi-gigabit bulk transfers (benchmarks); inbox ingest is bounded by PostgreSQL inserts. Do not run two replicas against one database: each would only know its own clients.

Security checklist

  • A strong TUNNEX_SECRET_KEY from the environment, never in a file or image.
  • TLS on every public host; public_scheme: https (the default) — the server refuses to start without a secret.
  • Metrics and PostgreSQL bound to localhost or a private network.
  • trusted_proxies limited to your reverse proxy.
  • Anonymous tunnels and signup off for private servers once accounts exist.
  • Scoped, expiring tokens for CI (--scope listen --expires-in 90), revoked when a job is retired.

More: Security.

Edit this page on GitLab