当请求通过反向代理时,.NET 核心应用程序中的方案不正确

Incorrect scheme in .NET core app when request goes through reverse-proxy

我使用 apache 作为反向代理。在 apache 后面有 .NET 核心应用程序。两者都使用 HTTPS。 问题是,当我通过代理访问此 .NET 应用程序时,.NET 报告请求是在没有使用 SSL 的情况下使用 http 发出的。

Apache 代理配置:

SSLProxyEngine on
<Location "/">
    ProxyPass "https://domain:8444/"
    ProxyPassReverse "https://domain:8444/"
</Location>

可以通过 https://domain/.

访问 Apache

当我通过https://domain:8444/地址访问应用程序时,然后是httpContext.Request.IsHttps==truehttpContext.Request.Scheme=="https",但是当我通过[访问应用程序时=15=] 然后 httpContext.Request.IsHttps==falsehttpContext.Request.Scheme=="http".

当我用 PHP 尝试相同的配置时,一切正常。 有什么我可以做的吗?

首先,您必须确保您的 Apache 正在设置 X-Forwarded-ForX-Forwarded-Proto headers。

其次,您需要让您的 ASP.NET 核心应用程序知道它。来自 docs:

app.UseForwardedHeaders(new ForwardedHeadersOptions
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

这已经在 UseIISIntegration() 中发生(请参阅 source),但您可能已将其删除(或者它未在您使用的模板中设置)。