- 将 nginx 反向代理路径从 /ollama-proxy 修改为 /proxy - 更新 .env.development 和 .env.example 中的 VITE_OLLAMA_API_URL 配置 - 修改 src/config/setting.ts 中的默认代理路径配置 - 更新 vite.config.ts 中的代理配置路径映射 - 优化代码格式化和多行语句的换行处理 - 调整打包配置中的文件命名逻辑格式
77 lines
2.0 KiB
Markdown
77 lines
2.0 KiB
Markdown
# AI /ai-proxy Nginx 反代示例
|
||
|
||
前端页面 `src/views/ai/index.vue` 默认在开发环境使用 `AI_API_URL=/ai-proxy`,通过同源反代解决浏览器 CORS。
|
||
|
||
## 1) Vite 开发环境
|
||
|
||
项目已在 `vite.config.ts` 配置(默认目标可通过 `AI_PROXY_TARGET` 调整):
|
||
|
||
- `/ai-proxy/*` -> `https://ai-api.websoft.top/api/v1/*`
|
||
|
||
配合 `.env.development`:
|
||
|
||
```bash
|
||
VITE_AI_API_URL=/ai-proxy
|
||
```
|
||
|
||
## 2) 生产环境(Nginx 反代)
|
||
|
||
如果你的生产站点是 Nginx 托管静态文件,建议也加一条同源反代:
|
||
|
||
```nginx
|
||
location /ai-proxy/ {
|
||
proxy_pass https://ai-api.websoft.top/api/v1/;
|
||
proxy_http_version 1.1;
|
||
proxy_set_header Host ai-api.websoft.top;
|
||
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/流式输出建议关闭缓存与缓冲
|
||
proxy_buffering off;
|
||
proxy_cache off;
|
||
|
||
# 如果你的 AI 网关开启了鉴权(401 Not authenticated),可以在反代层固定注入:
|
||
# proxy_set_header Authorization "Bearer YOUR_AI_API_KEY";
|
||
}
|
||
```
|
||
|
||
然后把生产环境的 `VITE_AI_API_URL` 配置为:
|
||
|
||
```bash
|
||
VITE_AI_API_URL=/ai-proxy
|
||
```
|
||
|
||
## 2.1) Ollama 原生接口(Nginx 反代)
|
||
|
||
如果你要直接用原生 Ollama(`http://<host>:11434`),生产环境同样建议走同源反代(避免 CORS + https 混合内容):
|
||
|
||
```nginx
|
||
location /proxy/ {
|
||
proxy_pass http://47.119.165.234:11434/;
|
||
proxy_http_version 1.1;
|
||
proxy_set_header Host 47.119.165.234;
|
||
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;
|
||
}
|
||
```
|
||
|
||
然后把 `VITE_OLLAMA_API_URL` 配置为:
|
||
|
||
```bash
|
||
VITE_OLLAMA_API_URL=/proxy
|
||
```
|
||
|
||
## 3) 关于 API Key
|
||
|
||
不建议把 Key 放在浏览器里。
|
||
|
||
推荐做法:
|
||
|
||
- Key 放在你自己的后端(或 Nginx)里统一注入 / 鉴权;
|
||
- 前端只请求同源 `/ai-proxy/*`。
|