search
HomeOperation and MaintenanceNginxHow nginx forwards requests based on URL

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>

Configuration

### 配置代理
#请求resource时代理转发到端口8088项目中
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 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;
 }
}

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;
 }
}

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
NGINX's Key Features: Performance, Scalability, and SecurityNGINX's Key Features: Performance, Scalability, and SecurityApr 13, 2025 am 12:09 AM

NGINX improves performance through its event-driven architecture and asynchronous processing capabilities, enhances scalability through modular design and flexible configuration, and improves security through SSL/TLS encryption and request rate limiting.

NGINX vs. Apache: Web Hosting and Traffic ManagementNGINX vs. Apache: Web Hosting and Traffic ManagementApr 12, 2025 am 12:04 AM

NGINX is suitable for high concurrency and low resource consumption scenarios, while Apache is suitable for scenarios that require complex configurations and functional extensions. 1.NGINX is known for handling large numbers of concurrent connections with high performance. 2. Apache is known for its stability and rich module support. When choosing, it must be decided based on specific needs.

NGINX: The Versatile Tool for Modern Web ApplicationsNGINX: The Versatile Tool for Modern Web ApplicationsApr 11, 2025 am 12:03 AM

NGINXisessentialformodernwebapplicationsduetoitsrolesasareverseproxy,loadbalancer,andwebserver,offeringhighperformanceandscalability.1)Itactsasareverseproxy,enhancingsecurityandperformancebycachingandloadbalancing.2)NGINXsupportsvariousloadbalancingm

Nginx SSL/TLS Configuration: Securing Your Website with HTTPSNginx SSL/TLS Configuration: Securing Your Website with HTTPSApr 10, 2025 am 09:38 AM

To ensure website security through Nginx, the following steps are required: 1. Create a basic configuration, specify the SSL certificate and private key; 2. Optimize the configuration, enable HTTP/2 and OCSPStapling; 3. Debug common errors, such as certificate path and encryption suite issues; 4. Application performance optimization suggestions, such as using Let'sEncrypt and session multiplexing.

Nginx Interview Questions: Ace Your DevOps/System Admin InterviewNginx Interview Questions: Ace Your DevOps/System Admin InterviewApr 09, 2025 am 12:14 AM

Nginx is a high-performance HTTP and reverse proxy server that is good at handling high concurrent connections. 1) Basic configuration: listen to the port and provide static file services. 2) Advanced configuration: implement reverse proxy and load balancing. 3) Debugging skills: Check the error log and test the configuration file. 4) Performance optimization: Enable Gzip compression and adjust cache policies.

Nginx Caching Techniques: Improving Website PerformanceNginx Caching Techniques: Improving Website PerformanceApr 08, 2025 am 12:18 AM

Nginx cache can significantly improve website performance through the following steps: 1) Define the cache area and set the cache path; 2) Configure the cache validity period; 3) Set different cache policies according to different content; 4) Optimize cache storage and load balancing; 5) Monitor and debug cache effects. Through these methods, Nginx cache can reduce back-end server pressure, improve response speed and user experience.

Nginx with Docker: Deploying and Scaling Containerized ApplicationsNginx with Docker: Deploying and Scaling Containerized ApplicationsApr 07, 2025 am 12:08 AM

Using DockerCompose can simplify the deployment and management of Nginx, and scaling through DockerSwarm or Kubernetes is a common practice. 1) Use DockerCompose to define and run Nginx containers, 2) implement cluster management and automatic scaling through DockerSwarm or Kubernetes.

Advanced Nginx Configuration: Mastering Server Blocks & Reverse ProxyAdvanced Nginx Configuration: Mastering Server Blocks & Reverse ProxyApr 06, 2025 am 12:05 AM

The advanced configuration of Nginx can be implemented through server blocks and reverse proxy: 1. Server blocks allow multiple websites to be run in one instance, each block is configured independently. 2. The reverse proxy forwards the request to the backend server to realize load balancing and cache acceleration.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use