简体   繁体   English

Node.js-我将如何转发HTTP通信(反向代理)?

[英]Node.js - How would I forward HTTP traffic (reverse proxy)?

I need what is essentially a reverse proxy but I need it in Node.js as I will have to put in some custom functionality. 我需要本质上是反向代理的东西,但是我需要在Node.js中使用它,因为我将不得不添加一些自定义功能。

The gateway will be the only visible service, and it needs to forward traffic on to an internal network of services. gateway将是唯一可见的服务,它需要将流量转发到内部服务网络。 A simple 302 isn't going to work here. 简单的302在这里不起作用。

How can I realistically achieve this with Node.js given the asynchronous nature of it? 鉴于Node.js的异步特性,我该如何现实地实现它?

Are there any well known libraries used for this? 是否有为此使用的知名库?

在此处输入图片说明

With pure core module (may be a bit ugly, but efficient): 使用纯核心模块(可能有点难看,但有效):

var http = require('http');

http.createServer(function (request, response) {
    if (request.headers.host === 'api.test') {
        // request data from 172.17.0.1:80
    } else if (request.headers.host === 'test') {
        // request data from 172.17.0.2:80
    } else {
        // Do something else
    }
}).listen(80);

If you don't like this example, you can try: https://www.npmjs.org/package/turtle.io 如果您不喜欢此示例,则可以尝试: https : //www.npmjs.org/package/turtle.io

I've managed this using node-http-proxy , where http://first.test/ and http://second.test/ are the hostnames. 我使用node-http-proxy来管理它,其中http://first.test/http://second.test/是主机名。

var http = require('http'),
    httpProxy = require('http-proxy');

var proxy = httpProxy.createProxyServer({});

// reverse proxy server
http.createServer(function (req, res) {
    var target = '';

    if (req.headers.host.match(/first.test/)) {
        target = 'http://127.0.0.1:8001';
    } else if (req.headers.host.match(/second.test/)) {
        target = 'http://127.0.0.1:8002';
    }

    console.log(req.headers.host, '->', target);
    proxy.web(req, res, { target: target });
}).listen(8000);

// test server 1
http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.write('8001\n');
    res.write(JSON.stringify(req.headers, true, 2));
    res.end();
}).listen(8001);

// test server 2
http.createServer(function(req, res) {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.write('8002\n');
    res.write(JSON.stringify(req.headers, true, 2));
    res.end();
}).listen(8002);

For a simple reverse proxy that uses the reactor pattern (like node), I would check out nginx. 对于使用反应堆模式(例如节点)的简单反向代理,我将签出nginx。 But, you mentioned you wanted to add in some custom functionality using node, so is that a realistic goal? 但是,您提到您想使用节点添加一些自定义功能,所以这是一个现实的目标吗? Absolutely! 绝对! Here are some things to think about when you are designing your reverse proxy: 在设计反向代理时,需要考虑以下几点:

  • How will you keep track of where incoming requests need to end up? 您将如何跟踪传入请求在何处结束? For example, if you proxy all request with /test/* to your UI, but the returned HTML has root relative URLs (/imgs/banner.jpg), how do you keep track of where the subsequent request needs to go (especially if it comes from javascript)? 例如,如果将所有带有/ test / *的请求代理到您的UI,但是返回的HTML具有根相对URL(/imgs/banner.jpg),那么如何跟踪后续请求的去向(特别是如果它来自javascript)? Are you going to tightly couple your proxy and your back end applications? 您是否要紧密耦合代理和后端应用程序? Or you might consider setting a cookie to keep track. 或者,您可以考虑设置cookie以保持跟踪。
  • Does this thing need to scale at all? 这个东西根本不需要扩展吗? If your answer is no, my follow up is - are you sure? 如果您的回答为“否”,那么我的后续行动是-您确定吗? If you really just need to proxy to two backend applications, I'm sure there are any number of clever ways to achieve that. 如果您真的只需要代理两个后端应用程序,那么我相信可以通过多种巧妙的方法来实现。 If at any time you may have N back end applications, then you need a solid plan for managing (add/remove/update) them on the proxy. 如果任何时候您可能有N个后端应用程序,那么您需要一个可靠的计划来在代理上管理(添加/删除/更新)它们。
  • Do your applications use HTTPS? 您的应用程序使用HTTPS吗? If so, are you going to terminate SSL on the proxy? 如果是这样,您是否要终止代理上的SSL? Can you send data in the clear between your proxy and your back end applications? 您可以在代理和后端应用程序之间明文发送数据吗?

Good luck on your reverse proxy endeavors! 祝您反向代理事业顺利! I will update this if anything else occurs to me. 如果我有其他事情发生,我将进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM