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