URL最初のヘッダーに基づいてnginxリクエストをリダイレクトする方法

URL最初のヘッダーに基づいてnginxリクエストをリダイレクトする方法

nginxサーバーによって処理される3つのアプリケーションサーバーがあります。

upstream he {
server h1.abc.example.com;
server h2.abc.example.com;

}

特定のヘッダー値に基づいてリクエストをリダイレクトする方法は?例えば

 abc-h1.abc.example.com should go to server 
h1.abc.example.com
 def-h1.abc.example.com should go to server 
h2.abc.example.com

すべての要求の場所は同じです-h1.abc.example.com

ベストアンサー1

Nginxは間違いなくこれを行うことができます。ホスト固有のヘッダー(server_name)を指定し、上流を分離して各ホストに1つずつ割り当てるだけです。

次のnginxサーバーフラグメントが機能する可能性があります(私の考えでテストされていません)。

upstream one { server h1.abc.example.com; }
upstream two { server h2.abc.example.com; }

server {
    listen 8080;
    server_name abc-h1.abc.example.com;

    location / {
        proxy_pass one;
    }
}

server {
    listen 8080;
    server_name def-h1.abc.example.com;

    location / {
        proxy_pass two;
    }
}

HTTP以外のエンドポイントにトラフィックを送信したい場合は、別のプロキシハンドラ(fastcgi_pass、、、、、)がありますuwsgi_passscgi_passmemcached_pass

編集:エラーを修正server_name

おすすめ記事