前提是在 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;}