Docs for UseForwardedHeaders, working with reverse proxies and load balancers
https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction https://github.com/aspnet/Security/issues/1070 https://github.com/aspnet/Security/issues/929 https://github.com/aspnet/Security/issues/757 https://github.com/aspnet/Security/issues/853 https://github.com/aspnet/IISIntegration/issues/140 https://github.com/aspnet/Security/issues/1620 https://github.com/aspnet/Security/issues/1702 Enabled by default by UseIISIntegration, but with limited settings (ANCM reverse proxy on loopback)
https://github.com/aspnet/BasicMiddleware/issues/190
@Tratcher Please clarify the ask and outline the solution.
Most sites are hosted behind a reverse proxy, especially our recommended configurations using IIS/ANCM or NGinx. When the request is being proxied some information may be lost like the original scheme (http/https), the client IP address, etc.. You may need these values to properly generate links, evaluate polices, geolocate clients, etc.. There is a convention for the proxies to forward these values as HTTP headers (x-forwarded-*). The UseForwardedHeaders middleware reads these headers and fills in the associated fields on HttpContext.
UseForwardedHeaders has pretty complicated settings due to trust concerns with these forwarded headers (e.g. spoofing). Explain how the middleware works overall and each of the settings.
UseForwardedHeaders is enabled by default by UseIISIntegration, but with a very restricted configuration specific to ANCM.
@Tratcher if UseIISIntegration is not enabled and/or IIS is not used (i.e. nginx, apache, etc) it is important to not only have the reverse proxy setup properly for forwarding the requests and headers, but to also add the UseForwardedHeaders middleware.
Is that what you are asking here to document?
and is more that the following needed:
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
@spboyer yes that's part of it. There are several other settings related to identifying/trusting the proxies. You also need to make sure your reverse proxy is adding the headers.
https://docs.microsoft.com/en-us/aspnet/core/publishing/linuxproduction captures the nginx specific scenario.
@Tratcher so are you looking for a specific document targeted at just the setting for UseForwardedHeaders?
UseForwardedHeaders has pretty complicated settings due to trust concerns with these forwarded headers (e.g. spoofing). Explain how the middleware works overall and each of the settings.
Not quite. We need to explain the end-to-end scenarios and how the various UseForwardedHeaders settings apply (e.g. IIS+ANCM, Nginx, multiple proxies, etc.).
We had some problems with forwarding and lost 2 days due to this. It would be nice to mention in the documentation that only headers for KnownProxies / KnownNetworks are taken into account and that the default is only IPAddress.IPv6Loopback / IPAddress.IPLoopback. So changing forwarding limit has basically no effect without modifying KnownProxies / KnownNetworks. And the right way to setup forwarding when there is Proxy -> IIS -> ASPNET situation is:
- increase ForwardingLimit > 1
- depending on the type of proxy also set RequireHeaderSymmetry = false
- add the proxy IP to KnownProxies / KnownNetworks.
I wanted to pitch in here as I failed to find documentation for what I am doing.
I write my nginx configuration in service fabric dynamic from the following code:
sb.AppendLine($"{tabs}proxy_set_header Host $host;");
sb.AppendLine($"{tabs}proxy_set_header X-Real-IP $remote_addr;");
sb.AppendLine($"{tabs}proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;");
sb.AppendLine($"{tabs}proxy_set_header X-Forwarded-Host $host;");
sb.AppendLine($"{tabs}proxy_set_header X-Forwarded-Server $host;");
sb.AppendLine($"{tabs}proxy_set_header X-Forwarded-Proto $scheme;");
sb.AppendLine($"{tabs}proxy_set_header X-Forwarded-Path $request_uri;");
and have a few senaries that the UseForwardedHeaders dont cover or I am missing documentation on how to make it work.
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.All
});
basically I have to run the following also to make it work
private const string XForwardedPathBase = "X-Forwarded-PathBase";
private const string XForwardedProto = "X-Forwarded-Proto";
app.Use((context, next) =>
{
if (context.Request.Headers.TryGetValue(XForwardedPathBase, out StringValues pathBase))
{
context.Request.PathBase = new PathString(pathBase);
}
if (context.Request.Headers.TryGetValue(XForwardedProto, out StringValues proto))
{
context.Request.Protocol = proto;
}
return next();
});
To update the protocol in those cases where nginx do the ssl offloading and the backend app just uses http. And to set the PathBase, for those cases when nginx has removed part of the path when passing the request forward.
If UseForwardedHeaders can do this already, how do i do it?
X-forwarded-proto support is built in, but it maps to scheme, not protocol (http/1.1).
With PathBase make sure you're creating your PathString with the unescaped value.
thanks for the added info. i will try a few thing then to find out why my identity server generates wrong urls. https://www.earthml.com/identity/.well-known/openid-configuration (the urls it generate should also be https )
context.Request.Protocol = proto; was a typo
Here is my working solution, so somehow the forward middleware do not work:

Next step is properly to check some case sensitive stuff, i properly made a mistake there
Can you log your headers as received by the app? The middleware is pretty strict.
Ye, i am off for a few hours- then i will try to log out some requests to see what actually happens. Thanks for helping
Here are the headers. Will start investigate the code to see where it can go wrong.
["X-Forwarded-Server"]=["www.earthml.com"],
["X-Forwarded-Proto"]=["https"]
["X-Forwarded-Path"]=["/identity/connect/authorize/consent?client_id=EarthML.Mapify&redirect_uri=https%3A%2F%2Flocalhost%3A44377%2F&response_type=id_token%20token&scope=openid%20profile&state=621bc11cd0674059994828b401561a8c&nonce=7f2766fd2c3f4393abebf1d3c0630e93"]
["X-Forwarded-PathBase"]=["/identity/"]
You have it set to ForwardedHeaders.All, but you don't have an x-Forwarded-For header, so it freaks out and won't process Proto either. Scope it down to just the header you need.
@pksorensen would you be willing to draft the document for this issue?
@Rick-Anderson Sure, not sure what exactly that means and what I should do :) The problem was resolved based on @Tratcher comment, about changing ForwardedHeaders.All to the headers actually used by my setup.
@Tratcher Is it ideal that it is this strict? Since the config lays within the application, meaning that if the guy responsible for the nginx setup changes the config, the the app breaks. Worth considering to use thoes headers present when set to all?
We have an open bug for relaxing some of the defaults to make it easier to use (https://github.com/aspnet/BasicMiddleware/issues/190).
However, we still need a writeup explaining the usage scenarios. See https://github.com/aspnet/Docs/issues/2384#issuecomment-270195458