本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
反向代理配置
所有 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 之前的版本),您需要以不同的方式配置代理服务器。您可以在本指南中的相应平台主题下找到这些旧的详细信息。
Elastic Beanstalk 在环境实例上将代理服务器配置为向环境根 URL 的主要 Web 应用程序转发 Web 流量;例如,http://my-env.elasticbeanstalk.com
。
默认情况下,Elastic Beanstalk 将代理配置为通过端口 5000 向主要 Web 应用程序转发 80 端口上的传入请求。通过使用配置文件中的 aws:elasticbeanstalk:application:environment 命名空间来设置 PORT
环境属性,您可以配置此端口号,如以下示例所示。
option_settings:
- namespace: aws:elasticbeanstalk:application:environment
option_name: PORT
value: <main_port_number>
有关设置应用程序环境变量的更多信息,请参阅选项设置。
您的应用程序应侦听代理中为其配置的端口。如果使用 PORT
环境属性更改默认端口,代码可以通过读取 PORT
环境变量的值来访问该端口。例如,在 Go 中调用 os.Getenv("PORT")
,或者在 Java 中调用 System.getenv("PORT")
。如果您将代理配置为向多个应用程序进程发送流量,则可以配置多个环境属性,并在代理配置和应用程序代码中使用它们的值。另一种选择是将端口值作为 Procfile
中的命令参数传递给进程。有关更多信息,请参阅Buildfile 和 Procfile。
配置 nginx
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
要完全覆盖 Elastic Beanstalk 默认 nginx 配置,请在您的源包的 .platform/nginx/nginx.conf
处包含一个配置:
~/workspace/my-app/
|-- .platform
| `-- nginx
| `-- nginx.conf
`-- other source files
如果要覆盖 Elastic Beanstalk nginx 配置,请将以下行添加到 nginx.conf
,以便加入适用于 Elastic Beanstalk 增强型运行状况报告和监控、自动应用程序映射和静态文件的 Elastic Beanstalk 配置。
include conf.d/elasticbeanstalk/*.conf;
配置 Apache HTTPD
Tomcat、Node.js、PHP 和 Python 平台允许您选择 Apache HTTPD 代理服务器作为 nginx 的替代方案。这不是默认值。以下示例将 Elastic Beanstalk 配置为使用 Apache HTTPD。
例 .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 上添加一个监听器。
例 .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 增强型运行状况报告和监控、自动应用程序映射和静态文件的 Elastic Beanstalk 配置。
IncludeOptional conf.d/elasticbeanstalk/*.conf