設定反向代理程式 - AWS Elastic Beanstalk

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

設定反向代理程式

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

Elastic Beanstalk 預設會設定 nginx 代理將請求轉送至連接埠 5000 上的應用程式。您可將 PORT 環境屬性設定為主要應用程式接聽的連接埠,藉此覆寫預設連接埠。

注意

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

在您的平台版本上設定代理伺服器

所有 AL2023/AL2 平台皆支援統一的代理組態功能。如需有關在執行 AL2023/AL2 的平台版本上設定代理伺服器的詳細資訊,請展開擴充 Elastic Beanstalk Linux 平台中的反向代理組態一節。

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

備註

若要擴展 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 組態。

include conf.d/elasticbeanstalk/*.conf;

以下來自 Scorekeep 範例應用程式的範例組態會覆寫 Elastic Beanstalk 的預設組態,以提供 public/var/app/current 子目錄的靜態 Web 應用程式,其中 Java SE 平台複製應用程式原始程式碼。/api 位置轉發流量以在 /api/ 下路由傳送到連接埠 5000 上的 Spring 應用程式接聽。根路徑的 Web 應用程式提供其他所有流量。

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; } }