

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

# 設定代理伺服器
<a name="java-se-nginx"></a>

Elastic Beanstalk 使用 [nginx](https://www.nginx.com/) 做為反向代理伺服器，在連接埠 80 上將您的應用程式映射到 Elastic Load Balancing 負載平衡器。Elastic Beanstalk 提供了預設的 nginx 組態，您可以加以擴展，或使用自己的組態將其完全覆寫。

Elastic Beanstalk 預設會設定 nginx 代理將請求轉送至連接埠 5000 上的應用程式。您可將 `PORT` [環境屬性](java-se-platform.md#java-se-options)設定為主要應用程式接聽的連接埠，藉此覆寫預設連接埠。

**注意**  
您應用程式接聽的連接埠，不會影響 nginx 伺服器為接收來自負載平衡器的請求所接聽的連接埠。

**在您的平台版本上設定代理伺服器**  
所有 AL2023/AL2 平台皆支援統一的代理組態功能。如需在執行 AL2023/AL2 的平台版本上設定代理伺服器的詳細資訊，請參閱 [反向代理組態](platforms-linux-extend.proxy.md)。

## 在 Amazon Linux AMI (之前的 Amazon Linux 2) 上設定代理
<a name="java-se-nginx.alami"></a>

如果您的 Elastic Beanstalk Java SE 環境使用 Amazon Linux AMI 平台版本 (Amazon Linux 2 之前)，請閱讀本節中的其他資訊。

**備註**  
本主題中的資訊僅適用於以 Amazon Linux AMI (AL1) 為基礎的平台分支。AL2023/AL2 平台分支與舊版 Amazon Linux AMI (AL1) 平台版本不相容，*需要不同的組態設定*。
 在 [2022 年 7 月 18 日，](https://docs.aws.amazon.com/elasticbeanstalk/latest/relnotes/release-2022-07-18-linux-al1-retire.html)Elastic Beanstalk 會根據 Amazon Linux AMI (AL1) 將所有平台分支的狀態設定為**已淘汰**。如需有關遷移至完全支援的目前 Amazon Linux 2023 平台分支的詳細資訊，請參閱 [將您的 Elastic Beanstalk Linux 應用程式遷移到 Amazon Linux 2023 或 Amazon Linux 2](using-features.migration-al.md)。

### 擴展和覆寫預設的代理組態 — Amazon Linux AMI (AL1)
<a name="java-se-nginx.alami.extending"></a>

若要擴展 Elastic Beanstalk 的預設 nginx 組態，請將 `.conf` 組態檔案加進您應用程式原始碼套件中名為 `.ebextensions/nginx/conf.d/` 的資料夾。Elastic Beanstalk nginx 組態會自動在此資料夾中加入 `.conf` 檔案。

```
~/workspace/my-app/
|-- .ebextensions
|   `-- nginx
|       `-- conf.d
|           `-- myconf.conf
`-- web.jar
```

若要完全覆寫 Elastic Beanstalk 預設 nginx 組態，請在 `.ebextensions/nginx/nginx.conf` 的原始碼套件中加入組態：

```
~/workspace/my-app/
|-- .ebextensions
|   `-- nginx
|       `-- nginx.conf
`-- web.jar
```

如果您覆寫了 Elastic Beanstalk nginx 組態，請在 `nginx.conf` 中加入下列行，以納入適用於 [Elastic Beanstalk 中的增強型運作狀態報告和監控](health-enhanced.md)、自動應用程式映射和靜態檔案的 Elastic Beanstalk 組態。

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

以下來自 [Scorekeep 範例應用程式](https://github.com/aws-samples/eb-java-scorekeep/)的範例組態會覆寫 Elastic Beanstalk 的預設組態，以提供 `public` 的 `/var/app/current` 子目錄的靜態 Web 應用程式，其中 Java SE 平台複製應用程式原始程式碼。`/api` 位置轉發流量以在 `/api/` 下路由傳送到連接埠 5000 上的 Spring 應用程式接聽。根路徑的 Web 應用程式提供其他所有流量。

**Example**  

```
user                    nginx;
error_log               /var/log/nginx/error.log warn;
pid                     /var/run/nginx.pid;
worker_processes        auto;
worker_rlimit_nofile    33282;

events {
    worker_connections  1024;
}

http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

  include       conf.d/*.conf;

  map $http_upgrade $connection_upgrade {
      default     "upgrade";
  }

  server {
      listen        80 default_server;
      root /var/app/current/public;

      location / {
      }git pull
      

      location /api {
          proxy_pass          http://127.0.0.1:5000;
          proxy_http_version  1.1;

          proxy_set_header    Connection          $connection_upgrade;
          proxy_set_header    Upgrade             $http_upgrade;
          proxy_set_header    Host                $host;
          proxy_set_header    X-Real-IP           $remote_addr;
          proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
      }

      access_log    /var/log/nginx/access.log main;

      client_header_timeout 60;
      client_body_timeout   60;
      keepalive_timeout     60;
      gzip                  off;
      gzip_comp_level       4;

      # Include the Elastic Beanstalk generated locations
      include conf.d/elasticbeanstalk/01_static.conf;
      include conf.d/elasticbeanstalk/healthd.conf;
  }
}
```