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

Nginx implements the actual combat experience of forwarding requests based on URL

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Preface

Because it takes a long time to complete the internal and external deployment of a distributed file system, using fastdfs, the application is deployed to port 8088, and its background management system is deployed on port 8089 (the same internal network server). The service of the background management system requests fastdfs services and can directly request (resources from the same server), but now we only wear the background management system (Springboot project). The IP that is not assigned through the public network can only access the backend management system. If you want to access the interface of the fastdfs service directly, you must connect to the server public network, but this restricts the access of our users (I can't go to your server every time. I think I can also access it elsewhere).

Proxy server: SockerServer listens to a port, connects to a designated server port according to http messages, and makes data requests

-HTTP Agent

The http request passes through the proxy server, which is only responsible for forwarding the corresponding http response body.

-HTTPS Agent

When the https request passes through the proxy server, a CONNECT message is sent to establish a tunnel with the proxy server. If the proxy server returns HTTP 200, the establishment is successful, and the subsequent proxy server is only responsible for forwarding the data. In fact, the SSL/TLS handshake still occurs between the client and the real server.

ProxyServlet

Because the background project port 8089 can access the server fastdfs service, the first thing that comes to mind is to use Springboot's ProxyServlet to proxy the specified request to the server port 8088

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

Dependence

Org.mitre.dsmiley.httpproxy smiley-http-proxy-servlet 1.7

Configuration

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

Change config add

@ Configurationpublic class ProxyServletConfig {@ Autowired private ProxyFilterServlet proxyFilterServlet; / / multiple proxy servlet can be configured with multiple bean @ Bean public ServletRegistrationBean servletRegistrationBean () {ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean (new ProxyServlet (), proxyFilterServlet.getServletUrl ()); / / this setName must be set, and when there are multiple, the name needs to be different servletRegistrationBean.setName ("go_backend"); servletRegistrationBean.addInitParameter ("targetUri", proxyFilterServlet.getTargetUrl ()); servletRegistrationBean.addInitParameter (ProxyServlet.P_LOG, "false"); return servletRegistrationBean;}}

Establishing a connection to the target server through the servlet container is not as strong as a professional proxy server like nginx.

Nginx- proxy forwarding

At this point, I thought of adding a layer of nginx between servers to forward different service requests to different ports api for processing.

Transfer the request from the external network to the private network 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;}}

Refer to blog posts:

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

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

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.

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

Servers

Wechat

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

12
Report