通过公网安全访问 OpenClaw
# 通过公网安全访问 OpenClaw
# 架构概览
[客户端 (浏览器/ClawPilot)]
│
│ HTTPS (TLS 1.2/1.3)
▼
[Nginx 反向代理 :<HTTPS端口>] ← Let's Encrypt 短证书
│
│ HTTP (内网)
▼
[OpenClaw Gateway :18789] ← 仅监听 127.0.0.1
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
核心思路:Nginx 做 TLS 终止和反向代理,Gateway 仅监听本地回环地址,外部只能通过 Nginx 代理访问。
# 一、安装 Nginx 反向代理
# 1.1 安装
apt-get update && apt-get install -y nginx
1
# 1.2 反向代理配置
创建 /etc/nginx/conf.d/openclaw-proxy.conf:
upstream openclaw_backend {
server 127.0.0.1:18789;
keepalive 8;
}
server {
listen <HTTPS端口> ssl;
listen [::]:<HTTPS端口> ssl;
server_name _;
# TLS 证书(后续会替换为 Let's Encrypt)
ssl_certificate /etc/nginx/ssl/openclaw.crt;
ssl_certificate_key /etc/nginx/ssl/openclaw.key;
# 安全加固
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers on;
ssl_ecdh_curve X25519:secp384r1;
ssl_session_tickets off;
# 安全响应头
add_header Strict-Transport-Security "max-age=86400; includeSubDomains" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always;
# WebSocket 支持(OpenClaw 必需)
location / {
proxy_pass http://openclaw_backend;
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_connect_timeout 10s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
注意:
<HTTPS端口>替换为你实际使用的端口号,需在云服务商安全组中放行。
# 1.3 主配置加固
编辑 /etc/nginx/nginx.conf 的 http 块,添加:
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers on;
ssl_ecdh_curve X25519:secp384r1;
server_tokens off;
limit_req_zone $binary_remote_addr zone=openclaw_ratelimit:10m rate=30r/s;
1
2
3
4
5
6
2
3
4
5
6
# 二、申请 Let's Encrypt IP 证书
# 2.1 背景
- Let's Encrypt 从 2026 年起支持为裸公网 IP 签发证书
- 仅支持 shortlived profile(6 天有效期)
- 验证方式:HTTP-01(端口 80)或 TLS-ALPN-01(端口 443)
# 2.2 云服务商安全组放行
在 ECS 安全组中添加入站规则:
| 协议 | 端口 | 授权对象 | 用途 |
|---|---|---|---|
| TCP | 80 | 0.0.0.0/0 | HTTP-01 验证 |
| TCP | 443 | 0.0.0.0/0 | TLS-ALPN-01 验证 |
| TCP | <HTTPS端口> | 0.0.0.0/0 | HTTPS 访问 |
# 2.3 安装 acme.sh
curl https://get.acme.sh | sh -s email=your_email@example.com
1
# 2.4 签发证书
~/.acme.sh/acme.sh --issue \
-d <你的公网IP> \
--standalone \
--httpport 80 \
--server letsencrypt \
--cert-profile shortlived \
--force
1
2
3
4
5
6
7
2
3
4
5
6
7
关键参数:
--cert-profile shortlived:指定 IP 证书专用的短有效期 profile--standalone:使用内置 HTTP 服务器完成 80 端口验证
# 2.5 安装证书到 Nginx
~/.acme.sh/acme.sh --install-cert -d <你的公网IP> --ecc \
--cert-file /etc/nginx/ssl/openclaw.crt \
--key-file /etc/nginx/ssl/openclaw.key \
--fullchain-file /etc/nginx/ssl/openclaw.crt \
--reloadcmd "/usr/sbin/nginx -t && systemctl reload nginx"
1
2
3
4
5
2
3
4
5
# 2.6 自动续期
acme.sh 安装时已自动创建 cron 任务:
1 7 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.openclaw" > /dev/null
1
证书 6 天有效期,每天检查一次,到期前自动续签并重载 Nginx。
# 三、OpenClaw Gateway 配置
# 3.1 允许公网 Origin 访问
编辑 ~/.openclaw/openclaw.json,在 gateway.controlUi 中添加:
{
"gateway": {
"controlUi": {
"allowInsecureAuth": true,
"allowedOrigins": ["https://<你的公网IP>:<HTTPS端口>"]
}
}
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
修改后重启 Gateway:
openclaw gateway restart
1
# 3.2 Gateway 认证
首次通过公网访问时,Control UI 会要求输入 Gateway Token。
Token 位于配置文件的 gateway.auth.token 字段,也可在 URL 中传递:
https://<你的公网IP>:<HTTPS端口>#token=<your-gateway-token>
1
# 四、安全措施总结
| 层级 | 措施 | 说明 |
|---|---|---|
| 传输层 | TLS 1.2/1.3 | 强密码套件,ECDHE 前向保密 |
| 证书 | Let's Encrypt 短证书 | 6 天有效期,减少泄露风险 |
| HTTP 安全头 | HSTS / X-Frame-Options / nosniff | 防止降级攻击、点击劫持、MIME 嗅探 |
| 速率限制 | 30 req/s, burst 50 | 防止暴力破解和 DDoS |
| Server 隐藏 | server_tokens off | 隐藏 Nginx 版本号 |
| Gateway 绑定 | 仅监听 127.0.0.1 | 外部只能通过 Nginx 代理访问 |
| Origin 白名单 | allowedOrigins 精确匹配 | 防止跨域请求伪造 |
# 五、验证清单
- [ ]
https://<你的公网IP>:<HTTPS端口>浏览器可正常访问(无证书警告) - [ ] ClawPilot App 可正常连接
- [ ] 证书到期前自动续期(检查 cron 日志)
- [ ] 云服务商安全组 80/443/
<HTTPS端口>端口已放行
# 六、常见问题
# 浏览器显示 "Not Secure"?
硬刷新(Ctrl+Shift+R)或使用无痕窗口。可能是旧自签名证书缓存。
# ClawPilot 报 "protocol mismatch"?
App 版本过旧,需更新到支持最新协议版本。
# 证书续期失败?
检查云服务商安全组 80 端口是否放行,acme.sh 需要通过 HTTP-01 验证。
# 访问报 "origin not allowed"?
gateway.controlUi.allowedOrigins 未包含公网 IP,需添加后重启 Gateway。
上次更新: 2026/06/06, 20:55:56