正如@MarkB 所指出的,问题出在 Nginx 配置中。AWS Elastic Beanstalk 有一个默认配置文件00_application.conf,/etc/nginx/conf.d/elasticbeanstalk这是罪魁祸首。它有一个声明:
proxy_set_header X-Forwarded-Proto $scheme;
需要更改为:
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
为了覆盖这个文件,我使用了这里详述的方法:https ://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html 。
.platform/nginx/conf.d/elasticbeanstalk我在已部署项目的根目录中添加了一个文件。它包含:
location / {
proxy_pass http://127.0.0.1:5000;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
}
我还必须在我的 ASP.Net Core 应用程序中添加一个中间件,以使用转发的标头,如此答案中所述:Redirect URI sent as HTTP and not HTTPS in app running HTTPS。
我将以下内容添加到我的Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
//...
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto;
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
//...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//...
app.UseForwardedHeaders();
//...
}
我希望这对其他人有帮助!