YARP (Yet Another Reverse Proxy) の設定

YARPの設定をします。

概要

こちらの記事でインストールしたYARPの振り分けの設定をします。

設定例1 : すべてのアクセスを指定したURLに向ける

YARPを導入したホストのアクセスすべてを https://www.ipentec.com に向ける設定です。


https://(ドメイン名)/app/convert にアクセスした場合、パスは維持され、https://www.ipentec.com/app/convertのプロキシとなります。


YARPのappsettings.jsonには以下の記述となります。
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オブジェクトのminimumclusterDestinationsが振り向け先になります。

オリジンサーバー(コンテンツサーバー)のアドレスは、Clusters オブジェクト内のクラスタIDのオブジェクトの名部に記述します。 Destinationsオブジェクト内に遷移先の名称のオブジェクト(上記の例では"ipentec")を作成し内部のAddressに振り向け先のアドレスを記述します。

設定例2 : すべてのアクセスを指定した内部のサーバーに向ける

先に紹介した設定例1とほぼ同じですが、振り向け先が内部のIPとなる例です。



YARPのアクセスを192.168.0.10 に振り向ける設定のappsettings.jsonです。
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"
          }
        }
      }
    }
  }
}

設定例3 :指定したアドレスをコンテンツサーバーのサブディレクトリに向ける

指定したアドレスをコンテンツサーバーのサブディレクトリに向ける設定例です。
https://test.ipentec.net/get にアクセスすると、http://192.168.0.10/app/get のプロキシとなります。

appsettings.json
{
  "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"
          }
        }
      }
    }
  }
}

設定例4 : サブディレクトリを指定したアドレスに向ける

先ほどの例では、 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 を削除する処理を追加します。


appsettings.json
{
  "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"
          }
        }
      }
    }
  }
}

設定例5 : 特定のホストのアクセスを指定したアドレスに向ける

特定のホストへのアクセスを指定したアドレスに向ける設定例です。
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 に書き換えられます。


appsettings.json
{
  "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" ]
  }
著者
iPentec.com の代表。ハードウェア、サーバー投資、管理などを担当。
Office 365やデータベースの記事なども担当。
最終更新日: 2024-01-07
作成日: 2022-07-11
iPentec all rights reserverd.