0

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)
Vasyl Stepulo
  • 1,493
  • 1
  • 23
  • 43

1 Answers1

0
 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;
    }

Bruno Paiuca
  • 131
  • 6
  • Well, I don't need to add azure_server (APIPublicApp.azurewebsites.net) into the load balancer. I only need to transfer all the requests "upstream-nginx-stage.infra.com/someAPIrequest#/*.* directly to APIPublicApp.azurewebsites.net According to your answer - you add azure_server to load balancer – Vasyl Stepulo Apr 21 '20 at 14:37
  • not really, I just create one upstream to the azure servers and direct all the requests that match with the location /someAPIrequest#, you can do it directly in the proxy_pass it will work, but you will lose some benefits that upstream offer you. – Bruno Paiuca Apr 21 '20 at 20:51