Files
mp-vue/docs/AI_PROXY_NGINX.md
赵忠林 ac9712819a fix(config): 更新AI代理配置为本地Ollama服务
- 将AI代理目标从远程服务器改为本地127.0.1:11434
- 更新Nginx配置中的代理地址和Host头部设置
- 修改AI_API_URL默认值指向本地Ollama服务
- 调整AI视图组件中的基础URL配置逻辑
- 更新环境变量示例文件中的默认API地址
- 修正Vite开发服务器代理配置指向本地服务
2026-02-28 01:42:18 +08:00

77 lines
2.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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/*` -> `http://127.0.0.1:11434/api/v1/*`
配合 `.env.development`
```bash
VITE_AI_API_URL=/ai-proxy
```
## 2) 生产环境Nginx 反代)
如果你的生产站点是 Nginx 托管静态文件,建议也加一条同源反代:
```nginx
location /ai-proxy/ {
proxy_pass http://127.0.0.1:11434/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://127.0.0.1:11434/;
proxy_http_version 1.1;
proxy_set_header Host 127.0.0.1;
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/*`