I am not Fat
I am not Fat

Reputation: 447

Yarp proxy does not log a redirect?

I am currently playing around with YARP (yet another reverse proxy) And seem to have managed to "short-cicuit" a redirect?

one of the redirect is being logged, and the other one is not being logged eventhough they have been setup the same way?

program.cs

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddReverseProxy().LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

var app = builder.Build();

app.MapReverseProxy();

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

appsetting.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ReverseProxy": {
    "Routes": {
      "serverforce": {
        "ClusterId": "serverforce",
        "Match": {
          "Path": "google"
        }
      },
      "azure": {
        "ClusterId": "azure",
        "Match": {
          "Path": "msn"
        }
      }
    },
    "Clusters": {
      "serverforce": {
        "Destinations": {
          "google": {
            "Address": "https://google.de/"
          }
        }
      },
      "azure": {
        "Destinations": {
          "msn": {
            "Address": "https://www.msn.com/da-dk"
          }
        }
      }
    }
  }
}

https://localhost:7147/msn redirects me to https://www.msn.com/da-dk with a log message

info: Yarp.ReverseProxy.Forwarder.HttpForwarder[9]
      Proxying to https://www.msn.com/da-dk/msn HTTP/2 RequestVersionOrLower no-streaming

https://localhost:7147/google redirects me to https://www.google.dk with no log message

why am O not receiving any log message? and why an old site, and not the latest site? I tried rebuild and cleaned rebuild the project multiple times?

Upvotes: 1

Views: 3905

Answers (1)

To answer your question. I have tested your configuration and everything is being logged correctly. This is my project file

 <Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Yarp.ReverseProxy" Version="1.0.0" />
  </ItemGroup>

</Project>

Logs:

enter image description here

Try deleting the bin and obj folders


Using a reverse proxy for redirecting is not correct. Reverse proxies are used to hide the existence and characteristics of origin servers (https://en.wikipedia.org/wiki/Reverse_proxy)

What you are seeing is that the origin servers are redirecting you to a different URL. For example you are requesting https://www.msn.com/da-dk/msn and msn is redirecting to https://www.msn.com/da-dk?r=1

Try this example and observe what happens:

  "ReverseProxy": {
    "Routes": {
      "bbc": {
        "ClusterId": "bbc",
        "Match": {
          "Path": "news"
        }
      }
    },
    "Clusters": {
      "bbc": {
        "Destinations": {
          "msn": {
            "Address": "https://www.bbc.com/"
          }
        }
      }
    }
  }

Upvotes: 1

Related Questions