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; // 接口地址
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM