wunmi dawodu
wunmi dawodu

Reputation: 37

Yarp Reverse Proxy - Response 502 Bad Gateway

I want to implement a reverse proxy using Yarp. The Api is up and running as i can send request to it. However, anytime i try the same request through the proxy, i get an error 502 Bad gateway

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddReverseProxy()
     .LoadFromConfig(Configuration.GetSection("ReverseProxy"));
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.UseRouting();
    // Register the reverse proxy routes
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapReverseProxy();
    });
}

Appsettings

"ReverseProxy": {
  "Routes": {
    "route1": {
      "ClusterId": "cluster1",
      "Match": {
        //"Methods": [ "GET", "POST", "PUT", "HEAD", "OPTIONS", "DELETE" ],
        "Path": "{**catch-all}"
      }
    }
  },
  "Clusters": {
    "cluster1": {
      "Destinations": {
        "cluster1/destination1": {
          "Address": "https://localhost:44339/"
        }
      }
    }
  }
}

I am getting a response - 502 bad gateway anytime i send a request through the proxy

Upvotes: 4

Views: 5026

Answers (1)

Jason Pan
Jason Pan

Reputation: 21972

You should click output and select ProjectName-Asp.Net Core Web Server to check the details.

I use your code and also append another settings, and check the logs, I found the reason is http://localhost:44339 not start. It should use another webapp's port in local.

enter image description here

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  //"AllowedHosts": "*"

  "ReverseProxy": {
    "Routes": [
      {
        "ClusterId": "cluster1",
        "Match": {
          //"Methods": [ "GET", "POST", "PUT", "HEAD", "OPTIONS", "DELETE" ],
          "Path": "{**catch-all}"
        }
      },
      {
        // matches /something/* and routes to 2 external addresses
        "ClusterId": "cluster2",
        "Match": {
          "Path": "/something/{*any}"
        }
      }
    ],
  "Clusters": {
      "cluster1": {
        "Destinations": {
          "cluster1/destination1": {
            "Address": "http://localhost:44339/"
          }
        }
      },
      "cluster2": {
        "Destinations": {
          "first_destination": {
            "Address": "https://baidu.com"
          },
          "another_destination": {
            "Address": "https://bing.com"
          }
        },
        "LoadBalancingPolicy": "PowerOfTwoChoices"
      }

    }
  }
}

Test Result

default: https://localhost:44370

enter image description here

https://localhost:44370/somthing

it will redirect to bing site.

enter image description here

Upvotes: 5

Related Questions