Clawdbot直连Qwen3-32B部署教程:Nginx反向代理+SSL证书配置完整步骤
1. 为什么需要这个部署方案
你是不是也遇到过这样的问题:本地跑着Qwen3-32B大模型,用Ollama启动后只能通过http://localhost:11434访问,但想让团队同事、客户或者外部用户也能安全稳定地使用?直接暴露内网端口风险高,没有域名访问不专业,HTTP协议在现代浏览器里还被标记为“不安全”。
Clawdbot作为轻量级Chat平台前端,本身不带服务端逻辑,它需要一个可靠的Web网关来对接后端大模型。而Qwen3-32B这类32B参数量级的模型,对推理服务的稳定性、并发响应和网络路径都有更高要求。单纯靠Ollama自带的API接口,既没法加HTTPS,也没法做负载均衡或访问控制。
本教程要带你从零完成一套生产可用的部署链路:
Ollama托管Qwen3-32B模型(已预装)
Clawdbot前端静态资源部署(无需Node.js运行时)
Nginx反向代理统一入口(80/443端口)
Let’s Encrypt自动签发SSL证书(免费、可信、自动续期)
全链路请求转发:https://chat.yourdomain.com→ Nginx →http://127.0.0.1:18789(Clawdbot网关)→http://127.0.0.1:11434(Ollama API)
整个过程不依赖Docker Compose编排,不修改Clawdbot源码,不重写任何前端逻辑——只靠配置和几条命令,就能把本地大模型变成一个可对外服务的AI聊天平台。
2. 环境准备与基础服务确认
2.1 前置条件检查
请确保你的服务器满足以下最低要求(推荐Ubuntu 22.04 LTS或CentOS 7+):
- 已安装Ollama(v0.3.0+),并成功拉取
qwen3:32b模型
ollama list | grep qwen3 # 应输出:qwen3:32b latest 25.4 GB ...- 已下载Clawdbot Web版静态包(官方Release中
clawdbot-web-v*.tar.gz) - 服务器有独立公网IP,且域名(如
chat.example.com)已解析到该IP - 防火墙放行80、443端口(UFW或firewalld)
- root或具备sudo权限的用户
注意:本教程不涉及模型量化、GPU驱动安装或Ollama服务配置优化。假设你已能通过
curl http://localhost:11434/api/chat调通Qwen3-32B基础API。
2.2 创建Clawdbot网关服务(18789端口)
Clawdbot本身是纯前端,但它需要一个中间层来转发请求到Ollama。我们用最轻量的方式实现:一个Python简易网关脚本(仅60行),不依赖Flask/FastAPI等框架。
新建文件/opt/clawdbot-gateway/app.py:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import json import requests from http.server import HTTPServer, BaseHTTPRequestHandler from urllib.parse import urlparse, parse_qs OLLAMA_URL = "http://127.0.0.1:11434" GATEWAY_PORT = 18789 class GatewayHandler(BaseHTTPRequestHandler): def do_POST(self): if self.path == "/api/chat": try: content_length = int(self.headers.get('Content-Length', 0)) post_data = self.rfile.read(content_length) data = json.loads(post_data) # 强制指定模型为 qwen3:32b(防止前端误传) data["model"] = "qwen3:32b" # 转发到Ollama resp = requests.post(f"{OLLAMA_URL}/api/chat", json=data, stream=True, timeout=300) self.send_response(resp.status_code) self.send_header("Content-Type", "application/json") self.end_headers() for chunk in resp.iter_content(chunk_size=8192): self.wfile.write(chunk) except Exception as e: self.send_error(500, f"Gateway error: {str(e)}") else: self.send_error(404) def do_OPTIONS(self): self.send_response(200) self.send_header("Access-Control-Allow-Origin", "*") self.send_header("Access-Control-Allow-Methods", "POST, OPTIONS") self.send_header("Access-Control-Allow-Headers", "Content-Type") self.end_headers() if __name__ == "__main__": server = HTTPServer(('127.0.0.1', GATEWAY_PORT), GatewayHandler) print(f"Clawdbot Gateway listening on http://127.0.0.1:{GATEWAY_PORT}") server.serve_forever()赋予执行权限并测试启动:
chmod +x /opt/clawdbot-gateway/app.py nohup python3 /opt/clawdbot-gateway/app.py > /var/log/clawdbot-gateway.log 2>&1 &验证网关是否就绪:
curl -X POST http://127.0.0.1:18789/api/chat \ -H "Content-Type: application/json" \ -d '{"messages":[{"role":"user","content":"你好"}]}'若返回流式JSON(含"message"字段),说明网关已连通Ollama。
2.3 部署Clawdbot前端静态文件
解压Clawdbot Web包到Nginx默认站点目录:
mkdir -p /var/www/clawdbot tar -xzf clawdbot-web-v*.tar.gz -C /var/www/clawdbot --strip-components=1确认关键文件存在:
ls -l /var/www/clawdbot/{index.html,main.js,config.json} # config.json 中需确保 apiEndpoint 字段为 "/api/chat"(相对路径,由Nginx重写)小技巧:Clawdbot的
config.json里不要写http://...绝对地址!所有API请求都走同域,由Nginx统一代理,这样才安全且免跨域。
3. Nginx反向代理配置详解
3.1 安装与基础配置
Ubuntu系统安装Nginx:
sudo apt update && sudo apt install nginx -y sudo systemctl enable nginx && sudo systemctl start nginx清空默认站点,创建Clawdbot专属配置:
rm /etc/nginx/sites-enabled/default nano /etc/nginx/sites-available/clawdbot填入以下内容(请将chat.yourdomain.com替换为你的真实域名):
server { listen 80; server_name chat.yourdomain.com; # 强制HTTPS跳转 return 301 https://$server_name$request_uri; } server { listen 443 ssl http2; server_name chat.yourdomain.com; # SSL证书路径(后续由certbot自动生成) ssl_certificate /etc/letsencrypt/live/chat.yourdomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/chat.yourdomain.com/privkey.pem; # SSL安全加固(采用Mozilla推荐配置) ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384; ssl_prefer_server_ciphers off; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; # 静态文件根目录 root /var/www/clawdbot; index index.html; # 处理前端路由(SPA模式) location / { try_files $uri $uri/ /index.html; } # 关键:API请求全部代理到本地网关 location /api/chat { 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_buffering off; proxy_cache off; proxy_redirect off; } # 可选:添加健康检查端点 location /healthz { return 200 "OK"; add_header Content-Type text/plain; } }启用配置并测试语法:
ln -sf /etc/nginx/sites-available/clawdbot /etc/nginx/sites-enabled/ nginx -t && sudo systemctl reload nginx此时访问http://chat.yourdomain.com应看到Clawdbot首页,但API仍会失败(因为还没配SSL证书)。
4. SSL证书自动申请与续期
4.1 使用Certbot获取Let’s Encrypt证书
安装Certbot(Ubuntu):
sudo apt install certbot python3-certbot-nginx -y首次申请证书(需确保域名DNS已生效,且80端口可被外网访问):
sudo certbot --nginx -d chat.yourdomain.com --non-interactive --agree-tos -m admin@yourdomain.comCertbot会自动:
- 修改Nginx配置,插入SSL证书路径
- 添加HTTP→HTTPS重定向规则
- 配置自动续期定时任务(
/etc/cron.d/certbot)
验证证书是否生效:
sudo certbot certificates # 查看输出中是否有 chat.yourdomain.com 的有效证书4.2 手动触发续期测试(推荐)
避免到期才发现问题,先手动跑一次续期流程:
sudo certbot renew --dry-run若输出Congratulations, all simulated renewals succeeded,说明自动续期机制正常。
安全提醒:证书默认90天有效期,Certbot会在到期前30天自动续期。无需人工干预,但建议每月登录服务器执行一次
sudo certbot renew --dry-run确认。
5. 全链路验证与常见问题排查
5.1 四层连通性逐级验证
按顺序执行以下检查,任一环节失败即定位问题所在:
| 层级 | 命令 | 预期结果 | 说明 |
|---|---|---|---|
| L1:Ollama是否就绪 | ollama ps | 显示qwen3:32b正在运行 | 检查模型是否加载 |
| L2:网关是否可达 | curl -v http://127.0.0.1:18789/healthz | 返回200 OK | 网关进程存活 |
| L3:Nginx静态服务 | curl -I http://chat.yourdomain.com | HTTP/2 200+content-type: text/html | DNS+HTTP通 |
| L4:HTTPS+API通路 | curl -k https://chat.yourdomain.com/api/chat -X POST -d '{}' | 返回400 Bad Request(非502/503) | 说明已抵达网关 |
注意:最后一步返回400是正常的!因为没传合法JSON,但只要不是502(Bad Gateway)或503(Service Unavailable),就证明Nginx→网关→Ollama整条链路已打通。
5.2 典型问题速查表
问题:页面打开空白,浏览器控制台报
Failed to fetch
检查/var/www/clawdbot/config.json中apiEndpoint是否为"/api/chat"(不能带协议和域名)
检查Nginx配置中location /api/chat块是否拼写正确,proxy_pass末尾不能加斜杠(应为18789,不是18789/)问题:发送消息后卡住,无响应
检查Ollama日志:journalctl -u ollama -n 50 --no-pager是否有OOM或CUDA错误
检查网关日志:tail -f /var/log/clawdbot-gateway.log是否有连接拒绝(Connection refused)
在网关服务器上执行:curl -v http://127.0.0.1:11434/api/chat -d '{"model":"qwen3:32b"}'直连Ollama测试问题:HTTPS访问提示证书不安全
执行sudo certbot certificates确认证书状态为EXPIRED还是VALID
检查Nginx配置中ssl_certificate路径是否与certbot certificates输出完全一致
清除浏览器缓存,或用curl -vk https://chat.yourdomain.com验证证书链问题:中文乱码、emoji显示异常
在Nginx配置的server块中添加:charset utf-8;
6. 进阶优化建议(可选)
6.1 提升Qwen3-32B响应速度
Ollama默认使用CPU推理,32B模型首token延迟可能达15秒以上。若服务器有NVIDIA GPU,建议:
- 安装NVIDIA Container Toolkit(即使不用Docker,Ollama也依赖它调用CUDA)
- 启动Ollama时指定GPU:
OLLAMA_NUM_GPU=1 ollama serve - 在Clawdbot网关中增加超时控制(修改
app.py中timeout=300为timeout=120),避免前端长时间等待
6.2 添加基础访问控制
不想完全公开?在Nginx中加入IP白名单(适用于内网或小范围试用):
# 在 server 块顶部添加 allow 192.168.1.0/24; # 允许内网 allow 203.0.113.42; # 允许特定公网IP deny all;6.3 日志分析与监控
将Clawdbot网关日志接入ELK或简单用goaccess分析:
# 实时统计API调用TOP10 sudo tail -f /var/log/clawdbot-gateway.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -107. 总结:你已掌握一套可落地的大模型Web化方案
回顾整个部署流程,你实际完成了三件关键事情:
🔹解耦了模型与前端:Qwen3-32B由Ollama专注推理,Clawdbot专注交互体验,网关层负责协议转换与安全管控;
🔹构建了生产级入口:Nginx不仅提供HTTPS,还承担了静态资源服务、反向代理、流式响应支持、健康检查等核心能力;
🔹实现了零成本安全合规:Let’s Encrypt证书让整个链路符合现代Web安全标准,且全自动续期,无需人工维护。
这套方案的优势在于极简、可控、可演进:
- 不依赖Kubernetes或复杂编排,单机即可承载中小团队日常使用;
- 所有组件(Ollama、Python网关、Nginx、Certbot)均为开源、轻量、社区活跃;
- 后续可无缝替换Clawdbot为其他前端(如Open WebUI),只需调整Nginx代理路径;
- 若需扩容,可将Ollama迁移到专用GPU服务器,Nginx保持不变。
现在,打开浏览器访问https://chat.yourdomain.com,输入一句“用Python写个快速排序”,亲眼看看Qwen3-32B如何在你自己的服务器上,以专业、安全、稳定的方式为你服务。
获取更多AI镜像
想探索更多AI镜像和应用场景?访问 CSDN星图镜像广场,提供丰富的预置镜像,覆盖大模型推理、图像生成、视频生成、模型微调等多个领域,支持一键部署。