NGINX: proxy_pass ディレクティブを使用してアップストリーム サーバーのマッピングを使用できますか?

NGINX: proxy_pass ディレクティブを使用してアップストリーム サーバーのマッピングを使用できますか?

私は、多数のバックエンド IIS サーバーへのリバース プロキシとして機能する NGINX サーバーを持っています。アドレス バーに同じ URL を保持しながら (バックエンド サーバー名またはアドレスをユーザーに表示しないようにするため)、指定された URI に応じて、各着信 HTTPS 要求を特定のバックエンド (上流) サーバーに渡したいと考えています。

たとえば、
アドレス バーのリクエストは次のようになります:
https://test.blahblah.com/url_a
実際には --> に送信されます
https://upstreamserver_a/url_a
が、アドレス バーでは次のようになります:
https://test.blahblah.com/url_a

これまでの nginx.conf は次のとおりです。

    ## Upstreamserver_A backend for test.blahblah.com
    upstream upstreamserver_a {
            server 10.10.1.12:80; #upstreamserver_a.blahblah.com
    }

    map_hash_bucket_size 128;
    map_hash_max_size 2048;


    # URI to Server Map
    map $1 $upstream_host {
            default         whatever;
            url_a       upstreamserver_a;
    }


    ## OUR HTTP SERVER AT PORT 80
    server  {
            listen      80;
            server_name test.blahblah.com;
            index       index.html;
            root        /usr/share/nginx/html;

            ## redirect http to https ##
            proxy_redirect http:// https://;
    }


    server  {
            listen      443 ssl;
            server_name test.blahblah.com;
            #root        /usr/share/nginx/html;
        ### ssl config - customize as per your setup ###
            ssl_certificate      ssl/blahblah.com/blahblah.pem;
            ssl_certificate_key  ssl/blahblah.com/blahblah.key;
            ssl_protocols        SSLv3 TLSv1 TLSv1.1 TLSv1.2;
            ssl_ciphers RC4:HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers on;
            keepalive_timeout    70;
            ssl_session_cache    shared:SSL:10m;
           ssl_session_timeout  10m;


        ## PROXY backend
            location ~ ^/(.*)$ {
                    rewrite ^/([^/]+)$ https://test.blahblah.com/webapps        /Application.html?firm=$1#login break;

                    proxy_pass http://$upstream_host$uri;
            }
     }

もちろん、1 つのサーバーが動作するようになったら、upstreamserver_b、upstreamserver_c など、複数のアップストリーム サーバーを追加します。SSL
の部分についてはサポートは必要ありません。マッピングや変数参照についてのみサポートが必要です。 次
の行を置き換えることで SSL が機能することを証明しました 。これは機能します。つまり、URL を 'test.blahblah.com' という URL に偽装したまま、upstreamserver_a にリダイレクトします。
proxy_pass http://$upstream_host$uri;

proxy_pass http://upstreamserver_a;

どなたか助けていただければ幸いです。
私が必要としているものに非常に近い回答の例をたくさん見つけましたが、私は愚かすぎて、それらを自分の特定のニーズに合わせて調整できませんでした。

答え1

URI パターンを正規表現で記述し、それを場所で使用して、rewrite または proxy_pass ターゲットで使用できる部分をキャプチャする必要があります。例:

location ~ ^/url_(.).*$ {
  #this will capture your /url_a or /url_b URI's 
  #and will store the "a" or "b" into $1 variable
  #which you can use to proxy the request to a specific server
  proxy_pass http://$1.$upstreams$uri;
  #the above will result in a.<upstreams>/url_a for your example.
  #don't forget the resolver configuration for domain names.
}

それが役に立てば幸い

答え2

具体的に何を望んでいるかに応じて、いくつかの指示が思い浮かびます。

たとえば、基本的に、インターネット名を完全には認識していないさまざまな上流サーバーに nginx プロキシをすべて実行し、内部名と外部名をマッピングしたい場合は、次のようにします。

map $host $host_upstream_mapped {
    hostnames;

    test.example.su  test_iis.internal:8070;
    example.com  bleeding_edge.internal:9999;

    default  localhost:8080;
}
server {
    listen [::]:80;
    location / {
        proxy_pass http://$host_upstream_mapped$request_uri;
    }
}

PS注意:使用することが重要です$request_uriこの文脈では(そして単に$uri、そうしないと、残ってしまうからです$args)、または、rewriteディレクティブも使用している場合は、$uri$is_args$argsも良いオプションです (この 2 つは同等ではないことに注意してください)。

関連情報