我试图将https配置到Windows Server 2012上IIS上托管的网站。我的项目是ASP.NET Core 2.1中的默认WebApi应用程序,并且我使用win-acme来配置带有Let's Encrypt证书的IIS。
除了HTTP流量未重定向到https之外,其他一切运行正常。如果查看日志,则会得到:
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3] Failed to determine the https port for redirect.
我认为可以通过将其添加到我的ConfigureServices
方法中来轻松解决:
services.AddHttpsRedirection(options =>
{
options.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
options.HttpsPort = 443;
});
但是,现在我根本无法访问我的网站。Chrome给我一条消息:
此页面无法正常运行www.example.com将您重定向了太多次
如果我查看日志,它显示出很多这样的行:
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 GET http://example.com info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 0.1026ms 307 info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1] Request starting HTTP/1.1 GET http://example.com info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Request finished in 0.1837ms 307
有人知道我在做什么错吗?
我刚刚找到答案。可以通过将以下代码添加到ConfigureServices.cs来解决此问题:
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardLimit = 2;
options.KnownProxies.Add(IPAddress.Parse("127.0.10.1"));
options.ForwardedForHeaderName = "X-Forwarded-For-My-Custom-Header-Name";
});
来源:https : //docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1#forwarded-headers-middleware-options
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句