本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
配置反向代理
Elastic Beanstalk 使用 nginx
默认情况下,Elastic Beanstalk 将 nginx 代理配置为通过端口 5000 向您的应用程序转发请求。您可以覆盖默认端口,方法是将 PORT
环境属性设置为主应用程序侦听的端口。
注意
应用程序侦听的端口不会影响 nginx 服务器为了从负载均衡器接收请求而侦听的端口。
在平台版本上配置代理服务器
所有 AL2 023/ AL2 平台都支持统一的代理配置功能。有关在运行 AL2 023/ 的平台版本上配置代理服务器的更多信息AL2,请参阅。在 Elastic Beanstalk 上配置反向代理
如果您的 Elastic Beanstalk Java SE 环境使用亚马逊 Linux 平台版本(AMI亚马逊 Linux 2 之前的版本),请阅读本节中的其他信息。
注意
-
本主题中的信息仅适用于基于 Amazon Linux AMI (AL1) 的平台分支。AL2023/ AL2 平台分支与之前的 Amazon Linux AMI (AL1) 平台版本不兼容,需要不同的配置设置。
-
2022 年 7 月 18 日,Elastic Beanstalk 将所有基于亚马逊 Linux () 的平台分支的状态设置为停用。AMI AL1有关迁移到当前且完全受支持的 Amazon Linux 2023 平台分支的更多信息,请参阅 将 Elastic Beanstalk Linux 应用程序迁移到 Amazon Linux 2023 或 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 增强了运行状况报告和监控、自动应用程序映射和静态文件的 Elastic Beanstalk 配置。
include conf.d/elasticbeanstalk/*.conf;
以下示例配置来自 Scorekeep 示例应用程序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; } }