为什么 X-Forwarded-Proto 在 Elastic Beanstalk 上始终设置为 HTTP?

Mat*_*ler 3 https amazon-web-services amazon-elb amazon-elastic-beanstalk nginx-reverse-proxy

设置

你好。我正在将 ASP.Net Core 应用程序部署到 AWS Elastic Beanstalk。我运行的平台是64位Amazon Linux 2/2.1.5,使用Nginx作为代理服务器软件。我在环境配置中为负载均衡器设置了一对侦听器。它们的设置如下:

  • Port=443 Protocol=HTTPS SSL=certificate Process=default
  • Port=80 Protocal=HTTP Process=default

我有一个流程:

Name=default Port=80 Protocol=HTTPS

问题

在我的 ASP.Net Core 服务器上,我尝试检查服务器的原始客户端是否通过 HTTPS 或 HTTP 进行通信。据我了解,X-Forwarded-Proto请求的标头应包含此信息。但是,无论客户端如何连接到服务器,的值X-Forwarded-Proto始终为。为什么即使从我的网络浏览器连接时也从未设置为?httpX-Forwarded-Protohttps

预先感谢您的任何帮助!

Mat*_*ler 9

正如 @MarkB 指出的,问题出在 Nginx 配置中。AWS Elastic Beanstalk 有一个默认配置文件00_application.conf,这/etc/nginx/conf.d/elasticbeanstalk是罪魁祸首。它有一个声明:

proxy_set_header    X-Forwarded-Proto     $scheme;
Run Code Online (Sandbox Code Playgroud)

需要改为:

proxy_set_header    X-Forwarded-Proto     $http_x_forwarded_proto;
Run Code Online (Sandbox Code Playgroud)

为了覆盖此文件,我使用了此处详细介绍的方法: 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;
}
Run Code Online (Sandbox Code Playgroud)

我还必须向我的 ASP.Net Core 应用程序添加一个中间件,以使用转发的标头,如本答案中所述:Redirect URI sent as HTTP and not HTTPS in app running HTTPS

我将以下内容添加到我的Startup.cs

proxy_set_header    X-Forwarded-Proto     $scheme;
Run Code Online (Sandbox Code Playgroud)

我希望这对其他人有帮助!