

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

# 反向代理配置
<a name="platforms-linux-extend.proxy"></a>

所有 Amazon Linux 2 和 Amazon Linux 2023 平台版本都使用 nginx 作为其默认的反向代理服务器。Tomcat、Node.js、PHP 和 Python 平台也支持将 Apache HTTPD 作为替代方案。要在这些平台上选择 Apache，请将 `aws:elasticbeanstalk:environment:proxy` 命名空间中的 `ProxyServer` 选项设置为 `apache`。所有平台都以一致的方式启用代理服务器配置，如本节所述。

**注意**  
在 Amazon Linux AMI 平台版本（Amazon Linux 2 之前的版本），您需要以不同的方式配置代理服务器。您可以在本指南中的[相应平台主题](concepts-all-platforms.md)下找到这些旧的详细信息。

Elastic Beanstalk 在环境实例上将代理服务器配置为向环境根 URL 的主要 Web 应用程序转发 Web 流量；例如，`http://my-env.elasticbeanstalk.com`。

默认情况下，Elastic Beanstalk 将代理配置为通过端口 5000 向主要 Web 应用程序转发 80 端口上的传入请求。通过使用配置文件中的 [aws:elasticbeanstalk:application:environment](command-options-general.md#command-options-general-elasticbeanstalkapplicationenvironment) 命名空间来设置 `PORT` 环境属性，您可以配置此端口号，如以下示例所示。

```
option_settings:
  - namespace:  aws:elasticbeanstalk:application:environment
    option_name:  PORT
    value:  {{<main_port_number>}}
```

有关设置应用程序环境变量的更多信息，请参阅[选项设置](ebextensions-optionsettings.md)。

您的应用程序应侦听代理中为其配置的端口。如果使用 `PORT` 环境属性更改默认端口，代码可以通过读取 `PORT` 环境变量的值来访问该端口。例如，在 Go 中调用 `os.Getenv("PORT")`，或者在 Java 中调用 `System.getenv("PORT")`。如果您将代理配置为向多个应用程序进程发送流量，则可以配置多个环境属性，并在代理配置和应用程序代码中使用它们的值。另一种选择是将端口值作为 `Procfile` 中的命令参数传递给进程。有关更多信息，请参阅 [Buildfile 和 Procfile](platforms-linux-extend.build-proc.md)。

## 配置 nginx
<a name="platforms-linux-extend.proxy.nginx"></a>

Elastic Beanstalk 使用 nginx 作为默认反向代理，将应用程序映射到 Elastic Load Balancing 负载均衡器。Elastic Beanstalk 提供一个默认 nginx 配置，您可以扩展该配置，或者将其完全替换为您自己的配置。

**注意**  
添加或编辑 nginx `.conf` 配置文件时，请务必将其编码为 UTF-8。

要扩展 Elastic Beanstalk 的默认 nginx 配置，请将 `.conf` 配置文件添加到您的应用程序源包的 `.platform/nginx/conf.d/` 文件夹中。Elastic Beanstalk nginx 配置自动在此文件夹中包括 `.conf` 文件。

```
~/workspace/my-app/
|-- .platform
|   `-- nginx
|       `-- conf.d
|           `-- myconf.conf
`-- {{other source files}}
```

`.platform/nginx/conf.d/` 中的配置文件包含在 nginx 配置的 `http` 块中。请将全局适用的配置存储在此位置。

若要扩展默认的 nginx `server` 块配置，请将 `.conf` 配置文件添加到您应用程序源捆绑包中名为 `.platform/nginx/conf.d/elasticbeanstalk/` 的文件夹中。Elastic Beanstalk nginx 配置包含 `server` 块中此文件夹中的 `.conf` 文件。

```
~/workspace/my-app/
|-- .platform
|   `-- nginx
|       `-- conf.d
|           `-- elasticbeanstalk
|               `-- server.conf
`-- {{other source files}}
```

使用此位置添加服务器特定的配置，例如其他位置块、自定义错误页面或服务器级指令。以下示例添加了一个自定义位置块。

**Example 。 platform/nginx/conf.d/elasticbeanstalk/server.conf**  

```
location /test {
    return 200 "Hello World!";
    add_header Content-Type text/plain;
}
```

要完全覆盖 Elastic Beanstalk 默认 nginx 配置，请在您的源包的 `.platform/nginx/nginx.conf` 处包含一个配置：

```
~/workspace/my-app/
|-- .platform
|   `-- nginx
|       `-- nginx.conf
`-- {{other source files}}
```

如果要覆盖 Elastic Beanstalk nginx 配置，请将以下行添加到 `nginx.conf`，以便加入适用于 [Elastic Beanstalk 中的增强型运行状况报告和监控](health-enhanced.md)、自动应用程序映射和静态文件的 Elastic Beanstalk 配置。

```
 include conf.d/elasticbeanstalk/*.conf;
```

## 配置 Apache HTTPD
<a name="platforms-linux-extend.proxy.httpd"></a>

Tomcat、Node.js、PHP 和 Python 平台允许您选择 Apache HTTPD 代理服务器作为 nginx 的替代方案。这不是默认值。以下示例将 Elastic Beanstalk 配置为使用 Apache HTTPD。

**Example .ebextensions/httpd-proxy.config**  

```
option_settings:
  aws:elasticbeanstalk:environment:proxy:
    ProxyServer: apache
```
您可以使用其他配置文件扩展 Elastic Beanstalk 默认 Apache 配置。也可以完全覆盖 Elastic Beanstalk 默认 Apache 配置。  
要扩展 Elastic Beanstalk 默认 Apache 配置，请将 `.conf` 配置文件添加到应用程序源包中名为 `.platform/httpd/conf.d` 的文件夹中。Elastic Beanstalk Apache 配置自动在此文件夹中包括 `.conf` 文件。  

```
~/workspace/my-app/
|-- .ebextensions
|   -- httpd-proxy.config
|-- .platform
|   -- httpd
|      -- conf.d
|         -- port5000.conf
|         -- ssl.conf
-- index.jsp
```
例如，以下 Apache 2.4 配置将在端口 5000 上添加一个监听器。  

**Example 。 platform/httpd/conf.d/port5000.conf**  

```
listen 5000
<VirtualHost *:5000>
  <Proxy *>
    Require all granted
  </Proxy>
  ProxyPass / http://localhost:8080/ retry=0
  ProxyPassReverse / http://localhost:8080/
  ProxyPreserveHost on

  ErrorLog /var/log/httpd/elasticbeanstalk-error_log
</VirtualHost>
```
要完全覆盖 Elastic Beanstalk 默认 Apache 配置，请在源包的 `.platform/httpd/conf/httpd.conf` 处包括一个配置。  

```
~/workspace/my-app/
|-- .ebextensions
|   -- httpd-proxy.config
|-- .platform
|   `-- httpd
|       `-- conf
|           `-- httpd.conf
`-- index.jsp
```
如果要覆盖 Elastic Beanstalk Apache 配置，请将以下行添加到 `httpd.conf`，以便加入适用于 [Elastic Beanstalk 中的增强型运行状况报告和监控](health-enhanced.md)、自动应用程序映射和静态文件的 Elastic Beanstalk 配置。  

```
IncludeOptional conf.d/elasticbeanstalk/*.conf
```