配置反向代理 - Amazon Elastic Beanstalk
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

配置反向代理

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