在开发的过程中 代理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 http

import 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

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐