davidfowl / AspireYarp

Yarp resource for Aspire.Hosting
71 stars 4 forks source link

YARP proxy not forwarding to origins #2

Open Kralizek opened 7 months ago

Kralizek commented 7 months ago

Hi,

The YARP proxy is not forwarding the call to the origins.

I created a repro of the issue here: https://github.com/Kralizek/AspireYarpIssueRepro

The project includes:

The AppHost program file looks like this:

using AppHost.Extensions;

var builder = DistributedApplication.CreateBuilder(args);

var api = builder.AddProject<Projects.API>("api");

var legacy = builder.AddProject<Projects.LegacyAPI>("legacy");

builder.AddYarp("test")
    .Route("legacy", legacy, path: "/legacy", preservePath: false)
    .Route("api", api, path: YarpResource.CatchAll);

builder.Build().Run();

When I run the solution

$ dotnet run --project .\tools\AppHost\

Building...
info: Aspire.Hosting.DistributedApplication[0]
      Aspire version: 8.0.0-preview.5.24201.12+1b627b7d5d399d4f9118366e7611e11e56de4554
info: Aspire.Hosting.DistributedApplication[0]
      Distributed application starting.
info: Aspire.Hosting.DistributedApplication[0]
      Application host directory is: C:\Users\RenatoGolia\Development\GitHub\AspireYarpIssueRepro\tools\AppHost
info: Aspire.Hosting.DistributedApplication[0]
      Now listening on: https://localhost:17284
info: Aspire.Hosting.DistributedApplication[0]
      Distributed application started. Press Ctrl+C to shut down.

I get the following on the dashboard

image

Unfortunately, when I navigate to http://127.0.0.1:63438/weatherforecast (that should be forwarded to https://localhost:7270/weatherforecast) I get redirected to https://api:7270/weatherforecast.

Similarly, when I navigate to http://127.0.0.1:63438/legacy/weatherforecast (that should be forwarded to https://localhost:7018/weatherforecast), I get redirected to https://api:7270/legacy/weatherforecast.

I wasn't able to reproduce the issue with the transformation as explained here. I'll keep investigating on why it doesn't work on my main project.

Thanks for your help!

JohnGalt1717 commented 7 months ago

I'm getting similar with P6 and configuration file based setup. I get a 502 error and there's also an SSL Certificate error in the logs.

Kralizek commented 7 months ago

@davidfowl were you able to reproduce the problem detailed in the issue?

JohnGalt1717 commented 7 months ago

@Kralizek I was able to fix this by adding :

"Host": "localhost",

To all of my clusters. It then started working again.

Kralizek commented 7 months ago

@Kralizek I was able to fix this by adding :

"Host": "localhost",

To all of my clusters. It then started working again.

Adding this solved the issue with the hostname in the link, but I still get a redirect...

builder.Resource.ClusterConfigs[target.Resource.Name] = new()
{
    ClusterId = target.Resource.Name,
    Destinations = new Dictionary<string, DestinationConfig>
    {
        [target.Resource.Name] = new() { 
            Address = $"http://{target.Resource.Name}",
            Host = builder.ApplicationBuilder.ExecutionContext.IsRunMode ? "localhost" : default
        }
    }
};

image

EDIT: adding the following solved the redirect issue.

var proxyBuilder = builder.Services
    .AddReverseProxy()
    .ConfigureHttpClient((context, handler) => handler.AllowAutoRedirect = true);