Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use Nodejs for reverse proxy

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

In the actual engineering development, there will be the need for front and rear separation.

In order to smoothly complete the front-end request to the back-end independent services, we need a middleware to achieve the request forwarding function, which can be achieved by using Nginx. Here, we use nodejs to implement a reverse proxy server.

The background of the actual frontend project is that node+express does frontend routing to provide basic rendering and request forwarding of the page.

The backend uses java springboot to develop multiple micro-services (here, spring cloud Eureka is not used for service management and API coordination). The IP of each service is the same and the port is different.

Experimental environment: nodejs+express port is 3001, start a java service, port is 8088, add a filter in java to output the received request address, and use the client around postman to initiate the request

@ Override public void doFilter (ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {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

Make a reference in the app.js of express

Var proxy = require ('http-proxy-middleware')

Configure the agent according to the actual use

1. Forward all http requests

Var options = {target: 'http://localhost:8088', / / destination host changeOrigin: true, / / need virtual host site}; var exampleProxy = proxy (options); / / enable proxy function and load configuration app.use (' /', exampleProxy); / / forward all requests with address'/'

Test: initiate any request to 127.0.0.1Flux 3001 to see how the java side receives it.

The request URL service receives the input result 127.0.0.1 test127.0.0.1:3002/test 3001 / request address is / 127.0.0.1:3001/test request address is / test127.0.0.1:3002/test is not forwarded

2. Forward the request for the specified path

App.use ('/ api', exampleProxy)

Test:

Request URL service receives input result 127.0.0.1:3001/api/test request address is / api/test127.0.0.1:3001/test do not forward 127.0.0.1:3001/api request address is / api127.0.0.1:3002/test do not forward

3. Redirect the specified path rule

Var options = {target: 'http://localhost:8088', / / destination host changeOrigin: true, / / need virtual host site ws: true, / / whether to proxy websocket pathRewrite: {' ^ / api/old-path':'/ api/new-path' '^ / api/remove/path':' / path','^ / api/auth/login':'/path'}} Var exampleProxy = proxy (options); / / enable the proxy function and load the configuration app.use ('/ api', exampleProxy); / / forward all requests with the address'/'

Test:

Request URL service receives input result 127.0.0.1:3001/api/old-path request address is / api/new-path127.0.0.1:3001/api/remove/path request address is / path127.0.0.1:3001/api/auth/login request address is / path127.0.0.1:3001/api/test request address is / api/test127.0.0.1:3001/test is not forwarded

4. Route redirection to the specified rule

It can be simply understood that, at present, I have started two or more java services with port 8088 Magi 8089, but the requests issued by the front end are all directed to 127.0.0.1 3001. The agent needs to parse the path according to the actual front end request and distribute it to different java services on different ports (808J 8089).

Var options = {target: 'http://localhost:8089', / / here the default forwarding destination is 127.0.0.1 router: {' / rest': 'http://localhost:8088',// if the request path is / api/rest Then redirect the request route of the url to '127.0.0.1url 3001 to ApiUniverse 8003:' http://localhost:8003', / / service the API is redirected}} Var exampleProxy = proxy (options); / / enable the proxy function and load the configuration app.use ('/ api', exampleProxy); / / forward all requests with the address'/'

Test:

The request URL service receives the input result 127.0.0.1:3001/api/rest8088: request address is / api/rest127.0.0.1:3002/api/rest No response 127.0.0.1:3001/api8088: request address is / api127.0.0.1:3001/api/8003 forwarding failed (because we currently do not have a service on port 8003) 127.0.0.1:3001/api/rest/32328088: request address is / api/rest/3232127.0.0.1:3001/api8089: request address is / api

It should be noted here that by default, the agent forwards all requests under / api to the service at port 8089. If an exception is taken for the configuration in router, the tool rule will be forwarded to the 8088 service or 8003 service.

Summary:

In the actual project, it is recommended to use the third case, using wildcard characters such as "/ api" to distinguish all requests to be forwarded from regular http page rendering requests. Then configure different router rules according to the actual backend service interface.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report