Nodejs Implementing a reverse Proxy

Source: Internet
Author: User

To talk about reverse proxy, many people think of the first time is Nginx, yes, in this field to do the best, I think it should be nginx, why do you want to use Nodejs to achieve? In a kind of research mentality and let the reverse proxy better for their own service, after all, to modify Nginx also need C C + + and other knowledge, and I am more familiar with Nodejs a little (plainly is not understand C + + ...).


Speaking of reverse proxy, that first to understand what is a reverse proxy, we generally use a proxy is that I want to visit a website, but direct access, and then through the proxy server access, and then the proxy server to return information to me, the agent of this behavior occurs on the client. Reverse proxy is just the opposite, the Proxy service received various requests on various networks (such as my access URL), and then based on the request to find the internal server, and then return the information to the requesting party (me), the agent's behavior occurs on the server side. In fact, the two scenarios are different, the forward proxy is usually we want to visit a foreign site, but direct access to the proxy server to achieve, and reverse proxy is usually used to manage the internal multiple servers, to achieve load balancing.


Then use the direction of the agent will have to say that the virtual path, medium and large-scale site static resources are separated out, if through such servers like Apache/tomcat/iis to access static resources, then the efficiency will be reduced a lot, the best way is to read directly, so the use of the virtual path, The role of the virtual path is that when you visit A.com/res/index.js, you point directly to the res/in the server directory Index.js, directly to the hard disk IO read and write, bypassing the dynamic server parsing, then the implementation of reverse proxy is from the static resource separation began, after all, the front-end is the most concerned about their own static resource territory.


Then through the above analysis, we can be implemented through the Http-proxy module in Nodejs, before using this is to do some of the request type of judgment, that is, the request js/css/jpg such files, the return of the response information type is what, So with the following code (also I wrote a content-type module) Content-type.js:

/*content Typeby subying [email protected] According to suffix conversion content-type*/var getcontenttype = function (ext) {var contentType = '; SW Itch (EXT) {case ". html": contenttype= "text/html"; break;case ". js": contenttype= "Text/javascript"; break;case ". css": Contenttype= "Text/css"; break;case ". gif": contenttype= "image/gif"; break;case ". jpg": contenttype= "Image/jpeg"; Break;case ". png": contenttype= "image/png"; break;case ". ico": contenttype= "Image/icon"; break;default:contenttype= " Application/octet-stream ";} return contentType;}; Module.exports = getContentType;




     with this you need to use a module inside the Nodejs: The HTTP-PROXY,NPM address is https://www.npmjs.org/package/http-proxy. So the previous said, the static resources are separated, will use the virtual directory, how to judge it is static resources? This is why I have to use Nodejs to implement the reverse proxy, because I can define myself freely, is a question of IF. I used to put static resources in a Res folder, so I just have to judge the request inside the '/res/' directory, I know is to request static resources. After determining that a static resource is requested, the physical path of the project, such as ' E:\teset\res ', is then read from the FS module and returned to the client. Then other requests are implemented through the other local servers. The proxy method used in this example is Http-proxy Web method, Http-proxy more usage can go to refer to the NPM address above.

/*http-proxy */var http = require (' http '), Httpproxy = require (' Http-proxy ')//http-proxy, proxy = Httpproxy.create ProxyServer ({}), FS = require (' FS '), PATH = require (' path '), Getcontype = require ('./content-type ')//own definition according to suffix conversion Content-type;var Server = Http.createserver (function (req, res) {var _url = Req.url//Get requested Url,_file,_localpath,_ Localfile,_ext,_stream, if (_url.indexof ('/res/') >-1) {_file = _url.replace (/\?. */ig, '); _ext = Path.extname (_file); File extension//Determine if the JS file if (_ext = = = '. js ') {_file = _file.replace (' res/js/', ' res/src_js/');} Convert cost to path _localpath = ' e:/web/angularjs/'; _localfile = _localpath+_file;//determine if file exists if (Fs.existssync (_localfile)) {// If the file exists Res.writehead ($, {"Content-type": Getcontype (_ext)}), _stream = Fs.createreadstream (_localfile, {flags: "R", Encoding:null});//read-only mode reads the contents of the file _stream.on (' Error ', function () {//) if the Read error returns 404res.writehead (404, {"Content-type": "Text /html "}"); Res.end (" 



In summary, the implementation of the reverse proxy static resource separation, the first is to determine the request to determine whether the static file, if the request is static files, find the specific file under the project, directly with FS read, If you are not requesting a static file then use Proxy.web to request the internal server (mine is 127.0.0.1:81) to implement. Hope to learn Nodejs realize reverse proxy friend some help, master mock not A,, formal project or with Nginx better!

Nodejs Implementing a reverse Proxy

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.