Total Pageviews

Thursday 3 March 2016

node的一个反向代理程序-redbird

A modern reverse proxy for node.

Redbird Reverse Proxy
with built in Cluster and Docker support.

redbird

Handling dynamic virtual hosts, load balancing, proxying web sockets and SSL encryption should be easy and robust.

With redbird you get a complete library to build dynamic reverse proxies with the speed and robustness of http-proxy.

This light-weight package includes everything you need for easy reverse routing of your applications. Great for routing many applications from different domains in one single host, handling SSL with ease, etc.

Developed by manast

BuildStatus NPM version

Features

Flexible and easy routing.
Websockets.
Seamless SSL Support (HTTPS -> HTTP proxy)
Automatic HTTP to HTTPS redirects.
Load balancer.
Register and unregister routes programatically without restart (allows zero downtime deployments)
Docker support for automatic registration of running containers
Cluster support that enables automatic multi-process.
Based on top of rock-solid node-http-proxy and battle tested on production in many sites.
Optional logging based on bunyan.
Install

npm install redbird
Example

You can programatically register or unregister routes dynamically even if the proxy is already running:

var proxy = require('redbird')({port: 80});

// OPTIONAL: Setup your proxy but disable the X-Forwarded-For header
var proxy = require('redbird')({port: 80, xfwd: false});

// Route to any global ip
proxy.register("optimalbits.com", "http://167.23.42.67:8000");

// Route to any local ip, for example from docker containers.
proxy.register("example.com", "http://172.17.42.1:8001");

// Route from hostnames as well as paths
proxy.register("example.com/static", "http://172.17.42.1:8002");
proxy.register("example.com/media", "http://172.17.42.1:8003");

// Subdomains, paths, everything just works as expected
proxy.register("abc.example.com", "http://172.17.42.4:8080");
proxy.register("abc.example.com/media", "http://172.17.42.5:8080");

// Route to any href including a target path
proxy.register("foobar.example.com", "http://172.17.42.6:8080/foobar");

// You can also enable load balancing by registering the same hostname with different
// target hosts. The requests will be evenly balanced using a Round Robin scheme.
proxy.register("balance.me", "http://172.17.40.6:8080");
proxy.register("balance.me", "http://172.17.41.6:8080");
proxy.register("balance.me", "http://172.17.42.6:8080");
proxy.register("balance.me", "http://172.17.43.6:8080");


About HTTPS

The HTTPS proxy supports virtual hosts by using SNI (which most modern browsers support: IE7 and above). The proxying is performed by hostname, so you must use the same SSL certificates for a given hostname independently of its paths.

HTTPS Example

Conceptually HTTPS is easy, but it is also easy to struggle getting it right. With redbird its straightforward, check this complete example:

1) Generate a localhost development SSL certificate:

/certs $ openssl genrsa -out dev-key.pem 1024
/certs $ openssl req -new -key dev-key.pem -out dev-csr.pem

// IMPORTANT: Do not forget to fill the field! Common Name (e.g. server FQDN or YOUR name) []:localhost

/certs $ openssl x509 -req -in dev-csr.pem -signkey dev-key.pem -out dev-cert.pem
Note: For production sites you need to buy valid SSL certificates from a trusted authority.

2) Create a simple redbird based proxy:

var redbird = new require('redbird')({
    port: 8080,

    // Specify filenames to default SSL certificates (in case SNI is not supported by the
    // user's browser)
    ssl: {
        port: 8443,
        key: "certs/dev-key.pem",
        cert: "certs/dev-cert.pem",
    }
});

// Since we will only have one https host, we dont need to specify additional certificates.
redbird.register('localhost', 'http://localhost:8082', {ssl: true});
3) Test it:

Point your browser to localhost:8000 and you will see how it automatically redirects to your https server and proxies you to your target server.

You can define many virtual hosts, each with its own SSL certificate. And if you do not define any, they will use the default one as in the example above:

redbird.register('example.com', 'http://172.60.80.2:8082', {
    ssl: {
        key: "../certs/example.key",
        cert: "../certs/example.crt",  
        ca: "../certs/example.ca"
    }
});

redbird.register('foobar.com', 'http://172.60.80.3:8082', {
    ssl: {
        key: "../certs/foobar.key",
        cert: "../certs/foobar.crt",   
    }
});
You can also specify https hosts as targets and also specify if you want the connection to the target host to be secure (default is true).

var redbird = require('redbird')({
    port: 80,
    secure: false,
    ssl: {
        port: 443,
        key: "../certs/default.key",
        cert: "../certs/default.crt",
    }
});
redbird.register('tutorial.com', 'https://172.60.80.2:8083', {
    ssl: {
        key: "../certs/tutorial.key",
        cert: "../certs/tutorial.crt",
    }
});
Docker support

If you use docker, you can tell Redbird to automatically register routes based on image names. You register your image name and then everytime a container starts from that image, it gets registered, and unregistered if the container is stopped. If you run more than one container from the same image, redbird will load balance following a round robin schema:

var redbird = require('redbird')({
  port: 8080,
});

require('redbird')
  .docker(redbird)
  .register("example.com", 'company/myimage:latest');
Cluster support

Redbird support automatic support for node cluster. Just specify in the options object the number of processes that you want redbird to use. Redbird will automatically re-start any thread thay may crash automatically, increasing even more its reliability.

var redbird = new require('redbird')({
    port: 8080,
  cluster: 4
});
NTLM support

If you need NTLM support, you can tell Redbird to add the required header handler. This registers a response handler which makes sure the NTLM auth header is properly split into two entries from http-proxy.

var redbird = new require('redbird')({
  port: 8080,
  ntlm: true
});

from https://github.com/OptimalBits/redbird