ASP.NET Web APIを使用すると、このようなプロキシサーバーを作成するのはかなり簡単です。必要なのは、委任ハンドラです:
public class ProxyHandler : DelegatingHandler
{
private static HttpClient client = new HttpClient();
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
// strip the /proxy portion of the path when making the request
// to the backend node because our server will be made to listen
// to :80/proxy/* (see below when registering the /proxy route)
var forwardUri = new UriBuilder(request.RequestUri.AbsoluteUri.Replace("/proxy", string.Empty));
// replace the port from 80 to the backend target port
forwardUri.Port = 8664;
request.RequestUri = forwardUri.Uri;
if (request.Method == HttpMethod.Get)
{
request.Content = null;
}
// replace the Host header when making the request to the
// backend node
request.Headers.Host = "localhost:8664";
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
return response;
}
}
、最後に残っているすべては、このハンドラを登録することです:
config.Routes.MapHttpRoute(
name: "Proxy",
routeTemplate: "proxy/{*path}",
handler: HttpClientFactory.CreatePipeline(
innerHandler: new HttpClientHandler(),
handlers: new DelegatingHandler[]
{
new ProxyHandler()
}
),
defaults: new { path = RouteParameter.Optional },
constraints: null
);
この例では、プロキシが:80/proxy/*
に耳を傾けますと:8664/*
にそれを転送します。
あなたのWeb APIに次のリクエストを送信するのであれば:これはまた、POSTと:80/proxy/*
に行われた他の動詞のために動作します
GET http://localhost:8664/SomeReq?some=query HTTP/1.1
Host: localhost:8664
Connection: close
:
GET http://localhost:80/proxy/SomeReq?some=query HTTP/1.1
Host: localhost:80
Connection: close
に変換されます。
ウェブサーバー全体をプロキシに変えて:80/*
を聞きたいのなら、私の例で使用した接頭辞/proxy
を取り除くことができます。
これは、これは概念実証プロキシサーバーに過ぎないと言われています。実際のプロダクションシステムでは、この目的のために設計されたnginx
やHAProxy
などのフロントエンドロードバランサにこのタスクをオフロードします。次に、IISとDelphiの両方のアプリケーションが任意のポートでリッスンし、nginxがポート80をリッスンし、いくつかのパターンに基づいてバックエンドノードにトラフィックを転送するように構成できます。ロード・バランサを使用すると、複数のバックエンド・ノードを所有している場合と同様に、負荷を分散し、ダウンタイムなしでアプリケーションを更新できる可能性があります(ノードが完全に制御されているためロードバランサプール)。
以下の答えを確認できます:http://stackoverflow.com/a/7310936/6381169 –
@KürşatDuyguluありがとうございますが、私は既にこのタイプのリダイレクトを認識しています。私の質問では、「私はリダイレクトを実行するのではなく、要求を目的のサーバーに複製するだけです」と述べています。だから、もしかすると、プロキシの多少。 –