【问题标题】:nodejs node-proxy, use a route table and modify responsenodejs node-proxy,使用路由表并修改响应
【发布时间】:2012-12-03 12:51:54
【问题描述】:

我们的实验室有一个简单的 nodejs 流程用于测试/开发工作。我们正在使用node-proxy 设置反向代理,允许我们使用 http(即 http -> https)与 https 服务器通信。作为其中的一部分,我们有一些用于修改响应的代码。这是我们正在做的事情:

var http = require('http'),
    httpProxy = require('http-proxy'), 
    endserver = 'server.local',
    endport = 8443;

var proxy = new httpProxy.HttpProxy({ 
  target: {
    https: true,
    host: endserver,
    port: endport
  } 
});

http.createServer(function (req, res) {
  //our code for modifying response is here...
  proxy.proxyRequest(req, res);
}).listen(8001);

我想设置一个node-proxy路由表,像这个例子here,这样我可以根据请求中的主机名将请求转发到不同的https服务器(我会在我们的dns指向上设置多个主机名到运行 nodejs 代理的同一台服务器)。

如何拥有节点代理路由表并修改响应?

(我对 node.js 很陌生,但对 javascript 不熟悉)

【问题讨论】:

    标签: node.js reverse-proxy


    【解决方案1】:

    如果我正确阅读了您的问题,您只需要知道如何使用节点代理路由表根据请求对象为不同主机路由请求。这实际上是由 node-http-proxy 文档解决的:Proxy requests using a ProxyTable

    粘贴过来,你只需要像这样在 JS 对象中配置路径:

    var options = {
      router: {
        'foo.com/baz': '127.0.0.1:8001',
        'foo.com/buz': '127.0.0.1:8002',
        'bar.com/buz': '127.0.0.1:8003'
      }
    };
    

    然后在你制作代理服务器的时候传入这个对象:

    var proxyServer = httpProxy.createServer(options);
    proxyServer.listen(80);
    

    为您的代码指定这个会导致如下结果:

    var http = require('http'),
        httpProxy = require('http-proxy'), 
        endserver = 'server.local',
        endport = 8443;
    
    var options = {
      router: {
        endserver + '/foo': '127.0.0.1:8081',
        endserver + '/bar': '127.0.0.1:8082',
        endserver + '/baz': '127.0.0.1:8083'
      }
    };
    
    var proxyServer = httpProxy.createServer(options);
    proxyServer.listen(endport);
    
    http.createServer(function (req, res) {
      // your code for modifying response is here...
      proxy.proxyRequest(req, res);
    }).listen(8001);
    

    使用路由器选项时无需自己设置目标。似乎也处理了https的区别,但我不确定。我从proxy-table.js source 得到了这个信息。特别是:

    ProxyTable.prototype.setRoutes = function (router) {
      if (!router) {
        throw new Error('Cannot update ProxyTable routes without router.');
      }
    
      var self = this;
      this.router = router;
    
      if (this.hostnameOnly === false) {
        this.routes = [];
    
        Object.keys(router).forEach(function (path) {
          if (!/http[s]?/.test(router[path])) {
            router[path] = (self.target.https ? 'https://' : 'http://')
              + router[path];
          }
    
          var target = url.parse(router[path]),
              defaultPort = self.target.https ? 443 : 80;
    
          //
          // Setup a robust lookup table for the route:
          //
          // {
          // source: {
          // regexp: /^foo.com/i,
          // sref: 'foo.com',
          // url: {
          // protocol: 'http:',
          // slashes: true,
          // host: 'foo.com',
          // hostname: 'foo.com',
          // href: 'http://foo.com/',
          // pathname: '/',
          // path: '/'
          // }
          // },
          // {
          // target: {
          // sref: '127.0.0.1:8000/',
          // url: {
          // protocol: 'http:',
          // slashes: true,
          // host: '127.0.0.1:8000',
          // hostname: '127.0.0.1',
          // href: 'http://127.0.0.1:8000/',
          // pathname: '/',
          // path: '/'
          // }
          // },
          //
          self.routes.push({
            source: {
              regexp: new RegExp('^' + path, 'i'),
              sref: path,
              url: url.parse('http://' + path)
            },
            target: {
              sref: target.hostname + ':' + (target.port || defaultPort) + target.path,
              url: target
            }
          });
        });
      }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 2011-12-03
      • 2014-05-30
      • 2012-11-15
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      相关资源
      最近更新 更多