正常走代理的请求:(其实http://www.only-screen.xyz:8080/api  ==   http://www.only-screen.xyz:8888)

一般vue 项目这么打包

npm run build

config/index.js  设置:  之前一个项目我也设置这样的,部署上去没问题。

// see http://vuejs-templates.github.io/webpack for documentation.
var path = require('path')
module.exports = {
  dev: {
    env: require('./dev.env'),
    host: 'localhost',
    port: 8080,
    autoOpenBrowser: true,
    autoOpenPage: '/login',
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
      '/api': {
        target: 'http://localhost:8888',
        pathRewrite: {
          '^/api': '/'
        }
      }
    },
    // CSS Sourcemaps off by default because relative paths are "buggy"
    // with this option, according to the CSS-Loader README
    // (https://github.com/webpack/css-loader#sourcemaps)
    // In our experience, they generally work as expected,
    // just be aware of this issue when enabling this option.
    cssSourceMap: false
  },
  build: {
    env: require('./prod.env'),
    index: path.resolve(__dirname, '../dist/index.html'),
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: './',
    productionSourceMap: true,
    // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],
    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
  }
}

效果:   http://www.only-screen.xyz:8080/api  走代理后 等同于访问: http://www.only-screen.xyz:8888  

生成的dist文件上传到 linux 、 nginx 正常代理的情况下照理说是没问题的,

但是我发现前端发送接口,居然 还是按照  8080 发出去,没有带 /api 这个 后缀,

找了好久,最后 发现是:

这个文件

箭头这一行要加上,表示是 base_url  就可以了。  不走代理就这样解决了。

over

Logo

前往低代码交流专区

更多推荐