feat(frontend): 生产改为同源 /api,前端挂到后端域名的根目录
- frontend/.env.production: VITE_API_BASE_URL 由 https://api.oms.gztbyc.com/api 改为 /api - tools/DEPLOY.md: 新增单站点 Nginx 配置(root=current、/api/ include upstream、SPA try_files 回退), 域名/CORS 章节改为同源说明 - tools/config/deploy.config.example.cjs: Nginx 前置说明同步为单站点 同源后不再需要 CORS,后端切换 v1/v2 端口时前端产物无需重新构建。 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
e2d7f04999
commit
122eb9ccc9
@ -1,9 +1,11 @@
|
||||
# 生产环境后端 API 地址
|
||||
# 前端站点 oms.gztbyc.com 与后端 API 站点 api.oms.gztbyc.com 不同源,这里直连 API 站点。
|
||||
# 生产环境后端 API 地址(单站点部署:前端静态与 API 同源)
|
||||
#
|
||||
# 注意:
|
||||
# 1. 必须带 /api 前缀(后端所有接口路由都在 /api 下,如 https://api.oms.gztbyc.com/api/auth/login)
|
||||
# 2. 必须与前端站点同协议(HTTPS 站点调 http:// 会被浏览器按「混合内容(Mixed Content)」拦截)
|
||||
# 3. 跨域请求由后端 CORS 中间件放行,白名单见 backend/app/middleware/cors.js
|
||||
# (默认已放行 http/https 的 oms.gztbyc.com,可用后端环境变量 CORS_ORIGINS 覆盖)
|
||||
VITE_API_BASE_URL=https://api.oms.gztbyc.com/api
|
||||
# 站点 oms.gztbyc.com 根目录指向前端 current,/api/ 由 Nginx 反代到 egg,
|
||||
# 因此这里用相对路径 /api,请求始终同源:
|
||||
# - 不产生跨域(无需 CORS 白名单)
|
||||
# - 不会出现 HTTPS 页面调 HTTP 接口的「混合内容(Mixed Content)」问题
|
||||
# - 后端切换 v1/v2 端口时前端无需重新构建
|
||||
#
|
||||
# 要求:Nginx 站点配置把 /api/ 反代到后端(可 include nginx-upstream.conf),
|
||||
# 参考 tools/DEPLOY.md「单站点部署」。
|
||||
VITE_API_BASE_URL=/api
|
||||
|
||||
@ -135,10 +135,41 @@ module.exports = {
|
||||
|
||||
健康检查失败时脚本自己停掉新实例并退出,旧实例继续服务(`current.md` / upstream 都未改动)。
|
||||
|
||||
### Nginx 前置(仅首次,站点 api.oms.gztbyc.com)
|
||||
### Nginx 配置(单站点部署,仅首次)
|
||||
|
||||
把反向代理 `location /` 里的 `proxy_pass http://127.0.0.1:6090;` 换成
|
||||
`include /www/wwwroot/api.oms.gztbyc.com/nginx-upstream.conf;`。
|
||||
前端静态与后端 API 同站点、同源:根路径出静态文件,`/api/` 反代到 egg。
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
server_name oms.gztbyc.com;
|
||||
|
||||
# 前端:站点根目录指向 current 软链(发布脚本原子切换)
|
||||
root /www/wwwroot/oms.gztbyc.com/current;
|
||||
index index.html;
|
||||
|
||||
# SPA 深链接回退:/order、/export-task 等直接刷新不再 404
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
# 后端:所有接口都在 /api 下;upstream 文件由发布脚本改写(v1/v2 交替),
|
||||
# 文件内容即 `proxy_pass http://127.0.0.1:<端口>;`
|
||||
location /api/ {
|
||||
include /www/wwwroot/api.oms.gztbyc.com/nginx-upstream.conf;
|
||||
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;
|
||||
proxy_read_timeout 300s; # 导出/批量等长请求
|
||||
client_max_body_size 50m; # 上传上限,按需调整
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
宝塔面板:站点 `oms.gztbyc.com` 的**网站目录**设为 `/www/wwwroot/oms.gztbyc.com/current`,
|
||||
并删掉指向 `127.0.0.1:6090` 的旧 `/api` 反代,改成上面的 `include`。
|
||||
|
||||
### 服务器端 `.env`(仅首次,之后由 shared-config 管理)
|
||||
|
||||
@ -155,7 +186,8 @@ v1/v2 共用)、可选 `OMS_CALLBACK_BASE_URL`。
|
||||
└─ current -> releases/v2026... # 软链接,Nginx 站点根目录指向它
|
||||
```
|
||||
|
||||
服务器前置(仅首次):`mkdir -p <basePath>/releases`,并把宝塔站点**网站目录**设为 `<basePath>/current`。
|
||||
服务器前置(仅首次):`mkdir -p <basePath>/releases`,并把宝塔站点**网站目录**设为 `<basePath>/current`,
|
||||
同时按上文「Nginx 配置(单站点部署)」把 `/api/` 反代到后端(`include nginx-upstream.conf`)。
|
||||
|
||||
`remote/deploy.sh` 流程:解压到新版本目录(不触碰线上目录)→ 校验 `index.html` 与引用的
|
||||
`/assets/*.js|css` → 合并历史版本中缺失的文件(旧 chunk 不 404)→ `ln -sfn` + `mv -Tf` 原子切换
|
||||
@ -174,19 +206,22 @@ v1/v2 共用)、可选 `OMS_CALLBACK_BASE_URL`。
|
||||
(根 `.gitignore` 已排除),只提交 `*.example.*` 模板。
|
||||
- `tools/temp/` 是本地发布包临时目录,已被忽略(保留最近 3 个包)。
|
||||
|
||||
## 前后端域名与跨域(CORS)
|
||||
## 单站点部署与同源请求
|
||||
|
||||
| 站点 | 说明 |
|
||||
| 路径 | 处理 |
|
||||
| --- | --- |
|
||||
| `oms.gztbyc.com` | 前端静态站点,由前端发布流程更新(Nginx 根目录指向 `current`) |
|
||||
| `api.oms.gztbyc.com` | 后端 API 站点,Nginx 反代到 egg 后端(端口由 `nginx-upstream.conf` 决定),路由前缀 `/api` |
|
||||
| `/` | 前端静态文件(Nginx `root` = `<basePath>/current`,`try_files` 回退 `index.html`) |
|
||||
| `/api/` | 反代到 egg(`include nginx-upstream.conf`,发布脚本切换 v1/v2 端口) |
|
||||
|
||||
- 前端 API 地址由构建期环境变量 `VITE_API_BASE_URL` 决定:
|
||||
- 生产 `${VITE_API_BASE_URL}=http://api.oms.gztbyc.com/api`(`frontend/.env.production`)
|
||||
- 开发 `VITE_API_BASE_URL=/api`(`frontend/.env.development`,由 vite dev server 代理到 `http://localhost:6090`)
|
||||
- 两个域名**不同源**,跨域请求由后端 `backend/app/middleware/cors.js` 按白名单放行
|
||||
(默认已含 http/https 的 `oms.gztbyc.com`,可用后端环境变量 `CORS_ORIGINS` 覆盖)。
|
||||
- 后端返回的图片/头像地址是相对路径(`/api/files/raw/...`),前端渲染时由
|
||||
`frontend/src/utils/media.ts` 的 `mediaUrl()` 补全为 API 站点绝对地址。
|
||||
- ⚠️ 若前端站点改用 HTTPS 访问,`VITE_API_BASE_URL` 必须同步改为 `https://api.oms.gztbyc.com/api`,
|
||||
否则浏览器会按「混合内容(Mixed Content)」拦截请求。
|
||||
- 前端构建期 `VITE_API_BASE_URL=/api`(`frontend/.env.production`)→ 浏览器请求始终同源
|
||||
- 同源后**不再需要 CORS**;`backend/app/middleware/cors.js` 保留以便老域名/其他站点访问,
|
||||
需要收紧时可把后端 `CORS_ORIGINS` 设为空或只留必要来源
|
||||
- 后端返回的图片/头像地址仍是相对路径(`/api/files/raw/...`),同源下
|
||||
`frontend/src/utils/media.ts` 的 `mediaUrl()` 原样使用
|
||||
- 后端切换 v1/v2 端口只需改写 `nginx-upstream.conf` 并 reload,**前端产物无需重新构建**
|
||||
- `api.oms.gztbyc.com` 现在只作为可选别名(外部回调、调试直连),指向同一后端即可;
|
||||
若不再使用可下线该站点
|
||||
- 站点改用 HTTPS 时,反向代理已带 `X-Forwarded-Proto`,前端构建产物无需改动
|
||||
|
||||
> 开发环境同理:`VITE_API_BASE_URL=/api` + vite dev server 把 `/api` 代理到 `http://localhost:6090`,
|
||||
> 与生产保持一致的相对路径。
|
||||
|
||||
@ -16,9 +16,11 @@
|
||||
* 每次发布复制到版本目录,覆盖包内同名文件
|
||||
* <deployRoot>/.deploy/ 发布工作目录:incoming 上传包、backups 备份、state 版本记录
|
||||
*
|
||||
* Nginx 前置(仅首次,站点 api.oms.gztbyc.com):
|
||||
* 把反向代理 `location /` 里的 `proxy_pass http://127.0.0.1:6090;` 换成
|
||||
* `include /www/wwwroot/api.oms.gztbyc.com/nginx-upstream.conf;`
|
||||
* Nginx 前置(仅首次,单站点 oms.gztbyc.com):
|
||||
* 站点根目录指向 <basePath>/current(前端静态),`location /` 用 try_files 回退 index.html;
|
||||
* `location /api/` 里 `include /www/wwwroot/api.oms.gztbyc.com/nginx-upstream.conf;`
|
||||
* (该文件内容为 `proxy_pass http://127.0.0.1:<端口>;`,由发布脚本切换 v1/v2 端口)。
|
||||
* 完整示例见 tools/DEPLOY.md。
|
||||
*/
|
||||
module.exports = {
|
||||
production: {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user