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

Detailed explanation of JS ES6 variable destructuring assignment

Table of contents 1. What is deconstruction? 2. A...

Tutorial on resetting the root password of Mac MySQL

Disclaimer: This password reset method can direct...

How to view and terminate running background programs in Linux

Linux task management - background running and te...

Three ways to implement waterfall flow layout

Preface When I was browsing Xianyu today, I notic...

Docker connection mongodb implementation process and code examples

After the container is started Log in to admin fi...

Vue implements nested routing method example

1. Nested routing is also called sub-routing. In ...

CSS3 gradient background compatibility issues

When we make a gradient background color, we will...

Practice of using Tinymce rich text to customize toolbar buttons in Vue

Table of contents Install tinymce, tinymce ts, ti...

Vue implements sample code to disable browser from remembering password function

Find information Some methods found on the Intern...

Methods and steps to build nginx file server based on docker

1. Create a new configuration file docker_nginx.c...

What is web design

<br />Original article: http://www.alistapar...

Comprehensive summary of mysql functions

Table of contents 1. Commonly used string functio...

Vue implements a simple timer component

When doing a project, it is inevitable to encount...

Node.js file copying, folder creation and other related operations

NodeJS copies the files: Generally, the copy oper...

Tomcat8 uses cronolog to split Catalina.Out logs

background If the catalina.out log file generated...