How to redirect to specific upstream servers based on request URL in Nginx?

后端 未结 2 1817
小蘑菇
小蘑菇 2021-02-07 06:25

I\'m using Nginx as a load balancer for my 5 app servers.

I\'d like to redirect to specific servers based on the request URL, for instance:

acme.com/cate         


        
2条回答
  •  长发绾君心
    2021-02-07 06:33

    Read the documentation, eveything is well explained in it. There's particularly a beginner's guide explaining basics. You would end up with :

    upstream backend  {
      least_conn;
      server 10.128.1.4;
      server 10.128.1.5;
    }
    
    server {
    
      server_name _;
    
      location / {
        proxy_set_header Host $host;
        proxy_pass  http://backend;
      }
    
    }
    
    server {
    
      server_name acme.com;
    
      location /admin/ {
        proxy_set_header Host $host;
        proxy_pass  http://10.128.1.2;
      }
    
      location /category/ {
        proxy_set_header Host $host;
        proxy_pass  http://10.128.1.1;
      }
    
      location / {
        proxy_set_header Host $host;
        proxy_pass  http://backend;
      }
    
    }
    
    server {
    
      server_name api.acme.com;
    
      location / {
        proxy_set_header Host $host;
        proxy_pass  http://10.128.1.3;
      }
    
    }
    

提交回复
热议问题