Nowadays, in many scenarios, people apply for a VPS host to host and run web projects. My brother and I are no exception. I bought a small Win 03 VPS and use it. In the process of using it, I faced a problem, that is, the same type of server environment is fine - but if three types of server projects, namely a PHP, an ASP, and a JSP, coexist, how to allocate a unique port 80? Woolen cloth? Because commercial WWW websites can often only occupy port 80 - of course, if they are only providing services, such as interfaces, using other ports will not conflict with port 80. Many developers will face the problem of port 80, and the actual situation will be limited by cost. Because it is not economical, cost-effective, and inconvenient to manage a VPS just for a project. Therefore, we should carefully consider how to distribute it to multiple servers while providing a port 80, so that different hosts can execute their own Web projects.
Dear, do we think this requirement can be realized? Yes, this is not a "magical technology", nor is it a complicated technology. I wonder if you know that one of the functions of "Reverse Proxy" in network services is to complete port distribution. We might as well use the domain name as the route for distribution: all requests for the AA.com domain name are distributed to PHP port 82 for execution; all requests for the BB.com domain name are distributed to ASP port 83 for execution;... and so on. Of course, the ports here are only for explanation. You can configure them arbitrarily. Anyway, the requests received from port 80 will be processed once and then distributed. Reverse proxy, in layman's terms, is just turning the left hand into the right hand.
Whenever people mention reverse proxy, they usually think of Nginx, but today we will temporarily ignore the famous Nginx and use Nodejs, the server-side younger brother who also uses a single-thread and event loop to achieve it. First of all, Node uses JS for server-side programming instead of Nginx or Lua to write configurations, which is more in line with my taste. Secondly, I am also familiar with Node, so it is easier to configure various aspects.
To accomplish this function is the node-http-proxy package. To download and install, please type:
npm install http-proxy
After the installation is complete, create a new proxy.js file and enter:
var http = require('http'), httpProxy = require('http-proxy'); // 新建一个代理 Proxy Server 对象 var proxy = httpProxy.createProxyServer({}); // 捕获异常 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.'); }); // 另外新建一个 HTTP 80 端口的服务器,也就是常规 Node 创建 HTTP 服务器的方法。 // 在每次请求中,调用 proxy.web(req, res config) 方法进行请求分发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 may be that it consumes more resources and CPU operations than not using it.
Usage problem: Folder cannot be specified proxy.web(req, res, { target: 'http://jb51.net:81/foo/' });