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

后端 未结 2 1815
小蘑菇
小蘑菇 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;
      }
    
    }
    
    0 讨论(0)
  • 2021-02-07 06:38

    You will also need to rewrite the URL otherwise /whatever/ will get forwarded to the backend server

    location /admin/ {
        rewrite ^/admin^/ /$1 break;
        proxy_pass http://10.128.1.2;
    }
    
    0 讨论(0)
提交回复
热议问题