Using Nodejs for reverse proxy

Source: Internet
Author: User
Tags web hosting

In the actual engineering development, there will be a need for front-end separation.

In order to smooth the completion of front-end requests to the back-end of the individual services, a middleware to implement the request forwarding function, using Nginx can be implemented, here, using Nodejs to implement a reverse proxy server.

The actual front-end project background is node+express doing front-end routing, providing the base rendering and request forwarding of the page.

The backend uses Java springboot to develop multiple microservices (there is no use of spring cloud Eureka for service management and API orchestration), with each service IP consistent and port inconsistent.

Experimental environment: Nodejs+express Port is 3001, start a Java service, port is 8088, add a filter in Java, to output the requested address received, use postman around the client to initiate the request

@Override public void DoFilter (ServletRequest req, servletresponse Res, Filterchain chain) throws IOException, Servlete        xception {HttpServletRequest HttpRequest = (httpservletrequest) req; SYSTEM.OUT.PRINTLN ("Request address is" + ((httpservletrequest) req). Getrequesturi ());

First install "Http-proxy-middleware" middleware under the original Express project

NPM Install--save-dev Http-proxy-middleware

Quoting in Express's App.js

var proxy = require (' Http-proxy-middleware ');

Agent configuration According to the actual use situation

1. Forwarding all HTTP requests

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

var options = {target: ' http://localhost:8088 ',//target host changeorigin:true,//requires a virtual host site};  var exampleproxy = proxy (options); Turn on the proxy function and load the configuration app.use ('/', exampleproxy);//Forwarding all requests for address '/'

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

Test: Initiate any request to 127.0.0.1:3001 to see what happens to Java-side reception

Request URL Service Access Input Results
127.0.0.1:3001/ Request Address Yes/
127.0.0.1:3001/test The request address is/test
127.0.0.1:3002/test Do not forward

2. Forwarding the request for the specified path

App.use ('/api ', exampleproxy);

Test:

Request URL Service Access Input Results
127.0.0.1:3001/api/test The request address is/api/test
127.0.0.1:3001/test Do not forward
127.0.0.1:3001/api The request address is/API
127.0.0.1:3002/test Do not forward

3. Redirect the specified path rule

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

var options = {        target:  ' http://localhost : 8088 ', //  target host         changeOrigin: true,                //  need a web hosting site          ws: true,                          //  Whether the agent websocket        pathrewrite: {              ' ^/api/old-path '  :  '/api/new-path ',                   ' ^/api/remove/path '  :   '/path ',                         ' ^/api/auth/login ': '/path '         }    }; Var exampleproxy = proxy (options);   //turn on proxy function and load configuration app.use ('/api ',  exampleproxy) ;//forwarding of all requests with address '/'

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

Test:

Request URL Service Access Input Results
127.0.0.1:3001/api/old-path The request address is/api/new-path
127.0.0.1:3001/api/remove/path The request address is/path
127.0.0.1:3001/api/auth/login The request address is/path
127.0.0.1:3001/api/test The request address is/api/test
127.0.0.1:3001/test Do not forward

4. Route redirection for specified rules

It is simple to understand that, to join the current I started 2 and more Java services, the port is 8088,8089, but the front-end requests are pointing to 127.0.0.1:3001, the agent needs to be based on the actual front-end request, after resolving the path, distributed to different ports (8088, 8089) in the Java service

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

var options = {target: ' http://localhost:8089 ',//Here The default forwarding target is 127.0.0.1:8089 router: {'/rest ': ' http://localhost:8088 ',//If the request path is/api/rest, redirect the request route of the URL ' 127.0.0.1:3001/api/8003 ': ' http://localhost:8003 ',/  /service This URL is redirected}};var exampleproxy = proxy (options); Turn on the proxy function and load the configuration app.use ('/api ', exampleproxy);//Forwarding all requests for address '/'

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

Test:

TD rowspan= "1" colspan= "1" > 8089: Request address is/api
request URL    < /td> service connection input result
127.0.0.1:3001/api/rest 8088: Request address is/api/rest
127.0.0.1:3002/api /rest no response
127.0.0.1:3001/api 8088: Request address is/api
127.0.0.1:3001/api/8003 Forwarding failed (because we currently do not have a 8003 port service)
127.0.0.1:3001/api/rest/3232 8088: The request address is/api/rest/3232
127.0.0.1:3001/api

It is important to note that the proxy defaults to all requests under/API, forwarding to port 8089 services, exception handling for configurations in router, and tool rules forwarding to 8088 services or 8003 services

Summarize:

In the actual project, it is recommended to use a third case, through the "/api" and other wildcard characters to distinguish all requests to be forwarded and the normal HTTP page rendering requests. Then, according to the actual background service interface, we can configure different router rules.


Using Nodejs for 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.