一台 VPS 上运行多个 OpenClaw 实例—— 不同的人、不同的项目、不同的配置,完全隔离。
为什么需要多 Gateway?
单 Gateway:
所有用户共享同一 AI 上下文
安全隔离性差(任何用户可以影响其他用户)
不适合不同信任级别的用户
多 Gateway(推荐):
每个实例完全独立(文件系统/会话/API Key)
用户之间零泄露
每个实例可以用不同模型/配置
可以分别为不同项目计费
目录结构规划
/home/
alice/
.openclaw/
openclaw.json <- Alice 的配置(端口 18789)
workspace/ <- Alice 的 workspace
bob/
.openclaw/
openclaw.json <- Bob 的配置(端口 18790)
workspace/ <- Bob 的 workspace
多实例配置示例
Alice 的配置(端口 18789):
json
{
"gateway": {
"port": 18789,
"bind": "127.0.0.1",
"auth": { "mode": "token", "token": "alice-token" }
},
"providers": {
"anthropic": { "apiKey": "sk-ant-alice-key" }
},
"channels": {
"telegram": {
"enabled": true,
"botToken": "alice-telegram-token"
}
}
}Bob 的配置(端口 18790):
json
{
"gateway": {
"port": 18790,
"bind": "127.0.0.1",
"auth": { "mode": "token", "token": "bob-token" }
},
"providers": {
"anthropic": { "apiKey": "sk-ant-bob-key" }
},
"channels": {
"telegram": {
"enabled": true,
"botToken": "bob-telegram-token"
}
}
}systemd 管理多实例
ini
# /etc/systemd/system/openclaw-alice.service
[Unit]
Description=OpenClaw Gateway - Alice
After=network.target
[Service]
Type=simple
User=alice
ExecStart=/usr/local/bin/openclaw gateway start
Restart=always
RestartSec=10
Environment=HOME=/home/alice
[Install]
WantedBy=multi-user.target启用并启动:
bash
sudo systemctl enable openclaw-alice openclaw-bob
sudo systemctl start openclaw-alice openclaw-bob
sudo systemctl status openclaw-aliceNginx 虚拟主机(每人一个域名)
nginx
server {
listen 443 ssl;
server_name ai-alice.example.com;
ssl_certificate /etc/letsencrypt/live/ai-alice.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ai-alice.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";
underscores_in_headers on;
}
}
server {
listen 443 ssl;
server_name ai-bob.example.com;
ssl_certificate /etc/letsencrypt/live/ai-bob.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ai-bob.example.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:18790;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
underscores_in_headers on;
}
}防火墙配置
只对外暴露 Nginx 端口,内部端口不开放:
bash
ufw allow 22 # SSH
ufw allow 80 # HTTP
ufw allow 443 # HTTPS
# 18789/18790 不对外开放
ufw enable单机多 Gateway vs 多机方案对比
| 维度 | 单机多 Gateway | 多机方案 |
|---|---|---|
| 成本 | 低(共享服务器) | 高(每人一台 VPS) |
| 隔离性 | 进程级隔离 | 物理隔离(更强) |
| 故障影响 | 宕机影响所有人 | 互不影响 |
| 管理复杂度 | 低 | 高 |
| 推荐场景 | 家庭/小团队 | 企业/高安全需求 |
Docker 隔离方案(推荐)
yaml
# docker-compose.yml
services:
openclaw-alice:
image: openclaw/openclaw:latest
ports:
- "127.0.0.1:18789:18789"
volumes:
- ./alice-config:/home/openclaw/.openclaw
openclaw-bob:
image: openclaw/openclaw:latest
ports:
- "127.0.0.1:18790:18789"
volumes:
- ./bob-config:/home/openclaw/.openclaw容器之间完全文件系统隔离,安全性更强。
来源:OpenClaw 官方文档 - docs.openclaw.ai/gateway/multiple-gateways