本地用 8080 端口启动的服务访问后台API有跨域问题,
from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
其实用 9021 端口后台就会放行,故用 nginx 解决
server {
listen 8080;
server_name localhost;
location /file/upload {
proxy_pass http://后台IP地址/file/upload; # 后端地址和端口
proxy_set_header Host "localhost:9021";
proxy_set_header Referer "http://localhost:9021";
proxy_set_header Connection "keep-alive";
proxy_set_header Cookie $http_cookie;
}
}
我的项目后端主要是根据 Referer 判断跨域,所以可以访问了。需要设置其它 header,自己手动设置