Home > Operation and Maintenance > Nginx > How Nginx dynamically forwards to upstream based on the path in the url

How Nginx dynamically forwards to upstream based on the path in the url

王林
Release: 2023-05-17 18:28:06
forward
1569 people have browsed it

场景1

/svr1/xxxx?yyy 转发到 svr1:8080/xxxx?yyy

/svr2/xxxx?yyy 转发到 svr2:8080/xxxx?yyy

配置如下:

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;
}
Copy after login

场景2

svc1下有3个对等服务srv1,2,3,/svc1/xxxx?yyy 转发到,srv1/2/3:8080/xxxx?yyy

svc2下有3个对等服务srv4,5,6,/svc2/xxxx?yyy 转发到,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;                
}
Copy after login

The above is the detailed content of How Nginx dynamically forwards to upstream based on the path in the url. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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