Loading

vue打包成dist发布到服务器后代理失效

1、项目中用到了代理,本地开发时没问题,但是发布到服务器就404。

// config文件夹下面index.js文件
 
module.exports = {
  dev: {
    assetsSubDirectory: "static",
    assetsPublicPath: "/",
    proxyTable: {
      "/api": {
        target: 'http://www.test.com/',      // 需要访问的代理接口
        changeOrigin: true,
        pathRewrite: {
          "^/api": "/"
        }
      }
    },
    host: 'localhost', // can be overwritten by process.env.HOST
    port: 8001, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
    autoOpenBrowser: true,
    errorOverlay: true,
    notifyOnErrors: true,
    poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-
    useEslint: true,
    showEslintErrorsInOverlay: false,
    devtool: "eval-source-map",
    cacheBusting: true,
    cssSourceMap: false
  },
}

2、打包发布后需要在nginx配置文件中加入如下配置:

location /api{
        rewrite ^.+api/?(.*)$ /$1 break; //可选参数,正则验证地址
        include uwsgi_params; //可选参数,uwsgi是服务器和服务端应用程序的通信协议,规定了怎么把请求转发给应用程序和返回
        proxy_pass http://www.test.com; // 接口地址
}
posted @ 2021-01-06 15:14  dBevil  阅读(6219)  评论(0编辑  收藏  举报