Discard Nginx and use nodejs as a reverse proxy server

  • 2020-03-30 03:31:16
  • OfStack

Nowadays, many scenarios apply for a VPS host to host and run Web projects, and my younger brother is no exception. I bought a small Windows 03 VPS to use. In the process of use, the problem is that the same type of server environment is fine -- but if a PHP, an ASP, a JSP three types of server projects coexist, how to allocate a unique port 80? Because commercial WWW sites tend to occupy only 80  Port, -- of course, if you're just doing services like interfaces, using other ports won't conflict with port 80. Many developers will be faced with the problem of port 80, and the actual situation will be limited by the cost. Because buying a VPS for a single project is not very economical, cost-effective, and easy to manage. Therefore, we should consider how to distribute to multiple servers and have different hosts execute their own Web projects while providing an 80 port.

Dear, so this demand we say can realize? Yes, it's not "magic technology" and it's not complicated technology. One of the functions of "Reverse Proxy" in web services is to distribute ports. We might as well use the domain name as the route distribution: AA.com domain name requests, distribution to PHP port 82 execution; All BB.com domain requests, distributed to ASP 83 port execution; ... And so on. Of course, the port here is just for illustration purposes, you can configure it any way you want, but the request received from port 80 is processed once and then distributed. A reverse proxy, in layman's terms, is a left hand turned right hand.

When people think of a reverse proxy, they usually think of Nginx, but let's ignore the famous Nginx for the moment and use the same single-threaded, event-loloop server-side cousin, Nodejs. Firstly, Node USES JS as the server-side programming, rather than Nginx to write configuration or Lua, which is more in line with my taste. Secondly, I am also familiar with Node, so it is more convenient to configure everything.

Complete the function is (link: https://github.com/nodejitsu/node-http-proxy) package. To download and install, please type:


npm install http-proxy

After the installation, create a new proxy.js file and enter:


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

//Create a new Proxy Server object
var proxy = httpProxy.createProxyServer({});

//Catch exceptions
proxy.on('error', function (err, req, res) {
 res.writeHead(500, {
 'Content-Type': 'text/plain'
 });
 res.end('Something went wrong. And we are reporting a custom error message.');
});

//In addition, create a new HTTP 80 server, which is the normal Node method of creating an HTTP server.
//In each request, call the proxy.web(req, res config) method to distribute the request Create your custom server and just call 'proxy.web()' to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
//
var server = require('http').createServer(function(req, res) {
 // You can define here your custom logic to handle the request
 // and then proxy the request.
 var host = req.url;
 host = url.parse(host); host = host.host;
 
 console.log("host:" + req.headers.host);
 console.log("client ip:" + (req.headers['x-forwarded-for'] || req.connection.remoteAddress));
 
 proxy.web(req, res, { target: 'http://localhost:8080' });
});

console.log("listening on port 80")
server.listen(80);

The cost of using a proxy server is probably more resources and more CPU than it consumes.

Usage problem: cannot specify folder proxy.web(req, res, {target: 'http://jb51.net:81/foo/'});


Related articles: