Home > Operation and Maintenance > Nginx > How nginx forwards requests based on URL

How nginx forwards requests based on URL

WBOY
Release: 2023-05-28 14:40:06
forward
2563 people have browsed it

Proxy server: sockerserver listens to a certain port, connects to the specified server port according to the http message, and makes data requests
-http proxy
http requests pass through the proxy server, and the proxy server is only responsible for forwarding the corresponding The http response body is fine.
- https proxy
When the 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 establishment is successful. Subsequent proxy servers only need to be responsible for forwarding data. In fact, the SSL/TLS handshake still occurs between the client and the real server.

proxyservlet

Because the 8089 port of the background project can access the server fastdfs service, the first thing I thought of was 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. Its default url-pattern is "/". If we want to add different calls (other server interfaces) to a certain url, You need to create a new proxy servlet. You will use servletregistrationbean to create a new proxyservlet to handle the monitoring of different ports and data sending, and register it in the servletcontext managed by springboot (set the specified server and port, and the interface for request forwarding. )

##Dependency

<dependency>
 <groupid>org.mitre.dsmiley.httpproxy</groupid>
 <artifactid>smiley-http-proxy-servlet</artifactid>
 <version>1.7</version>
 </dependency>
Copy after login

Configuration

### 配置代理
#请求resource时代理转发到端口8088项目中
proxy.test.servlet_url_one= /resource/*
proxy.test.target_url_one= https://localhost:8088
Copy after login
@component
@data
public class proxyfilterservlet {
 @value("${proxy.test.target_url_one}")
 private string targeturl;
 @value("${proxy.test.servlet_url_one}")
 private string servleturl;
}
Copy after login

Change config add

@configuration
public class proxyservletconfig {
 @autowired
 private proxyfilterservlet proxyfilterservlet;
 //多个代理servlet可以配置多个bean
 @bean
 public servletregistrationbean servletregistrationbean(){
 servletregistrationbean servletregistrationbean = new servletregistrationbean(new proxyservlet(), proxyfilterservlet.getservleturl());
 //这个setname必须要设置,并且多个的时候,名字需要不一样
 servletregistrationbean.setname("go_backend");
 servletregistrationbean.addinitparameter("targeturi", proxyfilterservlet.gettargeturl());
 servletregistrationbean.addinitparameter(proxyservlet.p_log, "false");
 return servletregistrationbean;
 }
}
Copy after login

Establish a connection with the target server through the servlet container. After all, there is no 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

Put the required requests Requests from the external network are forwarded to the same server's internal network 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;
 }
}
Copy after login

The above is the detailed content of How nginx forwards requests based on URL. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template