Practical experience of implementing nginx to forward requests based on URL

Practical experience of implementing nginx to forward requests based on URL

Preface

Because this is a distributed file system that has been deployed externally on the intranet, using fastdfs. The application is deployed to port 8088, and its backend management system is deployed on port 8089 (on the same intranet server). The service of the backend management system needs to request the fastdfs service, which can be directly requested (resources of the same server). However, we have only externalized the backend management system (Springboot project) and can only access the backend management system without using the IP allocated by the public network. To directly access the interface of the fastdfs service, you must connect to the server public network, but this limits our users' access (I can't go to your server every time to access it, I think I can access it from other places)

Proxy server: SockerServer listens to a port, connects to the specified server port according to the http message, and makes data requests
- HTTP Proxy
The http request passes through the proxy server, and the proxy server is only responsible for forwarding the corresponding http response body.
- HTTPS Proxy
When an https request passes through the proxy server, a CONNECT message will be sent to establish a tunnel with the proxy server. If the proxy server returns HTTP 200, the tunnel is established successfully. The subsequent proxy server only needs to be responsible for forwarding data. In fact, the SSL/TLS handshake still occurs between the client and the real server.

ProxyServlet

Because the backend project port 8089 can access the server fastdfs service, my first thought is to use Springboot's ProxyServlet to proxy the specified request to the server port 8088

The main Servlet of Spring boot is the DispatcherServlet of SpringMVC, and its default url-pattern is "/". If we want to add different calls (other server interfaces) to a certain url, we need to create a new proxy servlet, which will use ServletRegistrationBean, create a new ProxyServlet to handle the listening and data sending of different ports, and register it with the servletContext managed by springboot (set the specified server and port, and request the interface for forwarding)

rely

<dependency>
 <groupId>org.mitre.dsmiley.httpproxy</groupId>
 <artifactId>smiley-http-proxy-servlet</artifactId>
 <version>1.7</version>
 </dependency>

Configuration

### Configure the proxy# When requesting resource, the proxy forwards to port 8088 in the project proxy.test.servlet_url_one= /resource/*
proxy.test.target_url_one= https://localhost:8088
@Component
@Data
public class ProxyFilterServlet {
 @Value("${proxy.test.target_url_one}")
 private String targetUrl;
 @Value("${proxy.test.servlet_url_one}")
 private String servletUrl;
}

Change config to add

@Configuration
public class ProxyServletConfig {
 @Autowired
 private ProxyFilterServlet proxyFilterServlet;
 //Multiple proxy servlets can configure multiple beans
 @Bean
 public ServletRegistrationBean servletRegistrationBean(){
 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), proxyFilterServlet.getServletUrl());
 //This setName must be set, and when there are multiple, the names need to be different servletRegistrationBean.setName("go_backend");
 servletRegistrationBean.addInitParameter("targetUri", proxyFilterServlet.getTargetUrl());
 servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
 return servletRegistrationBean;
 }
}

Establish a connection with the target server through the servlet container. After all, it is not as powerful as a professional proxy server like nginx.

nginx—Proxy forwarding

  • At this time, I thought of adding a layer of nginx between the servers to forward different service requests to different port APIs for processing.

Transfer the external network request to the same server intranet port

server {
 listen 80;
 server_name 127.0.0.1;
 
 location / {
 proxy_pass http://127.0.0.1:3000;
 }
 
 location ~ /api/ {
 proxy_pass http://172.30.1.123:8081;
 }
}

Reference blog post:

https://www.jb51.net/article/174382.htm

https://www.jb51.net/article/174383.htm

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • How to use nginx to intercept specified URL requests through regular expressions
  • How to use nginx to block a specified interface (URL)
  • Nginx dynamically forwards to upstream according to the path in the URL
  • Introduction to the difference between adding or not adding / after the URL in nginx proxy_pass reverse proxy configuration
  • Solution to the problem of not being able to use Chinese URLs under Nginx
  • Detailed explanation of nginx configuration URL redirection-reverse proxy
  • Detailed explanation of nginx rewrite and location according to URL parameters
  • How to set up URL link in Nginx server

<<:  Will mysql's in invalidate the index?

>>:  Detailed explanation of the implementation principles of call, apply, and bind in JavaScript

Recommend

Three principles of efficient navigation design that web designers must know

Designing navigation for a website is like laying...

Vue3+TypeScript implements a complete example of a recursive menu component

Table of contents Preface need accomplish First R...

How to set a fixed IP in Linux (tested and effective)

First, open the virtual machine Open xshell5 to c...

Unzipped version of MYSQL installation and encountered errors and solutions

1 Installation Download the corresponding unzippe...

In-depth explanation of environment variables and configuration files in CentOS

Preface The CentOS environment variable configura...

JS Decorator Pattern and TypeScript Decorators

Table of contents Introduction to the Decorator P...

React Router V6 Updates

Table of contents ReactRouterV6 Changes 1. <Sw...

Solve the problem of spring boot + jar packaging deployment tomcat 404 error

1. Spring boot does not support jsp jar package, ...

Use of Linux sed command

1. Function Introduction sed (Stream EDitor) is a...

Vue2.x configures routing navigation guards to implement user login and exit

Table of contents Preface 1. Configure routing na...

Implementation code for operating mysql database in golang

Preface Golang provides the database/sql package ...

Solution to Ubuntu 20.04 Firefox cannot play videos (missing flash plug-in)

1. Flash plug-in package download address: https:...

Docker memory monitoring and stress testing methods

The Docker container that has been running shows ...

Solution to BT Baota Panel php7.3 and php7.4 not supporting ZipArchive

The solution to the problem that the PHP7.3 versi...