Discard Nginx Use Nodejs to do reverse proxy server _node.js

Source: Internet
Author: User
Tags vps

Nowadays many scenes, are applied for a VPS host to run the Web project, younger brother I am no exception, purchased a small Win VPS use. In the process of using, faced with a problem is that the same type of server environment is fine--but if it is a PHP, an ASP, a JSP three types of server projects coexist, how to allocate a unique 80 port? Because the commercial WWW site, often can only occupy 80 ports,-of course, if only to do the service, such as the interface and so on, the use of other ports will not conflict with the 80 port. Many developers are faced with the problem of 80 ports, and the actual situation is subject to cost constraints. Because a single project to buy a VPS, is not too economical, not too cost-effective, management is not convenient. So, we should think about how, in the case of providing a 80-port, distributed to a variety of services to the end, so that different hosts to carry out their own Web projects.

Kiss, is this demand we say can be achieved? Yes, this is not a "magic technology", nor is it a complex technique. I do not know if you know, network services in the "reverse proxy" (Reverse proxy), one of the functions is to complete the distribution of the port. We may wish to use the domain name for the route distribution: all aa.com domain name request, distributes to the PHP 82 port execution, all bb.com domain name request, distributes to the ASP 83 port execution; So the analogy. Of course, the port here is only said to use, you can configure any, anyway, from the 80 port to receive the request, first for processing, and then distributed. The reverse proxy, in layman's terms, is left hand turn right hand just.

Whenever the reverse agent, people usually think of is Nginx, but today we temporarily ignore the famous Nginx, using the same is the use of a single thread, the event cycle of the service--nodejs to achieve. First node using JS as a service-side programming, rather than Nginx write configuration or Lua, more in line with my taste mouth, and then I also more familiar with node, configuration of all aspects of what more smoothly.

The Node-http-proxy package is the one that completes the function. Download, install, type:

NPM Install Http-proxy

When the installation is complete, create a new Proxy.js file and enter:

var http = require (' http '), Httpproxy = require (' Http-proxy ');

Creates a new proxy proxy Server object var proxy = Httpproxy.createproxyserver ({});
 Catch exception Proxy.on (' Error ', function (err, req, res) {res.writehead ({' Content-type ': ' Text/plain '}); Res.end (' something went wrong.
And we are reporting a custom error message. ');

});
Create a new server with an HTTP 80 port, which is how regular Node creates the HTTP server. In each request, the Proxy.web (req, res config) method is invoked to request distribution of the Create your custom server and just call ' proxy.web () ' to proxy//a web RE Quest to the target passed in the options//also can use ' proxy.ws () ' to proxy a websockets request//var server = R Equire (' http '). Createserver (function (req, res) {//can define here your custom logic to handle the request//and T
 Hen 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") Server.listen (80);

If the cost of using a proxy server, it may be more than the consumption of resources, more CPU operation.

Usage Issue: Cannot specify folder Proxy.web (req, res, {target: ' http://jb51.net:81/foo/'});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.