当前位置: 代码网 > it编程>数据库>Mysql > 使用Nginx为OpenClaw反向代理的实现

使用Nginx为OpenClaw反向代理的实现

2026年04月10日 Mysql 我要评论
一、前提与约束无域名:仅通过 https://<服务器ip> 访问,需使用自签名证书(浏览器会提示不安全,需手动信任或继续访问)。openclaw 要求:远程访问必须使用 wss://,不

一、前提与约束

  • 无域名:仅通过 https://<服务器ip> 访问,需使用自签名证书(浏览器会提示不安全,需手动信任或继续访问)。
  • openclaw 要求:远程访问必须使用 wss://,不能使用明文 ws://;网关需保持 bind: "loopback",仅由 nginx 代理访问。
  • 认证:在 openclaw 网关侧启用 token认证(也可选用 password,见后文)。

二、openclaw 网关配置(密码认证)

在配置 nginx 之前,先让 openclaw 使用密码认证,并允许来自 nginx 的 origin(即 https://<你的ip>)。

2.1 编辑~/.openclaw/openclaw.json

gateway 段中设置:

"gateway": {
  "port": 18789,
  "mode": "local",
  "bind": "loopback",
  "controlui": {
    "allowedorigins": [
      "http://127.0.0.1:18789",
      "http://localhost:18789",
      "https://your_server_ip"
    ]
  },
  "auth": {
    "mode": "token",
    "password": "你的访问令牌"
  },
  "trustedproxies": ["127.0.0.1"]
}

说明

  • your_server_ip 替换为服务器公网 ip(例如 x.x.x.x)。若通过 https://ip:443 访问,origin 一般为 https://your_server_ip,无需写端口。
  • trustedproxies 为可选。当 nginx 与网关同机部署时,填 ["127.0.0.1"] 可让网关信任来自本机(nginx)的代理头,消除日志中的 "proxy headers detected from untrusted address" 警告。
  • auth.mode: "password" 表示使用密码认证;客户端需在请求中携带 authorization: bearer <密码> 或通过 url 参数传递(见下文访问方式)。

2.2 重启网关

systemctl --user restart openclaw-gateway.service
# 或
openclaw gateway start

三、安装 nginx

1. alibaba cloud linux 3 / centos 8 / rhel 8

# 1. 更新系统包
sudo dnf update -y# 
2. 添加 nginx 官方稳定源
sudo tee /etc/yum.repos.d/nginx.repo <<-'eof'
[nginx-stable]
name=nginx stable repo
baseurl=https://nginx.org/packages/rhel/8/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
eof
# 3. 安装
 nginxsudo dnf install -y nginx
# 4. 验证安装
nginx -v

2. centos 7 / alibaba cloud linux 2

# 1. 更新系统包
sudo yum update -y
# 2. 添加 nginx 官方稳定源
sudo tee /etc/yum.repos.d/nginx.repo <<-'eof'
[nginx-stable]
name=nginx stable repo
baseurl=https://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
eof
# 3. 安装 
nginxsudo yum install -y nginx
# 4. 验证安装
nginx -v

3. ubuntu / debian

# 1. 更新系统包
sudo apt update && sudo apt upgrade -y
# 2. 安装
 nginxsudo apt install -y nginx
# 3. 验证安装
nginx -v

四、生成自签名证书(仅 ip、无域名)

无域名时无法使用 let's encrypt,需自签名证书。将 your_server_ip 换成实际 ip(如 x.x.x.x):

sudo mkdir -p /etc/nginx/ssl
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout /etc/nginx/ssl/openclaw.key \
  -out /etc/nginx/ssl/openclaw.crt \
  -subj "/cn=openclaw" \
  -addext "subjectaltname=ip:your_server_ip"

注意:若使用公网 ip,请填公网 ip;若仅内网访问,填内网 ip。浏览器访问会提示"连接不是私密连接",需手动接受或继续访问方可使用。

五、nginx 反向代理配置

5.1 创建站点配置

找到nginx配置目录,我的系统是 alibaba cloud linux 3,nginx 配置目录是

/etc/nginx/conf.d/

创建配置文件

vim /etc/nginx/conf.d/openclaw.conf

写入以下内容(将 your_server_ip 替换为实际 ip):

# openclaw 反向代理(仅 ip、无域名,https + wss)
# 上游:openclaw 网关默认端口 18789,仅监听本机
upstream openclaw_backend {
    server 127.0.0.1:18789;
    keepalive 64;
}
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    # 无域名时用 ip 或 default_server
    server_name your_server_ip;
    ssl_certificate     /etc/nginx/ssl/openclaw.crt;
    ssl_certificate_key /etc/nginx/ssl/openclaw.key;
    ssl_protocols       tlsv1.2 tlsv1.3;
    ssl_ciphers         high:!anull:!md5;
    # 日志(可选)
    access_log /var/log/nginx/openclaw_access.log;
    error_log  /var/log/nginx/openclaw_error.log;
    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_read_timeout 86400s;
        proxy_send_timeout 86400s;
    }
}

要点

  • upgradeconnection "upgrade" 用于 websocket 升级,否则 control ui 与 wss 无法正常工作。
  • proxy_read_timeout / proxy_send_timeout 拉长,避免长连接被 nginx 断开。

5.2 启用站点并检查配置

nginx -t   # 测试配置是否正确
systemctl restart nginx  # 重启生效

六、防火墙与安全组

  • 放行 443(https)。
  • 若希望 http 自动跳转 https,可加 80 并做 redirect(见下节)。
  • 不要对公网开放 18789,网关仅对本机监听。

七、访问方式

7.1 浏览器访问 control ui

  • 地址:https://your_server_ip(例如 https://x.x.x.x)。
  • 首次会提示证书不受信任,选择"高级" → "继续前往"即可(自签名证书正常现象)。

密码认证

  • 若使用 token 模式:https://your_server_ip/#token=你的访问令牌

八、故障排查

8.1 反代后首次访问的典型问题与处理顺序

通过 nginx 反代首次用浏览器访问 https://your_server_ip/#token=... 时,常会依次遇到下面三类情况,按顺序处理即可。

1)提示 "origin not allowed"

  • 原因:浏览器请求的 origin 为 https://your_server_ip(经 nginx 访问时的来源),未在网关白名单中。
  • 处理:在 ~/.openclaw/openclaw.json 的 gateway.controlui.allowedorigins 中增加一项 https://your_server_ip(将 your_server_ip 换成实际 ip,无需写 :443)。保存后重启网关。

2)日志出现 "proxy headers detected from untrusted address"

  • 原因:网关收到了 nginx 转发的 x-forwarded-for 等代理头,但未把 nginx 所在地址视为可信代理。
  • 处理:在 gateway 中增加 "trustedproxies": ["127.0.0.1"](nginx 与网关同机时)。保存后重启网关。此步为可选,仅用于消除警告并改善"经代理的本地连接"识别。

3)连接失败:pairing required / not-paired(websocket code 1008)

  • 原因:经反代访问时,浏览器设备会被视为"远程设备",需在网关上完成一次设备配对后才能建立 websocket。与 nginx 配置无关。
  • 处理:在运行网关的服务器上执行:
openclaw devices list          # 查看 pending 列表中的 request id
openclaw devices approve <request id>   # 批准该设备,例如 approve <request id>
  • 批准后,刷新浏览器并再次访问 https://your_server_ip/#token=你的令牌,control ui 应能正常连接。地址栏从 / 跳转到 /chat?session=main 是前端路由行为,属正常。

8.2 常见现象速查表

现象可能原因处理
502 bad gateway网关未运行或 nginx 连不上 18789在服务器上执行 curl -i http://127.0.0.1:18789/,应为 200/3xx;执行 openclaw gateway start 或重启 systemd 服务。
origin not allowed未把 https://your_server_ip 加入 allowedorigins在 gateway.controlui.allowedorigins 中增加 https://your_server_ip,重启网关。
proxy headers from untrusted address(日志)未配置可信代理可选:在 gateway 中增加 "trustedproxies": ["127.0.0.1"],重启网关。
pairing required / not-paired(1008)浏览器设备尚未在网关上配对在服务器执行 openclaw devices list,再 openclaw devices approve <request id>。
能打开页面但"与网关断开"websocket 未正确代理或 origin 未放行确认 nginx 配置中有 proxy_http_version 1.1、upgrade、connection "upgrade";确认 gateway.controlui.allowedorigins 包含 https://your_server_ip。
认证失败密码/ token 错误或未传确认 openclaw.json 中 auth.mode 与 auth.password/auth.token 一致;url 用 ?token= 或 ?password=(视版本而定),不要用 #。
security error (ws 明文)直接访问了 ws:// 或 http://ip:18789必须通过 nginx 的 https:// / wss:// 访问,不要直连 18789。

九、参考链接

到此这篇关于使用nginx为openclaw反向代理的实现的文章就介绍到这了,更多相关nginx openclaw反向代理内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com