Abandon Nginx and use nodejs as the reverse proxy server _ node. js

Source: Internet
Author: User
Tags vps
Whenever we mention the reverse proxy, we usually think of Nginx, but today we temporarily ignore the well-known Nginx, using Nodejs, also a single-threaded, event-loop server brother. In many scenarios, I have applied for a VPS host to host and run Web projects. I bought a small Win 03 VPS instance. In the process of use, there is a problem, that is, the server environment of the same type is okay-but if three types of server projects exist in one PHP, one ASP, and one JSP, how do I allocate a unique port 80? For commercial WWW websites, only port 80 is used.-Of course, if you only use services, such as interfaces, other ports will not conflict with port 80. Many developers face port 80 and the actual cost is limited. It is neither economical nor cost-effective to buy a VPS for a single project, nor inconvenient to manage. Therefore, we should consider how to distribute data to multiple servers when a port 80 is provided, so that different hosts can execute their respective Web projects.

Dear user, can we achieve this requirement? Yes, this is not a "Magic technology" or a complex technology. I wonder if you have any knowledge about the "Reverse Proxy" in network services. One of the functions is to distribute ports. We may wish to use domain names for Route distribution: requests for AA.com domain names are distributed to port 82 of PHP for execution. Requests for BB.com domain names are distributed to port 83 of ASP for execution ;...... And so on. Of course, the port here is only described and used. You can configure it as needed. It is the request received from port 80, which is processed first and then distributed. Reverse Proxy, in other words, is to turn the left hand to the right hand.

Whenever we mention the reverse proxy, we usually think of Nginx, but today we temporarily ignore the well-known Nginx, using Nodejs, also a single-threaded, event-loop server brother. First, Node uses JS for server-side programming, instead of Nginx for configuration writing or Lua, which is more in line with my taste. Second, I am familiar with Node, and I am more comfortable with configuration.

The node-http-proxy package is used to complete this function. For download and installation, enter:

npm install http-proxy

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

Var http = require ('http'), httpProxy = require ('HTTP-proxy'); // create a proxy Proxy Server Object var proxy = httpProxy. createProxyServer ({}); // capture abnormal 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 an HTTP port 80 server, that is, the common method for creating an HTTP server for Node. // Call proxy in each request. create your m 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 it is said that the cost of using the proxy server may be more than the need to consume more resources, consuming more CPU operations.

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

Related Article

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.