YARPの設定をします。
こちらの記事でインストールしたYARPの振り分けの設定をします。
YARPを導入したホストのアクセスすべてを https://www.ipentec.com
に向ける設定です。
https://(ドメイン名)/app/convert
にアクセスした場合、パスは維持され、https://www.ipentec.com/app/convert
のプロキシとなります。
YARPのappsettings.jsonには以下の記述となります。
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"minimumroute": {
"ClusterId": "minimumcluster",
"Match": {
"Path": "{**catch-all}"
}
}
},
"Clusters": {
"minimumcluster": {
"Destinations": {
"ipentec": {
"Address": "https://www.ipentec.com/"
}
}
}
}
}
}
ReverseProxy
オブジェクトにYARPの振り分けの設定を記述します。
Routes
に検出するURLのパターンを記述します。今回はすべてのURLを振り向けるため、Match
オブジェクト内のPath
に{**catch-all}
を設定します。
Matchの条件に一致した場合の振り向け先はClusterId
の値にクラスタのIDの文字列を指定します。上記の例では、minimumcluster
を設定しており、
条件にマッチした場合は、Clusters
オブジェクトのminimumcluster
のDestinations
が振り向け先になります。
オリジンサーバー(コンテンツサーバー)のアドレスは、Clusters
オブジェクト内のクラスタIDのオブジェクトの名部に記述します。
Destinations
オブジェクト内に遷移先の名称のオブジェクト(上記の例では"ipentec")を作成し内部のAddress
に振り向け先のアドレスを記述します。
先に紹介した設定例1とほぼ同じですが、振り向け先が内部のIPとなる例です。
YARPのアクセスを192.168.0.10
に振り向ける設定のappsettings.jsonです。
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"minimumroute": {
"ClusterId": "minimumcluster",
"Match": {
"Path": "{**catch-all}"
}
}
},
"Clusters": {
"minimumcluster": {
"Destinations": {
"content-server": {
"Address": "http://192.168.0.10"
}
}
}
}
}
}
指定したアドレスをコンテンツサーバーのサブディレクトリに向ける設定例です。
https://test.ipentec.net/get
にアクセスすると、http://192.168.0.10/app/get
のプロキシとなります。
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"minimumroute": {
"ClusterId": "minimumcluster",
"Match": {
"Path": "/get/{**catch-all}"
}
}
},
"Clusters": {
"minimumcluster": {
"Destinations": {
"content-server": {
"Address": "http://192.168.0.10/app"
}
}
}
}
}
}
先ほどの例では、
https://test.ipentec.net/get
にアクセスすると、http://192.168.0.10/app/get
を参照するため、
サブディレクトリの名称がコンテンツサーバーにも引き継がれてしまいます。アプリケーションやネットワークの構成によっては、
アクセス元のURLをコンテンツサーバーに渡したくない場合があります。
次の例は、サブディレクトリを指定したアドレスに向ける設定例です。
https://test.ipentec.net/myapp/test/Default.aspx
にアクセスすると、http://192.168.0.10/app/Default.aspx
のプロキシとなります。
アクセス元の myapp/test
部分がコンテンツサーバーのURLには反映されない動作になります。
コンテンツサーバーのURLに渡さないようにするためには、Transforms
を設定し、PathRemovePrefix
を記述し、
URLからプレフィックス /myapp/test
を削除する処理を追加します。
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"minimumroute": {
"ClusterId": "minimumcluster",
"Match": {
"Path": "/myapp/test/{**catch-all}"
},
"Transforms": [
{
"PathRemovePrefix": "/myapp/test"
}
]
}
},
"Clusters": {
"minimumcluster": {
"Destinations": {
"content-server": {
"Address": "http://192.168.0.10/app"
}
}
}
}
}
}
特定のホストへのアクセスを指定したアドレスに向ける設定例です。
https://moon.ipentec.com/contents/index.html
にアクセスすると、http://192.168.0.200/contents/index.html
のプロキシとなります。
特定のホストのプロキシ処理を実装するには、"Match" キー内に "Hosts" キーを記述します。
アクセス先のホスト名が、"Hosts" キーの値のホスト(今回の例の場合は "moon.ipentec.net")であった場合は MoonRoute
にマッチするため、MoonCluster
の処理が実行され、
アクセス先が、http://192.168.0.200
に書き換えられます。
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ReverseProxy": {
"Routes": {
"MoonRoute": {
"ClusterId": "MoonCluster",
"Order": 0,
"AuthorizationPolicy": "Default",
"Match": {
"Hosts": [ "moon.ipentec.net" ]
}
}
},
"Clusters": {
"MoonCluster": {
"Destinations": {
"moon.ipentec.net": {
"Address": "http://192.168.0.200"
}
}
}
}
}
}
動作確認の際は、Hostsキーに "localhost"を追加して、プロキシマシン上から https://localhost/contents/index.html
にアクセスし、
192.168.0.200 のサーバーにプロキシされるかを確認すると、動作確認が楽です。
"Match": {
"Hosts": [ "localhost", "moon.ipentec.net" ]
}