nginxhttp-redirectload-balancingsticky-session

Nginx loadbalancer exceptions


I have an Nginx with 2 servers behind (90% and 10% load balancing), where I send requests. The configuration is following:

   upstream https://upstream-stage.infra.com/ {
    ip_hash;
    server oldserver-stage.infra.com weight=9 max_fails=3 fail_timeout=30s;
    server newserver-stage.infra.com weight=1 max_fails=3 fail_timeout=30s;
    }

server {
    server_name upstream-nginx-stage.infra.com www.upstream-nginx-stage.infra.com;
    return 301 https://upstream-nginx-stage.infra.com$request_uri;
}

server {
    listen 443 ssl;
    ssl_certificate         /etc/nginx/ssl/wild.infra.com.crt;
    ssl_certificate_key     /etc/nginx/ssl/wild.infra.com.key;
    server_name www.upstream-nginx-stage.infra.com;
    return 301 https://upstream-nginx-stage.infra.com$request_uri;
}

server {
    listen 443 ssl;
    ssl_certificate         /etc/nginx/ssl/wild.infra.com.crt;
    ssl_certificate_key     /etc/nginx/ssl/wild.infra.com.key;
    server_name upstream-nginx-stage.infra.com;

location / {


    proxy_pass https://upstream-stage.infra.com/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    }
}

My question is - how to make exceptions in this configuration to ALWAYS send requests as following without any load-balancing:

upstream-nginx-stage.infra.com/exception1PDF/... => oldserver-stage.infra.com

upstream-nginx-stage.infra.com/exception2/... => newserver-stage.infra.com

upstream-nginx-stage.infra.com/someAPIrequest#/... => APIPublicApp.azurewebsites.net (standalone and completelly different site)

Solution

  •  upstream old_server {
       ip_hash;
       server oldserver-stage.infra.com weight=9 max_fails=3 fail_timeout=30s;
     }
    
     upstream new_server {
       ip_hash;
       server newserver-stage.infra.com weight=9 max_fails=3 fail_timeout=30s;
     }
    
     upstream azure_server {
       ip_hash;
       server azure_app.infra.com weight=9 max_fails=3 fail_timeout=30s;
     }
    
     server {
       [configs]
    
       location / {
         proxy_pass https://upstream-stage.infra.com/;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Real-IP $remote_addr;
       }
    
       location /exception1PDF {
         proxy_pass https://old_server;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Real-IP $remote_addr;
       }
    
       location /exception2 {
         proxy_pass https://new_server;
         proxy_set_header Host $host;   
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Real-IP $remote_addr;
       }
    
       location /someAPIrequest# {
         proxy_pass https://azure_server;
         proxy_set_header Host $host;   # Or you can set the Host configured in the server_name of the backend server, it depends of the configuration of the backend
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Real-IP $remote_addr;
       }
    
     }
    

    Specific for Azure you can use other approach like that:

        location /someAPIrequest# {
          proxy_pass https://apipublicapp.azurewebsites.net;
          proxy_set_header Host "apipublicapp.azurewebsites.net";
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Real-IP $remote_addr;
        }