vue项目配置多个代理的注意点

news/2024/7/10 0:08:00 标签: 前端, Vue, 代理, 多个代理, MaxList

Vue项目的开发过程中,为了本地调试方便,我们通常会在 vue.config.js 中配置 devServer 来在本地启动一个服务器,在这个选项中,我们会配置proxy 属性来将指向到本地的请求(例如: /api/action代理到后端的开发服务器上(例如: http://xxx.xxx.xxx/api/action

devServer: {
        port: 8081,
        proxy: {
            '/api/action': {
                target: 'http://192.168.200.106:81',
                changeOrigin: true,
                ws: true,
                secure: false
            }
        }
    },```

在这个配置中,要注意以下两点:

接口地址有重叠地址时,将匹配度低的放在后面。

例如:
* 将 / 匹配到 192.191.1.1;
* 将 /api 匹配到 192.191.1.2
* 将 /api/action 匹配到 192.191.1.3

如果我们像下面一样书写:

proxy: {
            '/': {
                target: 'http://192.191.1.1',
                changeOrigin: true,
                ws: true,
                secure: false
            },
			 '/api': {
                target: 'http://192.191.1.2',
                changeOrigin: true,
                ws: true,
                secure: false
            },
			 '/api/action': {
                target: 'http://192.191.1.3',
                changeOrigin: true,
                ws: true,
                secure: false
            }
        }

那么所有到/, /api/api/action 的请求将全部被代理192.191.1.1 上面去

原因是这里的匹配实际上是一个正则匹配的过程,当我们请求 /api 时,首先读取到了配置项中的第一个,拿配置中的 / 去匹配请求中的 /api , 发现请求的/api 中包含配置项/, 匹配成功,直接将请求代理到了 192.191.1.1 上面去, 对/api/action的匹配也同理。

也就是说,它的匹配规则是: 拿配置项中的地址去匹配请求中的地址,如果请求中的地址中包含配置中的地址,则匹配成功,否则,拿下一个配置项继续匹配。

所以,配置中的地址与请求地址中匹配的字符越少,匹配度越低。 上例中配置中的地址(/)与请求地址(/api)只有一个字符是匹配的,所以匹配度低。

所以我们正确的写法应该是:

proxy: {
            '/api/action': {
                target: 'http://192.191.1.3',
                changeOrigin: true,
                ws: true,
                secure: false
            },
			 '/api': {
                target: 'http://192.191.1.2',
                changeOrigin: true,
                ws: true,
                secure: false
            },
			 '/': {
                target: 'http://192.191.1.1',
                changeOrigin: true,
                ws: true,
                secure: false
            }
        }

这样到三个地址的请求就都可以正确代理到相应的地址去了

多个地址代理同一个target 时,可进行合并

在实际应用中,由于后端采用微服务模式开发,在开发阶段,我们可能会将不同的服务代理到不同的地址上,当服务很多时,我们代理的数量也就很多:

proxy: {
  		'/api/action': {
                target: 'http://192.191.1.3',
                changeOrigin: true,
                ws: true,
                secure: false
            },
              '/api/action2': {
                target: 'http://192.191.1.4',
                changeOrigin: true,
                ws: true,
                secure: false
            },
              '/api/action3': {
                target: 'http://192.191.1.3',
                changeOrigin: true,
                ws: true,
                secure: false
            },
              '/api/action4': {
                target: 'http://192.191.1.4',
                changeOrigin: true,
                ws: true,
                secure: false
            },
              '/api/action5': {
                target: 'http://192.191.1.5',
                changeOrigin: true,
                ws: true,
                secure: false
            },
              '/api/action6': {
                target: 'http://192.191.1.6',
                changeOrigin: true,
                ws: true,
                secure: false
            },
              '/api/action7': {
                target: 'http://192.191.1.5',
                changeOrigin: true,
                ws: true,
                secure: false
            },
              '/api/action8': {
                target: 'http://192.191.1.6',
                changeOrigin: true,
                ws: true,
                secure: false
            },
              '/api/action9': {
                target: 'http://192.191.1.7',
                changeOrigin: true,
                ws: true,
                secure: false
            },
			 '/api': {
                target: 'http://192.191.1.2',
                changeOrigin: true,
                ws: true,
                secure: false
            },
			 '/': {
                target: 'http://192.191.1.1',
                changeOrigin: true,
                ws: true,
                secure: false
            },
              
        }

当配置的代理数量超过十个时,开发环境编译打包时会报以下错误:

在这里插入图片描述

为了解决报错,也同时减少代码体积,我们可以对具有同一个target的配置项进行合并,由上文我们可知,这里其实是一个正则匹配的过程,那我们就可以利用正则语法将他们进行合并:

proxy: {
  		'/api/action|/api/action3': {
                target: 'http://192.191.1.3',
                changeOrigin: true,
                ws: true,
                secure: false
            },
              '/api/action2|/api/action4'': {
                target: 'http://192.191.1.4',
                changeOrigin: true,
                ws: true,
                secure: false
            },
             
              '/api/action5|/api/action7': {
                target: 'http://192.191.1.5',
                changeOrigin: true,
                ws: true,
                secure: false
            },
              '/api/action6|/api/action8': {
                target: 'http://192.191.1.6',
                changeOrigin: true,
                ws: true,
                secure: false
            },
              '/api/action9': {
                target: 'http://192.191.1.7',
                changeOrigin: true,
                ws: true,
                secure: false
            },
			 '/api': {
                target: 'http://192.191.1.2',
                changeOrigin: true,
                ws: true,
                secure: false
            },
			 '/': {
                target: 'http://192.191.1.1',
                changeOrigin: true,
                ws: true,
                secure: false
            },
              
        }

当然,在正式部署的时候,还是需要后端去做统一代理


http://www.niftyadmin.cn/n/1736047.html

相关文章

NDEBUG

头文件assert.h定义的宏受NDEBUG的影响.如果预程序在处理这个头文件时已经定义了NDEBUG,assert宏的内容就定义为空,这意味着assert宏不在起作用.所以,可以在最终发布程序的时候可以使用-DNDEBUG关闭断言功能或者把#define NDEBUG加到每个源文件中,但这条语句必须放在#include &…

JS 中 `toLocaleString`妙用

缘起 kaven老师分享了一个数值取整的方法,即利用按位非操作符(~)进行取整: var a 1.5; console.log(~~a); // 1但是这种方法有点限制就是它只能进行向下取整,无法实现四舍五入。 所以就想到了toLocaleString() 方法…

js经典面试题-连续赋值时发生了什么

题目 var a {n:1}; var b a; a.x a {n:2}console.log(a); // ? console.log(b); // ? console.log(a.x) // ?考点分析 连续赋值的解析是从左到右的连续赋值的运算是从右到左的引用类型变量二次赋值时有一个重新绑定的过程对于引用类型,栈内存里存储的是一个…

TRACE宏 ASSERT宏 VERIFY 宏

一、TRACE宏 当选择了Debug目标,并且afxTraceEnabled变量被置为TRUE时,TRACE宏也就随之被激活了。但在程序的Release版本中,它们是被完全禁止的。下面是一个典型的TRACE语句: … int nCount 9 ; CStr…

br为什么能换行

背景 在做富文本编辑器基础研究时,研究到DOM节点的offsetLeft属性,发现当我们不对浏览器默认样式进行如下reset时,该属性的值会比期望的多出8px的宽度(chrome浏览器): * {margin: 0; }同时,我们也知道&am…

C语言宏定义方法总结

宏的单行定义 #define A(x) T_##x #define B(x) #x #define C(x) #x 我们假设:x1,则有: A(1)------〉T_1 B(1)------〉 1 C(1)------〉 "1 " .如何定义宏、取消宏 //定义宏 #defi…

逐个字符读字符串

因为 scanf 函数和 gets 函数都有风险且不够灵活,C 程序员经常会编写自己的输入函数。通过每次一个字符的方式来读入字符串。下面是自己编写的读取字符串的函数 read_line(): int read_line(char strp[], int n) {char ch;int i 0;while((ch getchar()) ! \n)if(i…

富文本编辑器开发系列2-document.execCommand 的API

系列文章快速阅读: 富文本编辑器开发系列-1-基础概念 富文本编辑器开发系列2-document.execCommand 的API 富文本编辑器开发系列3-selection 富文本编辑器开发系列4——Range对象 富文本编辑器开发系列5——浏览器Selection API探究 富文本编辑器开发系列6——Range…