open-webui二次开发跨域问题nginx解决
·
server {
listen 8888;
server_name localhost;
# 前端代理(Vite等开发服务器)
location / {
proxy_pass http://127.0.0.1:5173;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# 后端代理及CORS配置(API/Ollama/OpenAI等)
location ~ ^/(api|ollama|openai|ws)/ {
# 动态验证来源(防止伪造Origin)
if ($http_origin ~* (localhost:5173|yourdomain.com)) {
set $cors_origin $http_origin;
}
# CORS核心响应头(必须用变量动态匹配来源)
add_header 'Access-Control-Allow-Origin' $cors_origin always; #
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
add_header 'Access-Control-Allow-Credentials' 'true' always; # 必须与前端credentials模式匹配
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
# 处理OPTIONS预检请求(需返回详细头部)
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204; #
}
# 反向代理到后端
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
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;
}
}
更多推荐


所有评论(0)