反向代理 + HTTPS

小匠实战约 5 分钟读完更新于 2026-06-19

用 nginx 把小匠包装成 https://ai.your-company.com:同源代理到 :3001 + Let's Encrypt 证书 + SSE 流式不缓冲。

4.1 安装 nginx

装宿主机(不进 docker),方便接管 80 / 443。

  1. Ubuntu:sudo apt install -y nginx;CentOS:sudo yum install -y nginx
  2. 启动:sudo systemctl enable --now nginx
  3. 浏览器开 http://<服务器 IP> 看到欢迎页 = 装好

提示: 前提:第 ② 章已部署、http://localhost:3001 可访问;域名已解析到本机公网 IP;80/443 入站放行。

4.2 申请 Let's Encrypt 证书

certbot 一键申请并自动写 nginx,90 天有效期,自动续期。

  1. 装 certbot:sudo apt install -y certbot python3-certbot-nginx(CentOS 用 yum 同名)
  2. 申请(换成你的域名和邮箱):sudo certbot --nginx -d ai.your-company.com --email it@your-company.com --agree-tos --no-eff-email
  3. 看到 Successfully received certificate = 拿到,certbot 已自动加 443 段

小贴士: 内网拿不到 Let's Encrypt:用公司自签 CA + 终端分发根证书,或买商业证书(GeoTrust / DigiCert)。详见 FAQ。

4.3 写完整 server 块(核心)

certbot 默认配置 SSE 会被缓冲,把 /etc/nginx/conf.d/jecloud-ai.conf 整段覆盖成下面这版:

server {
    listen 80;
    server_name ai.your-company.com;
    location /.well-known/acme-challenge/ { root /var/www/certbot; }
    location / { return 301 https://$host$request_uri; }
}

server {
    listen 443 ssl http2;
    server_name ai.your-company.com;

    ssl_certificate     /etc/letsencrypt/live/ai.your-company.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/ai.your-company.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;

    client_max_body_size 100M;

    location / {
        proxy_pass http://127.0.0.1:3001;
        proxy_http_version 1.1;

        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;

        # SSE 流式:必须关 nginx 缓冲
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }
}
  1. sudo nginx -t 通过 → sudo systemctl reload nginx
  2. https://ai.your-company.com 看到登录页 + 地址栏小锁 = 成

注意: 3 个值缺一不可:proxy_buffering off(SSE 流式)、client_max_body_size 100M(上传)、proxy_read_timeout 3600s(长连接)。少任意一个用户侧症状各异、难排查。

小贴士: 不是 WSS:小匠流式走 SSEtext/event-stream),不是 WebSocket。业务平台的实时通知是浏览器直连业务平台的 wss /jesocket,不经本机 nginx。

4.4 验证 SSE 真的没被缓冲

HTTPS 通不等于 SSE 没被缓冲,必须分别验证。

  1. 登录后发一句长提问,如「写 200 字产品介绍」
  2. 回答应一段段流式冒出来,不是憋很久整段蹦出
  3. DevTools → Network → 找 /api/chat/...Type 应为 eventsourceContent-Type: text/event-stream

提示: 整段才蹦出:99% 是 proxy_buffering off 漏了,回 4.3 复制完整版。实时通知收不到:与本机 nginx 无关,查管理端「服务器配置」的 wsUrl。

4.5 Caddy 备选

公司已有 Caddy 也行,比 nginx 还短:

ai.your-company.com {
    reverse_proxy localhost:3001 {
        flush_interval -1   # SSE 流式必加
    }
}

小贴士: IIS / 云厂商 SLB:必须关「响应缓冲」(IIS 的 ARR 默认 256KB 缓冲会卡 SSE)。证书 + 长连接 + 关缓冲三件套配齐即可。详见 FAQ。

4.6 证书续期演练

certbot 默认带定时器,但要演练一次确认能真续上。

  1. 试跑:sudo certbot renew --dry-run
  2. 看到 all simulated renewals succeeded = OK
  3. 查下次自动续期:sudo systemctl list-timers certbot.timer

注意: 到期前 30 天没续上要警觉:常见原因是 80 端口被防火墙堵了(certbot HTTP-01 校验依赖 80)。证书过期那刻浏览器报红、桌面端 OTA 全断。

相关

没解决你的问题?

直接问学院 AI 助教小帅 —— 他读过全部学院文档,会带着步骤和文档链接回答;也可以让工程师一对一讲解。