In many scenarios nowadays, I apply for a VPS host to host and run a web project. I am no exception. I bought a small Win 03 VPS to use. During the use process, we face a problem, that is, the same type of server environment is fine - but if three types of server projects coexist with one PHP, one ASP, and one JSP, how should we allocate the only port 80? Because commercial WWW websites often can only occupy port 80. Of course, if you only do services, such as interfaces, using other ports will not conflict with port 80. Many developers face the problem of port 80, and the actual situation will be limited by cost. Because buying a VPS for a project alone is not economical, not cost-effective, and it is not convenient to manage. Therefore, we should carefully consider how to distribute it to multiple servers while providing a port 80, allowing different hosts to execute their respective web projects.
Dear, can we say this requirement be realized? Yes, this is not a "magic technology" or a complex technology. I wonder if you have any understanding of the "Reverse Proxy" in network services, one of the functions is to complete the distribution of ports. We might as well use the domain name as a route to distribute: Any requested by the AA.com domain name will be distributed to PHP port 82 for execution; Any requested by the BB.com domain name will be distributed to ASP port 83 for execution; ... and so on. Of course, the port here is just for instructions. You can configure it at will. Anyway, the request received from port 80 will be processed first and then distributed. Reverse proxy, in layman's terms, is just turning left hand into right hand.
Whenever the reverse proxy is mentioned, people usually think of Nginx, but today we ignore the famous Nginx for the time being and use Nodejs, a server-side brother that also uses single-threaded and event loops. First of all, Node uses JS for server programming, rather than Nginx to write configuration or Lua, which is more in line with my taste. Secondly, I am more familiar with Node, and it is easier to configure in all aspects.
The node-http-proxy package that completes this function. 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');// Create a new proxy Proxy Server object var proxy = httpProxy.createProxyServer({});// Catch exception 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.');});// Create another HTTP 80 The port server, which is the method of creating an HTTP server in regular Node. // In each request, call the proxy.web(req, res config) method to request distribution 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);If you talk about the cost of using a proxy server, it may be that it will consume more CPU operations than without consuming more resources.
Usage problem: Cannot specify folder proxy.web(req, res, { target: 'http://VeVB.COM:81/foo/' });