

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 反向代理組態
<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 會設定代理，以將 80 連接埠的傳入請求轉送至連接埠 5000 上的主要 Web 應用程式。您可以在組態檔案中使用 [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
```