From b272ad09ce80ba47b15c47b8d99ce96a95d50a2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Fri, 27 Feb 2026 22:59:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor(proxy):=20=E7=BB=9F=E4=B8=80=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E8=B7=AF=E5=BE=84=E9=85=8D=E7=BD=AE=E4=BB=8E=20/ollam?= =?UTF-8?q?a-proxy=20=E5=88=B0=20/proxy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 nginx 反向代理路径从 /ollama-proxy 修改为 /proxy - 更新 .env.development 和 .env.example 中的 VITE_OLLAMA_API_URL 配置 - 修改 src/config/setting.ts 中的默认代理路径配置 - 更新 vite.config.ts 中的代理配置路径映射 - 优化代码格式化和多行语句的换行处理 - 调整打包配置中的文件命名逻辑格式 --- .env.development | 2 +- .env.example | 2 +- docs/AI_PROXY_NGINX.md | 4 +-- src/config/setting.ts | 2 +- vite.config.ts | 58 ++++++++++++++++++++++++++++-------------- 5 files changed, 44 insertions(+), 24 deletions(-) diff --git a/.env.development b/.env.development index ae6beaf..275d639 100644 --- a/.env.development +++ b/.env.development @@ -9,7 +9,7 @@ VITE_APP_NAME=后台管理(开发环境) VITE_AI_API_URL=/ai-proxy # Ollama 原生接口(开发环境建议走同源反代,避免浏览器 CORS) -VITE_OLLAMA_API_URL=/ollama-proxy +VITE_OLLAMA_API_URL=/proxy # 如果 AI 网关启用了鉴权(401 Not authenticated),填入你的 Key(仅供本机 dev server 使用) # 不要加 VITE_ 前缀,避免被打包进前端 diff --git a/.env.example b/.env.example index 78102ce..3036ddb 100644 --- a/.env.example +++ b/.env.example @@ -12,7 +12,7 @@ VITE_FILE_SERVER=https://your-file-server.com VITE_AI_API_URL=https://ai-api.websoft.top/api/v1 # Ollama 原生接口(默认端口 11434) -# - 开发环境推荐走同源反代:VITE_OLLAMA_API_URL=/ollama-proxy(配合 vite.config.ts) +# - 开发环境推荐走同源反代:VITE_OLLAMA_API_URL=/proxy(配合 vite.config.ts) # - 生产环境不要直接用 http(会混合内容被拦截),建议 Nginx 反代成同源 https VITE_OLLAMA_API_URL=http://47.119.165.234:11434 diff --git a/docs/AI_PROXY_NGINX.md b/docs/AI_PROXY_NGINX.md index 4cc4e22..61a8d0f 100644 --- a/docs/AI_PROXY_NGINX.md +++ b/docs/AI_PROXY_NGINX.md @@ -47,7 +47,7 @@ VITE_AI_API_URL=/ai-proxy 如果你要直接用原生 Ollama(`http://:11434`),生产环境同样建议走同源反代(避免 CORS + https 混合内容): ```nginx -location /ollama-proxy/ { +location /proxy/ { proxy_pass http://47.119.165.234:11434/; proxy_http_version 1.1; proxy_set_header Host 47.119.165.234; @@ -63,7 +63,7 @@ location /ollama-proxy/ { 然后把 `VITE_OLLAMA_API_URL` 配置为: ```bash -VITE_OLLAMA_API_URL=/ollama-proxy +VITE_OLLAMA_API_URL=/proxy ``` ## 3) 关于 API Key diff --git a/src/config/setting.ts b/src/config/setting.ts index 849db15..0110ac8 100644 --- a/src/config/setting.ts +++ b/src/config/setting.ts @@ -26,7 +26,7 @@ export const AI_API_URL = // Note: browsers cannot call http from an https site (mixed-content); prefer same-origin proxy. export const OLLAMA_API_URL = import.meta.env.VITE_OLLAMA_API_URL || - (import.meta.env.DEV ? '/ollama-proxy' : 'http://47.119.165.234:11434'); + (import.meta.env.DEV ? '/proxy' : 'http://47.119.165.234:11434'); /** * 以下配置一般不需要修改 diff --git a/vite.config.ts b/vite.config.ts index a4d60a1..a20a541 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -70,7 +70,10 @@ export default defineConfig(({ command, mode }) => { // 代理配置 proxy: { '/api': { - target: env.VITE_API_URL || process.env.VITE_API_URL || 'https://server.websoft.top', + target: + env.VITE_API_URL || + process.env.VITE_API_URL || + 'https://server.websoft.top', changeOrigin: true, secure: false, configure: (proxy, _options) => { @@ -78,12 +81,20 @@ export default defineConfig(({ command, mode }) => { console.log('proxy error', err); }); proxy.on('proxyReq', (proxyReq, req, _res) => { - console.log('Sending Request to the Target:', req.method, req.url); + console.log( + 'Sending Request to the Target:', + req.method, + req.url + ); }); proxy.on('proxyRes', (proxyRes, req, _res) => { - console.log('Received Response from the Target:', proxyRes.statusCode, req.url); + console.log( + 'Received Response from the Target:', + proxyRes.statusCode, + req.url + ); }); - }, + } }, // OpenAI-compatible gateway reverse proxy (dev only). // Example: @@ -94,7 +105,10 @@ export default defineConfig(({ command, mode }) => { changeOrigin: true, secure: false, rewrite: (path) => - path.replace(/^\/ai-proxy/, env.AI_PROXY_REWRITE_PREFIX || '/api/v1'), + path.replace( + /^\/ai-proxy/, + env.AI_PROXY_REWRITE_PREFIX || '/api/v1' + ), configure: (proxy) => { proxy.on('proxyReq', (proxyReq) => { // Inject auth for local dev to avoid putting API keys in the browser. @@ -110,17 +124,17 @@ export default defineConfig(({ command, mode }) => { } }, // Ollama native API reverse proxy (dev only). - // GET /ollama-proxy/api/tags -> http://47.119.165.234:11434/api/tags - // POST /ollama-proxy/api/chat -> http://47.119.165.234:11434/api/chat - '/ollama-proxy': { + // GET /proxy/api/tags -> http://47.119.165.234:11434/api/tags + // POST /proxy/api/chat -> http://47.119.165.234:11434/api/chat + '/proxy': { target: 'http://47.119.165.234:11434', changeOrigin: true, secure: false, - rewrite: (path) => path.replace(/^\/ollama-proxy/, '') + rewrite: (path) => path.replace(/^\/proxy/, '') } }, // 端口冲突时的处理 - strictPort: false, // 允许自动选择其他端口 + strictPort: false // 允许自动选择其他端口 }, // 预览服务器配置(用于生产构建预览) preview: { @@ -128,7 +142,7 @@ export default defineConfig(({ command, mode }) => { host: '0.0.0.0', open: true, cors: true, - strictPort: false, + strictPort: false }, plugins: [ vue({ @@ -167,12 +181,13 @@ export default defineConfig(({ command, mode }) => { ext: '.br' }), // 打包分析 - isBuild && visualizer({ - filename: 'dist/stats.html', - open: false, - gzipSize: true, - brotliSize: true - }) + isBuild && + visualizer({ + filename: 'dist/stats.html', + open: false, + gzipSize: true, + brotliSize: true + }) ].filter(Boolean), css: { preprocessorOptions: { @@ -230,14 +245,19 @@ export default defineConfig(({ command, mode }) => { // 文件命名 chunkFileNames: (chunkInfo) => { const facadeModuleId = chunkInfo.facadeModuleId - ? chunkInfo.facadeModuleId.split('/').pop().replace(/\.\w+$/, '') + ? chunkInfo.facadeModuleId + .split('/') + .pop() + .replace(/\.\w+$/, '') : 'chunk'; return `js/${facadeModuleId}-[hash].js`; }, assetFileNames: (assetInfo) => { const info = assetInfo.name.split('.'); const ext = info[info.length - 1]; - if (/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name)) { + if ( + /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name) + ) { return `media/[name]-[hash].${ext}`; } if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(assetInfo.name)) {