【问题标题】:Proxy multiple hosts that have same URL patterns代理具有相同 URL 模式的多个主机
【发布时间】:2018-08-12 11:07:06
【问题描述】:

我正在使用 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"
});

解决方案 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】:

    我设法使用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,请多多包涵。

    【讨论】:

      猜你喜欢
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 2013-03-23
      • 1970-01-01
      相关资源
      最近更新 更多