方案一:启动本地 CORS 代理(推荐)
复制以下命令,在终端中运行即可启动一个本地代理服务:
node -e "const h=require('http'),u=require('url');h.createServer((q,r)=>{r.setHeader('Access-Control-Allow-Origin','*');r.setHeader('Access-Control-Allow-Methods','*');r.setHeader('Access-Control-Allow-Headers','*');if(q.method==='OPTIONS'){r.writeHead(204);r.end();return}const t=u.parse(q.url.slice(1),false).href;if(!t.startsWith('http')){r.writeHead(400);r.end('Bad URL');return}const p=new URL(t),o=(p.protocol==='https:'?require('https'):h).request(t,{method:q.method,headers:{...q.headers,host:p.host}},s=>{r.writeHead(s.statusCode,s.headers);s.pipe(r)});o.on('error',e=>{r.writeHead(502);r.end(e.message)});q.pipe(o)}).listen(8080,()=>console.log('CORS Proxy: http://localhost:8080'))"
启动后在"高级设置 → CORS 代理地址"填入 http://localhost:8080/
方案二:API 服务器启用 CORS
在 API 服务器启动时添加 CORS 允许配置:
Ollama: 设置环境变量 OLLAMA_ORIGINS=*
vLLM: 启动参数 --api-server-cors-allow-origins *
LMStudio: 设置 CORS 为 true
其他: 在 Nginx/反向代理中添加 CORS 头