vue cli3 跨域代理多个域名,第一个有效,后面的无效404

问题描述

vue cli3 跨域代理多个域名,第一个有效,后面的无效404

问题出现的环境背景及自己尝试过哪些方法

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)
devServer: {

    proxy: {
        '/apis': {
            target: 'http://xxx.xxx.cc/', 
            ws: true,  // proxy websockets 
            changeOrigin: true,  //允许跨域
            pathRewrite: {
                '^/apis': ''  // rewrite path
            }
        },
        '/apis2': {
            target: 'http://xxx.aliyuncs.com/',  
            ws: true,  // proxy websockets 
            changeOrigin: true,  //允许跨域
            pathRewrite: {
                '^/apis2': ''  // rewrite path
            }
        },
        
    }
},

你期待的结果是什么?实际看到的错误信息又是什么?

阅读 5.2k
4 个回答

把/apis2换成别的名字 比如 /req 尽量不一样

clipboard.png

你确定么 我这样写是可以的 试试重启 404那肯定就是地址不对 看看你请求的地址是多少

按照如下的方式写

devServer: {
    proxy: {
        '^/apis/': {
            target: 'http://xxx.xxx.cc/', 
            ws: true,  // proxy websockets 
            changeOrigin: true,  //允许跨域
            pathRewrite: {
                '^/apis/': '/'  // rewrite path
            }
        },
        '^/apis2/': {
            target: 'http://xxx.aliyuncs.com/',  
            ws: true,  // proxy websockets 
            changeOrigin: true,  //允许跨域
            pathRewrite: {
                '^/apis2/': '/'  // rewrite path
            }
        },
        
    }
}

对比修改后的,不用我说具体是什么原因了吧?

新手上路,请多包涵

配置改了后有重启吗?
还有如果发起的的是get请求,勾选上Disable cache避免浏览器缓存。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题