无法访问托管在 EBS 上的 Spring Boot 应用程序

Posted

技术标签:

【中文标题】无法访问托管在 EBS 上的 Spring Boot 应用程序【英文标题】:Unable to access spring boot application hosted on EBS 【发布时间】:2018-01-13 12:40:27 【问题描述】:

我在 AWS elastic-beanstalk 上托管了我的 Spring Boot 应用程序,但是当我尝试使用公共 URL 访问它时,它给了我 502 网关错误。 在日志中,我可以看到以下错误: 2017/08/05 20:10:33 [错误] 22965#0: *157 连接()失败(111:连接被拒绝)而 连接到上游,客户端:XXX.68.241.XXX,服务器:,请求:“GET /swagger-ui.html HTTP/1.1”,上游:“http://127.0.0.1:5000/swagger-ui.html”,主机: "XXXXXXXX-env.XXX.us-west-2.elasticbeanstalk.com"

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events 
  worker_connections 1024;


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


  upstream tomcat_servers
    ip_hash;
    server 127.0.0.1:5000;
    keepalive 256;
  

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

  sendfile            on;
  tcp_nopush          on;
  tcp_nodelay         on;
  keepalive_timeout   65;
  types_hash_max_size 2048;

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



【问题讨论】:

安全组80端口是否开放?这是单个实例还是负载平衡后面?在公共子网上? @strongjz 再次查看日志中的错误消息。与实例和安全组的连接正常。 @strongjz 是的 80 在安全组中打开。 nginx 正在向127.0.0.1:5000/swagger-ui.html 发送请求,但它仍然给出 502 错误 您能否发布您的 nginx 配置和 beanstalk 应用程序的 .ebextensions。 @strongjz 我已经用 nginx 配置更新了我的问题 【参考方案1】:

我会仔细检查您的 Spring Boot 应用程序正在运行的端口。

默认情况下,Spring Boot 应用程序将侦听端口 8080。Elastic Beanstalk 假定应用程序将侦听端口 5000。有两种方法可以解决此差异:更改 Elastic Beanstalk 配置使用的端口,或更改端口Spring Boot 应用程序监听。对于这篇文章,我们将更改 Spring Boot 应用程序侦听的端口。

最简单的方法是在 Elastic Beanstalk 环境中指定 SERVER_PORT 环境变量,并将值设置为 5000。(配置属性名称为 server.port,但 Spring Boot 允许您指定更多的环境变量 -友好名称)。

https://aws.amazon.com/blogs/devops/deploying-a-spring-boot-application-on-aws-using-aws-elastic-beanstalk/

Answered here as well

Help on how to update the port

【讨论】:

我没有在 EBS 环境配置中添加端口号

以上是关于无法访问托管在 EBS 上的 Spring Boot 应用程序的主要内容,如果未能解决你的问题,请参考以下文章

如何将 EBS 上 Rails 应用程序上的入站安全组限制为仅来自我的应用程序(在 S3 上)并防止其他服务器访问我的 API?

如何为安装在 Windows EC2 上的 ebs 卷标记 Windows 驱动器

从托管在 GitHub 上的 Spring Cloud Config Server 访问的 repo 的身份验证问题

Spring Boot Docker Container 无法连接到托管在 Docker 主机上的 postgresql

无法从托管在 Websphere 应用程序服务器 v7.0 上的应用程序访问 HTTPS 服务

我无法从 Windows 托管的 Web API 上的 IIS 访问 wkhtmltopdf