An nginx implementation experiences forwarding requests based on urls

The application is deployed on port 8088 using FastDFS, and the backend management system is deployed on port 8089 (on the same Intranet server). The backend management system can request fastDFS service directly (on the same server). However, now we only have access to the background management system (Springboot project), and we can only access the background management system without IP allocated through the public network. If we want to directly access the fastdFS service interface, we must connect to the server public network, but this limits the access of our users (I cannot visit your server every time. I think it can be accessed elsewhere)

Proxy server: SockerServer listens on a port, connects to a specified server port based on HTTP packets, and requests data

– HTTP proxy

The HTTP request passes through the proxy server, which simply forwards the corresponding HTTP response body.

– the HTTPS proxy

If the proxy server returns HTTP 200, the establishment is successful. The proxy server only needs to forward the data. In fact, the SSL/TLS handshake still takes place between the client and the real server.

ProxyServlet

  • Since port 8089 of the backend project has access to the server fastdFS service, the first thing that came to mind was SpringbootProxyServletProxies the specified request to port 8088 of the server

Spring Boot’s main Servlet is SpringMVC’s DispatcherServlet, whose default URL-pattern is “/”. If we want to add different calls (other server interfaces) for a URL, we need to create a new proxy Servlet. The ServletRegistrationBean is used, a new ProxyServlet is created to handle listening on different ports and sending data, and it is registered with the SpringBoot managed servletContext (set the specified server and port, request forwarding interface).

Rely on

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

configuration

Servlet_url_one = /resource/* proxy.test.target_url_one= https://localhost:8088Copy the code

@Component
@Data
public class ProxyFilterServlet {
    @Value("${proxy.test.target_url_one}")
    private String targetUrl;
    @Value("${proxy.test.servlet_url_one}")
    private String servletUrl;
}
Copy the code

Change config add

@Configuration public class ProxyServletConfig { @Autowired private ProxyFilterServlet proxyFilterServlet; Public ServletRegistrationBean ServletRegistrationBean (){ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), proxyFilterServlet.getServletUrl()); / / the elegantly-named setName must be set up, and multiple, name need not servletRegistrationBean elegantly-named setName (" go_backend "); servletRegistrationBean.addInitParameter("targetUri", proxyFilterServlet.getTargetUrl()); servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false"); return servletRegistrationBean; }}Copy the code

Establishing a connection to a target server through a servlet container is not as powerful as a professional proxy server like Nginx

Nginx – Proxy forwarding

  • At this point I came up with the idea of adding a layer of Nginx between servers to forward different service requests to different port apis for processing

Forwards requests from the Internet to the Intranet port of the same server

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; }}Copy the code

Refer to blog post:

https://blog.csdn.net/wohaqiyi/article/details/81327512

https://juejin.cn/post/6844903473163550734