

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 配置 HTTP 到 HTTPS 重定向
<a name="configuring-https-httpredirect"></a>

本主题介绍如何在最终用户仍在启动应用程序时处理到该应用程序的 HTTP 流量。为此，您需要配置 *HTTP to HTTPS redirection (HTTP 到 HTTPS 重定向)*，有时也称为*强制 HTTPS*。

要配置重定向，您首先将环境配置为处理 HTTPS 流量。然后，您可以将 HTTP 流量重定向到 HTTPS。下面的小节将讨论这两个步骤。

## 配置您的环境以处理 HTTPS 流量
<a name="configuring-https-httpredirect.https"></a>

根据环境的负载均衡配置，执行以下操作之一：
+ **负载均衡环境** – [配置负载均衡器以终止 HTTPS](configuring-https-elb.md)。
+ **单实例环境** – [配置您的应用程序以终止实例中的 HTTPS 连接](https-singleinstance.md)。此配置取决于您环境的平台。

## 将 HTTP 流量重定向到 HTTPS
<a name="configuring-https-httpredirect.redirect"></a>

要将应用程序的 HTTP 流量重定向到 HTTPS，您可以配置环境实例上的 Web 服务器，也可以配置环境的应用程序负载均衡器。

**配置实例 Web 服务器**  
此方法适用于任何 Web 服务器环境。在您的 Amazon EC2 实例上配置网络服务器，使其以 HTTP 重定向响应状态响应 HTTP 流量。

此配置取决于您环境的平台。在上的[https://github.com/awsdocs/elastic-beanstalk-samples/tree/master/configuration-files/aws-provided/security-configuration/https-redirect](https://github.com/awsdocs/elastic-beanstalk-samples/tree/master/configuration-files/aws-provided/security-configuration/https-redirect)集合中找到适用于您平台的文件夹 GitHub，然后使用该文件夹中的示例配置文件。

如果您的环境使用 [Elastic Load Balancing 运行状况检查](using-features.healthstatus.md#using-features.healthstatus.understanding)，负载均衡器需要运行状况良好的实例用 HTTP 200 (OK) 响应来响应 HTTP 运行状况检查消息。因此，您的 Web 服务器不应将这些消息重定向到 HTTPS。[https://github.com/awsdocs/elastic-beanstalk-samples/tree/master/configuration-files/aws-provided/security-configuration/https-redirect](https://github.com/awsdocs/elastic-beanstalk-samples/tree/master/configuration-files/aws-provided/security-configuration/https-redirect) 中的示例配置文件可正确处理这一需求。

**配置负载均衡器**  
如果您拥有使用[应用程序负载均衡器](environments-cfg-alb.md)的负载均衡环境，则此方法有效。当 HTTP 流量进入时，应用程序负载均衡器可以发送重定向响应。在这种情况下，不需要在您环境的实例上配置重定向。

我们有两个示例配置文件 GitHub ，它们显示了如何配置 Application Load Balancer 以进行重定向。
+ [https://github.com/awsdocs/elastic-beanstalk-samples/blob/master/configuration-files/aws-provided/resource-configuration/alb-http-to-https-redirection-full.config](https://github.com/awsdocs/elastic-beanstalk-samples/blob/master/configuration-files/aws-provided/resource-configuration/alb-http-to-https-redirection-full.config) 配置文件在端口 443 上创建 HTTPS 侦听器，并修改默认端口 80 侦听器以将传入的 HTTP 流量重定向到 HTTPS。
+ [https://github.com/awsdocs/elastic-beanstalk-samples/blob/master/configuration-files/aws-provided/resource-configuration/alb-http-to-https-redirection.config](https://github.com/awsdocs/elastic-beanstalk-samples/blob/master/configuration-files/aws-provided/resource-configuration/alb-http-to-https-redirection.config) 配置文件需要定义 443 侦听器。要定义它，您可以使用标准 Elastic Beanstalk 配置命名空间或 Elastic Beanstalk 控制台。然后，它负责修改端口 80 侦听器以便执行重定向。