> For the complete documentation index, see [llms.txt](https://www.openclawbook.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.openclawbook.xyz/en/ch37-deployment/37.5-vps-deployment.md). Agents can also search and read this documentation through its [MCP server](https://www.openclawbook.xyz/~gitbook/mcp).

# 37.5 VPS Deployment

> **Generated by**: Claude Opus 4.6 (initial draft) / Claude Opus 5 (`v2026.3.9` revision) **Token usage**: Input \~230k tokens, Output \~5k tokens (this section) **Source baseline**: `v2026.3.9` (`v2026.3.8-170-g665f67726`)

***

VPS (Virtual Private Server) deployment is the solution for running OpenClaw Gateway on cloud servers, suitable for scenarios requiring persistent online availability, public internet accessibility, or multi-user sharing. This section combines OpenClaw's Docker image (see section 29.2) and reverse proxy support to provide a complete VPS deployment guide.

## 37.5.1 Deployment Architecture

A typical VPS deployment architecture looks like this:

```
                    ┌─────────────────────────────┐
     Internet        │         VPS (Ubuntu)         │
   ─────────────►   │                               │
   HTTPS :443       │   ┌─────────┐   ┌───────────┐ │
   ─────────────►   │   │  Caddy  │──►│  Gateway  │ │
   WSS :443         │   │ /Nginx  │   │  :18789   │ │
                     │   └─────────┘   └───────────┘ │
                     │        │                       │
                     │        ▼                       │
                     │   Auto TLS Certificate        │
                     │   (Let's Encrypt)              │
                     └─────────────────────────────┘
```

Core components:

1. **Reverse Proxy** (Caddy / Nginx / Traefik): Handles TLS termination, automatic certificate renewal, WebSocket upgrade
2. **OpenClaw Gateway**: Docker container running on loopback, handles business logic
3. **Firewall**: Only open ports 80/443

## 37.5.2 Docker Compose Production Configuration

Extending the Docker configuration from section 29.2 for production environment:

```yaml
# docker-compose.prod.yml
services:
  gateway:
    image: openclaw/openclaw:latest
    init: true                          # tini init process
    command: ["gateway", "--bind", "lan"]
    ports:
      - "127.0.0.1:18789:18789"        # Bind to loopback only
    environment:
      - OPENCLAW_GATEWAY_PASSWORD=${GATEWAY_PASSWORD}
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
    volumes:
      - openclaw-data:/home/node/.openclaw
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "node", "-e", "fetch('http://127.0.0.1:18789/health').then(r=>r.ok?process.exit(0):process.exit(1)).catch(()=>process.exit(1))"]
      interval: 30s
      timeout: 5s
      retries: 3

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

volumes:
  openclaw-data:
  caddy-data:
  caddy-config:
```

Note the Gateway port mapping as `127.0.0.1:18789:18789` — this ensures Gateway isn't directly exposed to the public internet, all external traffic must go through the reverse proxy.

## 37.5.3 Reverse Proxy Configuration

### Caddy (Recommended)

Caddy is the best reverse proxy for OpenClaw — it automatically manages TLS certificates and natively supports WebSocket proxying:

```
# Caddyfile
openclaw.example.com {
    reverse_proxy gateway:18789
}
```

Caddy automatically:

* Requests TLS certificate from Let's Encrypt
* Redirects HTTP to HTTPS
* Proxies WebSocket connections (including `Upgrade` and `Connection` headers)
* Auto-renews certificates before expiration

### Nginx

```nginx
server {
    listen 443 ssl http2;
    server_name openclaw.example.com;

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

    location / {
        proxy_pass http://127.0.0.1:18789;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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_read_timeout 86400;    # WebSocket long connections don't timeout
    }
}
```

> **Critical**: `proxy_read_timeout` must be set to a large value (like 86400 seconds), otherwise Nginx will close the WebSocket connection after the default 60 seconds.

## 37.5.4 Trusted Proxy Configuration

When Gateway runs behind a reverse proxy, all connections' `remoteAddress` is the proxy's IP (usually `127.0.0.1`), not the real client IP. This causes:

* All connections identified as "local connections", potentially bypassing authentication
* Device pairing unable to distinguish different clients
* Security audit logs recording incorrect IPs

OpenClaw solves this through `gateway.trustedProxies` configuration:

```json
{
  "gateway": {
    "auth": {
      "mode": "password",
      "password": "your-secure-password"
    },
    "trustedProxies": ["127.0.0.1", "::1"]
  }
}
```

When connections come from trusted proxies, Gateway reads `X-Forwarded-For` or `X-Real-IP` headers to get the real client IP:

```typescript
// src/gateway/net.ts (simplified)
export function resolveGatewayClientIp(params: {
  remoteAddr?: string;
  forwardedFor?: string;
  realIp?: string;
  trustedProxies?: string[];
}): string | undefined {
  const remote = normalizeIp(params.remoteAddr);
  // Only trust forwarded headers when connection comes from trusted proxy
  if (!isTrustedProxyAddress(remote, params.trustedProxies)) {
    return remote;  // Untrusted, use socket address directly
  }
  // Prefer X-Real-IP, then last IP from X-Forwarded-For
  return params.realIp || params.forwardedFor || remote;
}
```

OpenClaw's security audit system (`src/security/audit.ts`) issues warnings in these cases:

* `bind=loopback` + `controlUi.enabled=true` + `trustedProxies` empty: May indicate user running behind reverse proxy but forgot to configure trusted proxies
* Console UI exposed through reverse proxy without authentication enabled

## 37.5.5 TLS Configuration

Besides reverse proxy TLS termination, Gateway also supports native TLS:

```json
{
  "gateway": {
    "tls": {
      "enabled": true,
      "autoGenerate": true,
      "certPath": "/path/to/cert.pem",
      "keyPath": "/path/to/key.pem"
    }
  }
}
```

| Configuration  | Default | Description                                                 |
| -------------- | ------- | ----------------------------------------------------------- |
| `enabled`      | `false` | Enable TLS                                                  |
| `autoGenerate` | `true`  | Auto-generate self-signed certificate if missing            |
| `certPath`     | —       | PEM certificate file path                                   |
| `keyPath`      | —       | PEM private key file path                                   |
| `caPath`       | —       | CA certificate bundle (for mTLS or custom root certificate) |

**Recommended practice**: In VPS scenarios, use reverse proxy for TLS termination rather than Gateway native TLS. Reverse proxy (especially Caddy) has more mature certificate management and doesn't require managing certificate files in OpenClaw configuration.

## 37.5.6 Security Hardening Checklist

When deploying OpenClaw on VPS, it's recommended to execute the following security hardening measures:

| Measure                     | Configuration                        | Reason                                          |
| --------------------------- | ------------------------------------ | ----------------------------------------------- |
| Password authentication     | `auth.mode: "password"`              | Public internet exposure requires password      |
| Trusted proxies             | `trustedProxies: ["127.0.0.1"]`      | Correctly parse client IP                       |
| Gateway not bound to public | Port mapping `127.0.0.1:18789:18789` | Traffic must go through reverse proxy           |
| Firewall                    | Only open 80/443/22                  | Minimize attack surface                         |
| Docker non-root             | `USER node` (default)                | Container process doesn't run as root           |
| Data volume persistence     | `volumes: openclaw-data`             | Avoid data loss on container rebuild            |
| Auto-restart                | `restart: unless-stopped`            | Auto-recover on process crash                   |
| Health check                | Docker `healthcheck`                 | Container orchestration aware of service status |

## 37.5.7 Using Tailscale Instead of Public Exposure

If public access isn't needed, you can use Tailscale instead of reverse proxy approach, avoiding exposing Gateway to the public internet:

```yaml
# docker-compose.tailscale.yml
services:
  gateway:
    image: openclaw/openclaw:latest
    init: true
    command: ["gateway", "--bind", "tailnet"]
    environment:
      - OPENCLAW_GATEWAY_PASSWORD=${GATEWAY_PASSWORD}
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
    volumes:
      - openclaw-data:/home/node/.openclaw
    network_mode: host            # Need to access Tailscale network stack
    restart: unless-stopped
```

`--bind tailnet` binds Gateway to Tailscale's 100.64.x.x address, only devices within the tailnet can access — no firewall configuration needed, no TLS certificate management needed.

***

## Section Summary

1. **VPS deployment** recommended architecture: reverse proxy (Caddy) + Docker container (Gateway), reverse proxy handles TLS and WebSocket proxying, Gateway binds to loopback.
2. **Trusted proxies** (`trustedProxies`) is necessary configuration for reverse proxy scenarios, ensuring Gateway can get real client IP through `X-Forwarded-For` / `X-Real-IP`.
3. **Caddy** is the simplest choice — two lines of configuration complete TLS + WebSocket proxying; Nginx requires additional configuration for `proxy_read_timeout` and WebSocket upgrade headers.
4. **Security hardening** core: password authentication + loopback binding + trusted proxies + firewall.
5. If public access isn't needed, **Tailscale `--bind tailnet`** is the safest and simplest alternative.
