25 Aug 2013

HTTP Proxy - node.js

An HTTP proxy with custom routing logic that conditionally forwards to different ports based upon the requested host and path name.

For our HTTP proxy we use the http-proxy module, which provides a robust proxy implementation.

Install http-proxy and url:

$ sudo npm install http-proxy url

Then define the proxy as a node.js instance (we'll name the file proxy-server.js):

var httpProxy = require("http-proxy");
var url = require("url");

httpProxy.createServer(function(req, res, proxy) {

  var hostname = req.headers.host.split(":")[0];
  var pathname = url.parse(req.url).pathname;

  // Options for the outgoing proxy request.
  var options = { host: hostname };

  // Routing logic
  if(hostname == "") {
    options.port = 8083;
  } else if(pathname == "/upload") {
    options.port = 8082;
    options.path = "/"; 
  } else {
    options.port = 8081;
  // (add more conditional blocks here)

  proxy.proxyRequest(req, res, options);


console.log("Proxy listening on port 8080");

All incoming requests on port 8080 will be received by the proxy and considered by the routing logic. The url module is used to parse and extract the pathname, which is tested along with the hostname to determine the forwarding target.

Requests with as the host name will be forwarded to localhost:8083. Requests that do not have as the host but have the path /upload are forwarded to localhost:8082. Any other request will be forwarded the port localhost:8083.

Extending the example to match against other host and path combinations should be trivial. If required the port, protocol (http or https) and other attributes such as remote IP address can be used in determining the target.

Before testing the proxy define 3 additional node instances, listening on ports 8081, 8082 and 8083. You can add this to the bottom of proxy-server.js:

// We simulate the 3 target applications

var http = require("http");

http.createServer(function(req, res) {

  res.end("Request received on 8081");


http.createServer(function(req, res) {

  res.end("Request received on 8082");


http.createServer(function(req, res) {

  res.end("Request received on 8083");


Start the proxy by running in the terminal:

$ node proxy-server.js
Proxy listening on port 8080

Now assert the routing works as expected using your browser.

This should forward to the server listening on port 8081:

Request received on 8081

The /upload path will forward to 8082:

Request received on 8082

Entering as the host name forwards to 8083:
Request received on 8083

localhost and are used for example only. In a production environment you would more likely be dealing with nice server names. You could consider adding entries to your local /etc/hosts for testing purposes, for example:    server1.com    images.server2.com    upload.server2.com