docs: 补充跨域处理方案(Nginx 同源反代 + Vite proxy)

- 02-系统架构: 部署架构改为 Nginx 同源反代模型,新增完整 Nginx 配置和 Vite proxy 配置
- 03-接口文档: 连接管理新增跨域处理小节(生产 Nginx / 开发 Vite proxy / CheckOrigin 策略)
This commit is contained in:
hhs
2026-06-13 14:05:57 +08:00
parent 0ef7aa657d
commit 0cafe94f3a
2 changed files with 90 additions and 6 deletions

View File

@@ -1085,3 +1085,25 @@ function reconnect(attempt: number) {
}
// attempt: 0 → 1s, 1 → 2s, 2 → 4s, 3 → 8s, ... 最大 30s
```
### 跨域处理
采用 **Nginx 同源反代**方案,前后端统一到同一域名,浏览器层面不存在跨域问题。
**生产环境**Nginx 将 `/`(前端)、`/api/*`REST`/ws`WebSocket统一反代到同一域名详见 `02-系统架构.md` 部署架构章节。
**开发环境**Vite 内置代理,前端 :5173 的 `/api``/ws` 请求代理到后端 :8080
```typescript
// frontend/vite.config.ts
server: {
proxy: {
"/api": "http://localhost:8080",
"/ws": { target: "ws://localhost:8080", ws: true },
},
},
```
**Go 后端 WebSocket CheckOrigin**:生产环境 Nginx 同源,`CheckOrigin` 可保持默认(拒绝跨域)。开发环境由 Vite proxy 转发,不存在跨域。因此后端无需配置 CORS 中间件,`CheckOrigin` 保持 gorilla/websocket 默认值即可。
> 如果未来需要支持第三方客户端直连(如移动端),再按需添加 CORS 中间件和 `CheckOrigin` 白名单。