fix(config): 更新AI API配置以支持生产环境HTTPS访问

- 将生产环境AI API URL从 http://127.0.0.1:11434/api/v1 修改为 https://ai-api.websoft.top/api/v1
- 在ai组件中移除硬编码的本地地址,统一使用HTTPS反向代理URL
- 添加重要注释说明浏览器生产环境中不应使用127.0.0.1地址
- 更新示例环境变量配置,将VITE_AI_API_URL和VITE_OLLAMA_API_URL设置为同源反向代理路径
- 解决浏览器混合内容安全策略导致的API调用失败问题
This commit is contained in:
2026-02-28 02:07:02 +08:00
parent ac9712819a
commit 5b4f5c393e
3 changed files with 9 additions and 6 deletions

View File

@@ -9,12 +9,12 @@ VITE_FILE_SERVER=https://your-file-server.com
# AI 网关(OpenAI兼容)
# - 开发环境推荐走同源反代VITE_AI_API_URL=/ai-proxy配合 vite.config.ts
# - 生产环境可直连(需 AI 服务允许 CORS或在 Nginx 里配置 /ai-proxy 反代
VITE_AI_API_URL=http://127.0.0.1:11434/api/v1
VITE_AI_API_URL=/ai-proxy
# Ollama 原生接口(默认端口 11434
# - 开发环境推荐走同源反代VITE_OLLAMA_API_URL=/proxy配合 vite.config.ts
# - 生产环境不要直接用 http会混合内容被拦截建议 Nginx 反代成同源 https
VITE_OLLAMA_API_URL=http://127.0.0.1:11434
VITE_OLLAMA_API_URL=/proxy
# 仅用于本地开发反代注入vite.config.ts 会读取并注入到 /ai-proxy 请求头)
# 不要加 VITE_ 前缀,避免被打包到前端产物里

View File

@@ -20,7 +20,7 @@ export const FILE_SERVER =
export const AI_API_URL =
import.meta.env.VITE_AI_API_URL ||
// Prefer same-origin reverse proxy during local development to avoid CORS.
(import.meta.env.DEV ? '/ai-proxy' : 'http://127.0.0.1:11434/api/v1');
(import.meta.env.DEV ? '/ai-proxy' : 'https://ai-api.websoft.top/api/v1');
// Ollama native API endpoint (usually http://host:11434).
// Note: browsers cannot call http from an https site (mixed-content); prefer same-origin proxy.

View File

@@ -12,9 +12,12 @@
// Hardcode endpoint to avoid going through mp.websoft.top `/proxy`.
// The API methods append `/api/*` paths.
const BASE_URL = import.meta.env.PROD
? 'http://127.0.0.1:11434'
: 'https://ai-api.websoft.top';
//
// IMPORTANT: do not use `127.0.0.1` in browser production builds:
// it points to the visitor's machine, not your server.
// If you want to use server-local Ollama (`127.0.0.1:11434`), put it behind an HTTPS reverse proxy
// (e.g. `https://ai-api.websoft.top` or same-origin `/proxy`).
const BASE_URL = 'https://ai-api.websoft.top';
const modelLoading = ref(false);
const models = ref<Array<{ id: string; name?: string }>>([]);