0

我有一个 Nginx,后面有 2 个服务器(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)
4

1 回答 1

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

 }

特定于 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;
    }

于 2020-04-21T14:28:36.210 回答