代理具有相同URL模式的多个主机

问题描述 投票:0回答:1

我正在使用http-proxy-middleware来代理我的API调用

如何代理多个目标主机?我已经搜索了这个问题但仍然无法理解。

https://github.com/chimurai/http-proxy-middleware/issues/12 - 允许我使用代理表但是我应该为target添加什么链接?因为我有多个目标。

我正在考虑使用模式匹配来代理多个主机,但是会出现另一个问题,因为我有一些主机具有几乎相同的URL链接。

试过下面的一些解决方案,但不起作用

解决方案1

const proxyOptions = proxy({
  target: ["https://website1.com", "https://website2.com", "https://api.website3.com"],
  changeOrigin: true,
  loglevel: "debug"
});

Solutuon 2

const proxyTable = {
  "/api": ["https://website1.com", "https://website2.com", "https://api.website3.com"]
};

const proxyOptions = proxy({
  target: "http://localhost:3000",
  router: proxyTable,
  changeOrigin: true,
  loglevel: "debug"
});

解决方案3

server.use(
      proxy("/api", { target: "https://website1.com", changeOrigin: true }),
      proxy("/api", { target: "https://website2.com", changeOrigin: true }),
      proxy("/api", { target: "https://api.website3.com", changeOrigin: true })
    );

挂载代理

server.use("/api", proxyOptions)

感谢您查看此问题!

javascript node.js express http-proxy node-http-proxy
1个回答
0
投票

我设法使用pathRewrite获得解决方案

const website1 = proxy({
  target: "https://website1.com",
  changeOrigin: true,
  pathRewrite: {
    "^/website1": "/api"
  },
  loglevel: "debug"
});

const website2 = proxy({
  target: "https://website2.com",
  changeOrigin: true,
  pathRewrite: {
    "^/website2": "/api"
  },
  loglevel: "debug"
});

const website3 = proxy({
  target: "https://api.website3.com",
  changeOrigin: true,
  pathRewrite: {
    "^/website3": "/api"
  },
  loglevel: "debug"
});

挂载服务器

const server = express();

server.use("/website1", website1);
server.use("/website2", website2);
server.use("/website3", website3);

但是,这将给我的网站很多我不需要的页面,如http://localhost:3000/website1等。

有没有办法隐藏这些页面或显示其他东西而不是我正在使用的网站的主页。

对不起,我还在学习node.js请耐心等待。

© www.soinside.com 2019 - 2024. All rights reserved.