部署使用了代理/api的vue项目到nginx,配置nginx代理

   vue项目使用代理,解决跨域问题。  在本地可以正常启动, 部署在服务器之后有 /api 不能访问到后台。  本地启动命令 npm run serve 。

  npm打包之后,vue项目的dist包里面都是静态文件。

  配置成如下的nginx配置代理。

location / {
        root    /home/xxx/dist;
        index   index.html index.htm;
        # 添加下面的几行 vue项目刷新不会404
        try_files $uri $uri/ @router;
}
# 添加下面的几行 vue项目刷新不会404
location @router {
        rewrite ^.*$ /index.html last;
}
# 设置反向代理,因为前台是通过代理访问的  记得加上 最后的/不然无效。
location /api/ {
        proxy_pass      http://192.168.1.128:8080/;
}

 

posted @ 2022-10-11 11:08  不要西红柿  阅读(1782)  评论(1编辑  收藏  举报