问题描述
如果我按照官方文档使用.NET Core 2.0强制实施HTTPS,则该网站无法加载,并显示错误ERR_TOO_MANY_REDIRECTS.
Using .NET Core 2.0 if I follow the official documentation to enforce HTTPS the website fails to load with the error ERR_TOO_MANY_REDIRECTS.
复制步骤:
- 创建新的.NET Razor应用程序:dotnet新剃须刀-o aspnetcoreapp
- 从此处详细介绍的文档中添加两个代码段: https://docs.microsoft. com/en-us/aspnet/core/security/enforcing-ssl?view = aspnetcore-2.0
- 部署到在Ubuntu 17.10上运行的Apache2(未在较早版本上进行测试),其中conf使用以下代码将连接传递到Kestrel服务器:
- Create new .NET Razor App:dotnet new razor -o aspnetcoreapp
- Add the two snippits of code from documentation detailed here:https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl?view=aspnetcore-2.0
- Deploy to Apache2 running on Ubuntu 17.10 (not tested on older versions) where conf passes connection to Kestrel server using this code:
ProxyPreserveHost OnProxyPass / http://127.0.0.1:5001/ProxyPassReverse / http://127.0.0.1:5001/
ProxyPreserveHost OnProxyPass / http://127.0.0.1:5001/ProxyPassReverse / http://127.0.0.1:5001/
如果删除了第2步中的代码,则该网站可以通过HTTP或HTTPS正常运行(但显然不会重定向),但是如果添加了此代码,则该站点无法通过HTTP或HTTPS加载ERR_TOO_MANY_REDIRECTS.似乎方法已随.NET Core 2.1更改,但尚未发布.
If code from step 2 is removed then the website works fine over HTTP or HTTPS (but obviously does not redirect) but if this code is added the site fails to load with ERR_TOO_MANY_REDIRECTS over either HTTP or HTTPS.It seems the methods have changed with .NET Core 2.1 but this has not yet been released.
推荐答案
您已将传递设置为http
,而不是https
.这里的Apache作为反向代理运行,因此它只是将请求转发到您的直通服务器,该服务器将始终根据此处的配置通过http
执行.然后,由于将ASP.NET Core应用程序配置为需要HTTPS,因此它将重定向,从而导致新请求命中Apache,然后再次通过http
转发 .
You've set your passthrough as http
, rather than https
. Apache here is running as a reverse proxy, so it simply forwards the request on to your passthrough server(s), which it will always do over http
, based on your config here. Then, since the ASP.NET Core app is configured to require HTTPS, it redirects, causing a new request to hit Apache, which it then forwards again over http
.
很长很短,您需要直通https
,而不是http
.另外,您可以从ASP.NET Core应用程序中删除仅HTTPS要求,而在Apache中强制实施HTTPS.
Long and short, you need to passthrough to https
, not http
. Alternatively, you can remove the HTTPS-only requirement from your ASP.NET Core app, and instead enforce HTTPS in Apache.
这篇关于.NET Core-RequireHttpsAttribute导致在具有Apache2的Ubuntu上出现ERR_TOO_MANY_REDIRECTS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!