Nginx dynamically forwards to upstream according to the path in the URL

Nginx dynamically forwards to upstream according to the path in the URL

In Nginx, there are some advanced scenarios where you need to dynamically forward to an unreachable upstream based on the path parameter in the URL.

Scenario 1

/svr1/xxxx?yyy forwards to svr1:8080/xxxx?yyy

/svr2/xxxx?yyy forwards to svr2:8080/xxxx?yyy

The configuration is as follows:

location ~* /(srv[1-9]+)/(.*)$ {
  allow all;
  proxy_pass http://$1/$2$is_args$args; 
  proxy_set_header Host $host;                         
  proxy_set_header x-forwarded-for $forwarded_addr;                                
}

upstream srv1 {                                                   
   server srv1-ip:8080;
}
      
upstream srv2 {                                                   
   server srv2-ip:8080;
}

Scenario 2

There are 3 peer services srv1, 2, and 3 under svc1. /svc1/xxxx?yyy is forwarded to srv1/2/3:8080/xxxx?yyy

There are 3 peer services srv4, 5, and 6 under svc2. /svc2/xxxx?yyy is forwarded to svr4/5/6:8080/xxxx?yyy

location ~* /(svc[1-9]+)/(.*)$ {                             
  allow all;                                  
  proxy_pass http://$1/$1/$2$is_args$args;                   
  proxy_set_header Host $host;
  proxy_set_header x-forwarded-for $forwarded_addr;                   
}

upstream svc1 {
  server srv1:8080;   
  server srv2:8080;   
  server srv3:8080;                
}

upstream svc2 {
  server srv3:8080;   
  server srv4:8080;   
  server srv5:8080;                
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to use nginx to intercept specified URL requests through regular expressions
  • How to use nginx to block a specified interface (URL)
  • Practical experience of implementing nginx to forward requests based on URL
  • Introduction to the difference between adding or not adding / after the URL in nginx proxy_pass reverse proxy configuration
  • Solution to the problem of not being able to use Chinese URLs under Nginx
  • Detailed explanation of nginx configuration URL redirection-reverse proxy
  • Detailed explanation of nginx rewrite and location according to URL parameters
  • How to set up URL link in Nginx server

<<:  WeChat applet calculator example

>>:  Standard summary for analyzing the performance of a SQL statement

Recommend

Nginx external network access intranet site configuration operation

background: The site is separated from the front ...

Detailed explanation of the watch listener example in vue3.0

Table of contents Preface The difference between ...

Several ways to set the expiration time of localStorage

Table of contents Problem Description 1. Basic so...

Detailed explanation of a method to rename procedure in MYSQL

Recently I have used the function of renaming sto...

React internationalization react-intl usage

How to achieve internationalization in React? The...

Vue uses rules to implement form field validation

There are many ways to write and validate form fi...

How to solve the problem of ERROR 2003 (HY000) when starting mysql

1. Problem Description When starting MYSQL, a pro...

How to use positioning to center elements (web page layout tips)

How to center an element in the browser window He...

Implementation of IP address configuration in Centos7.5

1. Before configuring the IP address, first use i...

A detailed account of the process of climbing a pit of Docker deployment service

First time writing. Allow me to introduce myself....

Example of using store in vue3 to record scroll position

Table of contents Overall Effect Listen for conta...

A brief talk about the diff algorithm in Vue

Table of contents Overview Virtual Dom principle ...

What to do if you forget your Linux/Mac MySQL password

What to do if you forget your Linux/Mac MySQL pas...