Mostafa
Mostafa

Reputation: 682

How define specific pattern for yarp path configuration

I need to define a pattern for below path for yarp Path section in appsettings.json file:

/Contact/Images/Image.ashx?h=28&id=69865t65-888t-w987-tg7l-j88yt6541uj&w=100&54464454

my config for yarp in appsettings.json file is as below:

      "ReverseProxy": {
         "Routes": {
            "route1": {
               "ClusterId": "cluster1",
               "Match": {
                     "Path": ""
                 }
              }
           },
           "Clusters": {
              "cluster1": {
                 "LoadBalancingPolicy": "RoundRobin",
                 "Destinations": {
                    "cluster1/destination1": {
                       "Address": "https://www.example.com"
                    }
                 }
              }
           }
        }

What can I do? Any help will be appreciated.

Upvotes: 0

Views: 474

Answers (1)

Yuning Duan
Yuning Duan

Reputation: 1702

You can add a match pattern in the Path field in the Match section. {**catch-all} is a wildcard that will match any path. Then configure the target server address in Address.

In this example: I configured the routing address for the response, which means that when there is a request with a path matching /WeatherForecast/PP/{**catch-all}, the request will be forwarded to the target api address I configured in Address:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ReverseProxy": {
    "Routes": {
      "route1": {
        "ClusterId": "cluster1",
        "Match": {
          "Path": "/WeatherForecast/PP/{**catch-all}"
        }
      }
    },
    "Clusters": {
      "cluster1": {
        "Destinations": {
          "destination1": {
            "Address": "https://localhost:7198/"

          }
        }
      }
    }
  }
}

When the corresponding routing node:

enter image description here

This will forward to my target address:

enter image description here

Upvotes: 2

Related Questions