首页 > 解决方案 > 反向代理 - 根据请求路径选择路由

问题描述

我已经使用 .net Core 5.0 创建了一个反向代理,并且到目前为止它可以工作。现在我想根据请求的 URL 更改目的地。这是我的 appsettings.json:

 "ReverseProxy": {
"Routes": [
  {
    "RouteId": "route1",
    "ClusterId": "cluster1",
    "Match": {
      "Path": "{**catch-all}"
    }
  }
],
"Clusters": {
  "cluster1": {
    "Destinations": {
      "cluster1/destination1": {
        "Address": "http://192.168.178.36"
      }
    }
  },
  "cluster2": {
    "Destinations": {
      "cluster1/destination1": {
        "Address": "http://localhost:8080/signalr"
      }
    }
  }
}

我知道希望所有请求都转发到我的网络服务器。但是,如果有人使用路径 /signalr 开始一个请求,那么它应该被转发到我的信号器服务器。我也尝试使用谷歌,但据我所知,与 appsettings.json 相关的文档到目前为止真的很差。

标签: c#.net-corereverse-proxyappsettings

解决方案


我找到了答案,我的新 apssettings.json:

"ReverseProxy": {
"Routes": [
  {
    "RouteId": "signalrServer",
    "ClusterId": "signalrServer",
    "Match": {
      "Path": "/signalr/{**catch-all}"
    }
  },
  {
    "RouteId": "mediaserver",
    "ClusterId": "mediaserver",
    "Match": {
      "Path": "/live/{**catch-all}"
    }
  },
  {
    "RouteId": "default",
    "ClusterId": "default",
    "Match": {
      "Path": "{**catch-all}"
    }
  }
],
"Clusters": {
  "default": {
    "Destinations": {
      "notfound": {
        "Address": "http://192.168.178.41/system/error/NotFound"            
      }
    }
  },
  "mediaserver": {
    "Destinations": {
      "mediaserver": {            
        "Address": "http://192.168.178.36"
      }
    }
  },
  "signalrServer": {
    "Destinations": {
      "signalrServer": {
        "Address": "http://localhost:8080"
      }
    }
  }
}

推荐阅读