Nginx исключений loadbalancer - PullRequest
0 голосов
/ 21 апреля 2020

У меня есть Nginx с двумя серверами позади (балансировка нагрузки 90% и 10%), куда я отправляю запросы. Конфигурация следующая:

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

Мой вопрос - как сделать исключения в этой конфигурации, чтобы ВСЕГДА отправлять запросы следующим образом без какой-либо балансировки нагрузки:

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)

1 Ответ

0 голосов
/ 21 апреля 2020
 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;
   }

 }

Специфика c для Azure Вы можете использовать другой подход, например:

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

...