YARP reverse proxy doesn't work
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe the bug
I am trying to configure YARP in my AppHost class so that instead of all of my applications running on separate ports, e.g. the Web Api on https://localhost:3003 and the Auth Api on https://localhost:3002, I can access them on https://localhost:8001/webapi and https://localhost:8001/authapi. I've burned a few days on this and have had no luck with getting YARP configured.
The app host starts up without any errors and I see the gateway running on the Aspire dashboard with no errors. The non-proxied urls, e.g. https://localhost:3003, work fine with no https errors. Also of note, every once in a while like after a reboot, the proxy will work for a few minutes but after an app restart it will start failing again. I'm developing on an ARM-based Mac.
Things I've tried:
- With and without https:// support, both with my company's SSL cert and with the developer cert (both set to trusted.)
- Disabling the AddDockerComposeEnvironment code.
- Adding the ASPIRE_ENABLE_CONTAINER_TUNNEL environment variable.
- Running the app (through the Rider IDE) with Admin (sudo) permissions.
- Running these commands:
```aspire publish -o Docker```
```aspire do docker-compose-down-production -o Docker```
```aspire deploy -o Docker```
```
// Initialize builder and configuration.
var builder = DistributedApplication.CreateBuilder(args);
var compose = builder.AddDockerComposeEnvironment("production")
.WithDashboard(dashboard => dashboard.WithHostPort(8080));
await Startup.SetupConfigurationAsync(builder.Configuration);
Startup.InitializeStartup(builder.Configuration);
// Configure data access with .NET Aspire.
var databases = builder.ConfigureDatabases(["StaticData", "Application", "OrgManagement", "Logging", "SearchCache", "Cache"]);
// Configure apps to run under .NET Aspire.
var sslCertificate = AppHostExtensions.GetSslCertificate();
var main = builder.AddProject("main")
.WithHttpsCertificate(sslCertificate);
var authApi = builder.AddProject("auth-api")
.WithHttpsCertificate(sslCertificate)
.AddDatabaseReferences(databases);
var webApi = builder.AddProject("web-api")
.WithHttpsCertificate(sslCertificate)
//.WithHttpsDeveloperCertificate()
.AddDatabaseReferences(databases)
.WithReference(authApi)
.WaitFor(authApi);
var mobileApi = builder.AddProject("mobile-api")
.WithHttpsCertificate(sslCertificate)
.AddDatabaseReferences(databases)
.WithReference(authApi)
.WaitFor(authApi);
var emailGenerator = builder.AddProject("email-generator")
.WithHttpsCertificate(sslCertificate)
.AddDatabaseReferences(databases);
var ssoPortal = builder.AddProject("sso-portal")
.WithHttpsCertificate(sslCertificate)
.AddDatabaseReferences(databases);
var restApi = builder.AddProject("rest-api")
.WithHttpsCertificate(sslCertificate)
.AddDatabaseReferences(databases);
// Set up YARP as the reverse proxy.
var proxyPort = 8001;
var yarp = builder.AddYarp("gateway")
.WithConfiguration(yarpBuilder =>
{
yarpBuilder.AddRoute("/main/{**catch-all}", main)
.WithTransformPathRemovePrefix("/main");
yarpBuilder.AddRoute("/auth/{**catch-all}", authApi)
.WithTransformPathRemovePrefix("/auth");
yarpBuilder.AddRoute("/webapi/{**catch-all}", webApi)
.WithTransformPathRemovePrefix("/webapi");
yarpBuilder.AddRoute("/mobileapi/{**catch-all}", mobileApi)
.WithTransformPathRemovePrefix("/mobileapi");
yarpBuilder.AddRoute("/emailgenerator/{**catch-all}", emailGenerator)
.WithTransformPathRemovePrefix("/emailgenerator");
yarpBuilder.AddRoute("/ssoportal/{**catch-all}", ssoPortal)
.WithTransformPathRemovePrefix("/ssoportal");
yarpBuilder.AddRoute("/restapi/{**catch-all}", restApi)
.WithTransformPathRemovePrefix("/restapi");
})
.WithEnvironment("ASPNETCORE_URLS", $"https://*:{proxyPort.ToString()}")
.WithEnvironment("ASPIRE_ENABLE_CONTAINER_TUNNEL", "true")
.WithHttpsCertificate(sslCertificate)
//.WithHttpsDeveloperCertificate()
.WithEndpoint(proxyPort, proxyPort, "https", "gateway", isExternal: true);
// Run app.
builder.Build().Run();
```
### Expected Behavior
The YARP reverse proxy works in routing traffic to the separate localhost port urls.
### Exceptions (if any)
This site can’t be reached
localhost unexpectedly closed the connection.
### Aspire doctor output
Aspire Environment Check
========================
.NET SDK
✅ .NET 10.0.300 installed (arm64)
Container Runtime
✅ Docker v29.4.2: running (auto-detected (default)) ← active
Environment
✅ HTTPS development certificate is trusted
Summary: 3 passed, 0 warnings, 0 failed
Contributor guide
Assessment
This issue has not been assessed yet.