在开发的过程中 代理proxy报错:
[vite] http proxy error: /ranking/hostRank?dateType=1
Error: connect ETIMEDOUT 43.xxx.xxx.xxx:443
网络请求是http的:
// vite.config.ts
import { Agent } from 'node:http';server: {host: '0.0.0.0',port: port,open: true,https: false,proxy: {'***': {target: '***',ws: true,changeOrigin: true,agent: new Agent({ keepAlive: true, keepAliveMsecs: 20000 }), // add this line},},},
网络请求是https的:
// vite.config.ts
import { Agent } from 'node:https';server: {host: '0.0.0.0',port: port,open: true,https: false,proxy: {'***': {target: '***',ws: true,changeOrigin: true,agent: new Agent({ keepAlive: true, keepAliveMsecs: 20000 }), // add this line},},},
上面本人亲测有效
提示:如果是使用yarn
yarn add agent-base
yarn add httpimport http from "http";
agent: new http.Agent({keepAlive: true, keepAliveMsecs: 20000})
如果代理地址是localhost
不用localhost,将proxy配置中的target的地址改成使用127.0.0.1 (其他人已谈到的解决方案)
另外还有大佬提到下面这个,提供参考
export NODE_OPTIONS="--dns-result-order=ipv4first"
vite