vue打包后的静态资源部署到了nginx下的html文件夹里,start nginx后,在浏览器中输入localhost:9000/index.html看到了页面,但是数据请求的接口不是需要跨域就是代理到了localhost:9000,并没有代理到目标接口http://192.168.122.44:8000/R5Service
server {
listen 9000;
server_name localhost
location / {
root html;
index index.html index.htm;
}
location /api/ {
proxy_pass http://192.168.122.44:8000/R5Service;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
我在vue中用了两种请求,一种是
axios.defaults.baseURL = '/api/'
然后请求时如下
this.axios.get("GetPilotJson").then(res => {
console.log(JSON.parse(res.data));
});
另一种是请求完整的接口地址
this.axios.get("http://192.168.122.44:8000/R5Service/GetFlightJson").then(res => {
console.log(JSON.parse(res.data));
});
从下图看出
用api请求的从报错里可以看出请求的是http://localhost:9000/api/GetPilotJson,
直接请求的就会报跨域错误
那么用nginx反向代理解决跨域到底是怎么操作的?
你的问题在于proxy_pass的这个路由的问题