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 in Action: Examples and Real-World ApplicationsNGINX in Action: Examples and Real-World ApplicationsApr 17, 2025 am 12:18 AM

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINX Unit: Supporting Different Programming LanguagesNGINX Unit: Supporting Different Programming LanguagesApr 16, 2025 am 12:15 AM

NGINXUnit supports multiple programming languages ​​and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.

Choosing Between NGINX and Apache: The Right Fit for Your NeedsChoosing Between NGINX and Apache: The Right Fit for Your NeedsApr 15, 2025 am 12:04 AM

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

How to start nginxHow to start nginxApr 14, 2025 pm 01:06 PM

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

How to check whether nginx is startedHow to check whether nginx is startedApr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to close nginxHow to close nginxApr 14, 2025 pm 01:00 PM

To shut down the Nginx service, follow these steps: Determine the installation type: Red Hat/CentOS (systemctl status nginx) or Debian/Ubuntu (service nginx status) Stop the service: Red Hat/CentOS (systemctl stop nginx) or Debian/Ubuntu (service nginx stop) Disable automatic startup (optional): Red Hat/CentOS (systemctl disabled nginx) or Debian/Ubuntu (syst

How to configure nginx in WindowsHow to configure nginx in WindowsApr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to solve nginx403 errorHow to solve nginx403 errorApr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools