vue 打包后访问接口报错404 解决方案 (前提是在vue里使用了代理)

先上图展示下自己vue里面针对跨域设置的代理截图,记住红框里面的代码,待会要用。一般设置代理都是为了防止在测试调用接口的时候出现跨域的情况,但是设置代理后直接npm run start并没有问题,访问接口也正常,但是当打包执行npm run build 之后,console控制台会出现下图404的情况

如果build后的dist文件是放在nginx上的,可用以下方式解决 ,记得上图红框里面的代码吗,把对应的接口地址和反向代理的名称在nginx的nginx.conf文件里补充一下就可以,解决404的问题

代码:
server {
    listen 8082;
    server_name localhost;
    location / {
        root D:/GXT/gxtWebsite/dist;
        index index.html index.htm;
    }
    location /getNews {
        proxy_pass http://172.16.27.67:8080/enterprise/selectAll;
    }
    location /getProducts {
        proxy_pass http://172.16.27.67:8080/enterprise/selectProductSolution;
    }
}

图:

另:千万不要在 

   location /getNews {
        proxy_pass http://172.16.27.67:8080
    }

里面直接写域名,至少后面跟一个方法  /selectAll,虽然我不知道为什么,但是直接域名的话好像不行,还是会404

location /getNews {
        proxy_pass http://172.16.27.67:8080/enterprise/selectAll;
  }

猜你喜欢

转载自www.cnblogs.com/gaoxiaoxuner/p/11887159.html