我遇到了类似的问题,使用类似下面这样的设置,每个代理请求都需要5秒或更长时间:
"proxy": [
{
"context": [
"/api",
],
"target": "http://my-backend-server.local:1234",
"secure": false
}
]
在hosts文件中:
127.0.0.1 my-backend-server.local
127.0.0.1 some-other-hostname.local
127.0.0.1 a-few-more-of-these.local
当我将代理更改为指向IPv6回环地址时,问题消失了。就像这样:
"proxy": [
{
"context": [
"/api",
],
"target": "http://[::1]:1234",
"secure": false
}
]
为了能够在代理配置中使用实际的主机名而不是回环地址,我编辑了我的主机文件,将所有主机名条目都放在单行上,并将它们指向IPv4和IPv6回环地址。就像这样:
127.0.0.1 my-backend-server.local some-other-hostname.local a-few-more-of-these.local
::1 my-backend-server.local some-other-hostname.local a-few-more-of-these.local
现在延迟问题已经解决,它的功能符合预期。
devServer: {
proxy: {
'/api': {
target: 'http://mybackedn.url',
changeOrigin: true,
headers: {
Connection: 'keep-alive' // add this can fix this issue
}
}
}
}