Configure Kestrel safely behind a reverse proxy
Kestrel is the default web server for ASP.NET Core apps. When an edge proxy terminates TLS and forwards HTTP to Kestrel, the app sees the proxy's address and may not know the original request used HTTPS or came from which client. Bind Kestrel to an internal address and trust forwarded headers only from the actual proxy so redirects, authentication, and logs use the right request context. This guide follows .NET 10 docs for one proxy and an HTTP backend; direct-edge Kestrel TLS is out of scope.
Kestrel is the default web server for ASP.NET Core apps. When an edge proxy terminates TLS and forwards HTTP to Kestrel, the app sees the proxy's address and may not know the original request used HTTPS or came from which client. Bind Kestrel to an internal address and trust forwarded headers only from the actual proxy so redirects, authentication, and logs use the right request context. This guide follows .NET 10 docs for one proxy and an HTTP backend; direct-edge Kestrel TLS is out of scope.
Confirm the topology
This guide assumes HTTPS from clients to a trusted reverse proxy, TLS termination at the proxy, and HTTP from that proxy to Kestrel on a private network.
| Hop | Connection | Responsibility |
|---|---|---|
| Client → proxy | HTTPS | Public certificate, public Host validation, edge controls |
| Trusted proxy → Kestrel | Private HTTP | Proxy identity and forwarded-header trust, app processing |
Without processing X-Forwarded-Proto, the app sees an original HTTPS request as HTTP. That can cause redirect loops or incorrect login URLs and secure cookies. If an app has no trusted proxy, processing forwarded headers lets internet clients forge an IP or scheme. Do not enable the middleware on a directly exposed Kestrel server.
Bind Kestrel to an internal interface
In an ASP.NET Core .NET 10 app, configure a URL. This example listens on loopback so only a proxy on the same host can connect:
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls("http://127.0.0.1:5000");
var app = builder.Build();
app.MapGet("/", () => "ok");
app.Run();You can also try dotnet run --urls http://127.0.0.1:5000 from the app project directory. curl -i http://127.0.0.1:5000/ should return 200 and ok, confirming local reachability only—not public TLS or proxy behavior.
127.0.0.1 works only inside the same network namespace. If the proxy and app are separate containers, bind the app to a container-network interface and block public access with network policy or a firewall. 0.0.0.0 listens on every IPv4 interface and can expose the app unintentionally without network isolation.
--urls and UseUrls configure URL prefixes. Explicit code Listen endpoints take precedence when both are used. An HTTPS prefix also needs a default certificate. This behind-proxy example uses private HTTP only.
Trust forwarded headers only from the proxy
A proxy can forward the original client IP in X-Forwarded-For and the external scheme in X-Forwarded-Proto. Enable the middleware before using these values, and restrict it to the known immediate peer. The example assumes the proxy and app share a host and the proxy connects from 127.0.0.1.
using System.Net;
using Microsoft.AspNetCore.HttpOverrides;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls("http://127.0.0.1:5000");
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.KnownProxies.Add(IPAddress.Parse("127.0.0.1"));
options.ForwardLimit = 1;
});
var app = builder.Build();
app.UseForwardedHeaders(); // before HTTPS redirection, authentication, and HSTS
app.UseHttpsRedirection();
app.MapGet("/", () => "ok");
app.Run();Set KnownProxies to the real remote address of the proxy connection. In containers or on separate hosts, use the proxy's stable private address or a verified narrow network range; do not copy the loopback value blindly. The proxy must remove or replace client-supplied spoofed X-Forwarded-* values. If the request traverses multiple proxies, verify every trusted hop and header behavior before raising ForwardLimit; do not make it unlimited.
Run forwarded-header middleware early, before UseHttpsRedirection, authentication/authorization, external URL generation, and HSTS. Otherwise the app may make decisions using the wrong scheme. Avoid enabling it only with ASPNETCORE_FORWARDEDHEADERS_ENABLED=true: that setting can clear trusted-proxy lists and trust every upstream. Keep Host filtering as defense in depth, and validate public Host values at the proxy too.
Verify and troubleshoot
Confirm the app listens only on the intended private address and that the public network cannot reach the Kestrel port.
From the same host, run `curl -i http://127.0.0.1:5000/` and confirm the app response.
Send a request through the real public HTTPS proxy. Check for redirect loops and confirm login callback URLs and secure-cookie behavior. If logs show only the proxy IP, review the trusted peer, forwarded headers, and middleware order.
In an approved test environment, check that a client-supplied `X-Forwarded-For` value cannot override the proxy's trusted value.
**502 or connection refused:** check Kestrel's bind address and port, app process, proxy upstream, and container network.
**HTTPS redirect loop:** ensure forwarded headers run first and the proxy sets `X-Forwarded-Proto` to the external scheme. Do not fix this by trusting arbitrary peers.
**Client IP missing or spoofed:** confirm the immediate proxy is in the trust list and overwrites forwarded headers. Do not trust every proxy or process every header.
**Proxy cannot connect:** `127.0.0.1` in one container does not identify another container. Use an internal service address and network policy without exposing a public app port.
Sources and limits
[Kestrel web server](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-10.0) — default ASP.NET Core server and deployment topologies
[Configure Kestrel endpoints](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-10.0) — URL endpoints, binding precedence, and HTTPS certificates
[Kestrel security considerations](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/security-considerations?view=aspnetcore-10.0) — edge versus proxy trust boundaries
[Configure ASP.NET Core behind proxies](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-10.0) — forwarded-header meaning, trusted peers, and middleware order
Microsoft .NET 10 documentation was checked on 2026-11-19. Code is illustrative and was not built or run. No proxy address, TLS certificate, firewall, container network, or production configuration was verified. Consult deployment-specific docs for IIS integration or Kestrel TLS at the edge.