简体   繁体   English

使用 Puma 在 Elastic Beanstalk 上部署的 Rails 应用程序失败 - 每个请求都出现 502 错误

[英]Rails application deployed on Elastic Beanstalk with Puma fails - 502 errors on every request

I just deployed a Rails app to Elastic Beanstalk, and every request is giving me a 502 error.我刚刚将一个 Rails 应用程序部署到 Elastic Beanstalk,每个请求都给我一个 502 错误。

Here's the contents of /var/logs/nginx/error.log这是 /var/logs/nginx/error.log 的内容

2015/05/20 16:24:25 [warn] 1535#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
2015/05/20 16:27:12 [crit] 1537#0: *20 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.51.94, server: _, request: "POST /get HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/get", host: "securities-api-prod.elasticbeanstalk.com"
2015/05/20 16:27:17 [crit] 1537#0: *20 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.51.94, server: _, request: "POST /get HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/get", host: "securities-api-prod.elasticbeanstalk.com"
2015/05/20 16:27:19 [crit] 1537#0: *20 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.51.94, server: _, request: "POST /get HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/get", host: "securities-api-prod.elasticbeanstalk.com"
2015/05/20 16:27:22 [crit] 1537#0: *16 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.44.210, server: _, request: "GET /datapoint?tickers=AAPL&datapoints=Ratings HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/datapoint?tickers=AAPL&datapoints=Ratings", host: "securities-api-prod.elasticbeanstalk.com"
2015/05/20 16:27:27 [crit] 1537#0: *20 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.51.94, server: _, request: "POST /get HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/get", host: "securities-api-prod.elasticbeanstalk.com"
2015/05/20 16:27:32 [crit] 1537#0: *20 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.51.94, server: _, request: "POST /get HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/get", host: "securities-api-prod.elasticbeanstalk.com"
2015/05/20 16:28:53 [crit] 1537#0: *52 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.51.94, server: _, request: "GET /datapoint?tickers=AAPL&datapoints=Ratings HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/datapoint?tickers=AAPL&datapoints=Ratings", host: "securities-api-prod.elasticbeanstalk.com"
2015/05/20 16:30:47 [crit] 1537#0: *69 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.51.94, server: _, request: "POST /get HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/get", host: "securities-api-prod.elasticbeanstalk.com"

I think puma is running -我认为 puma 正在运行 -

[ec2-user@ip-172-31-44-135 nginx]$ ps aux | grep puma
root     23299  1.0  0.2  53008  1428 ?        Ss   16:38   0:00 su -s /bin/bash -c puma -C /opt/elasticbeanstalk/support/conf/pumaconf.rb webapp
webapp   23314  0.0  2.4  75764 14604 ?        Rsl  16:38   0:00 /opt/rubies/ruby-2.1.4/bin/ruby /opt/rubies/ruby-2.1.4/bin/puma -C /opt/elasticbeanstalk/support/conf/pumaconf.rb
ec2-user 23317  0.0  0.1 110284   844 pts/0    S+   16:38   0:00 grep puma

pumaconf.rb pumaconf.rb

directory '/var/app/current'
threads 8, 32
workers %x(grep -c processor /proc/cpuinfo)
bind 'unix:///var/run/puma/my_app.sock'
stdout_redirect '/var/log/puma/puma.log', '/var/log/puma/puma.log', true
daemonize false

Anyone know what's going on?有人知道发生了什么事吗?

I faced the same error and adding these two lines to config/puma.rb fixed the problem:我遇到了同样的错误,将这两行添加到config/puma.rb解决了这个问题:

bind "unix:///var/run/puma/my_app.sock"
pidfile "/var/run/puma/my_app.sock"

After this step I faced another error to add ENV values, which is very straight forward.在这一步之后,我在添加 ENV 值时遇到了另一个错误,这是非常简单的。

Hope this helps.希望这可以帮助。

Sorry - Super late to the game.对不起 - 比赛太晚了。 But I found this entry when searching for the same error message and could solve it differently.但是我在搜索相同的错误消息时发现了这个条目,并且可以以不同的方式解决它。

I am trying to let a Rails 6.1.1 (currently latest and greatest) application run on AWS Beanstalk (Puma with Ruby 2.6), where I got the same error as described in this post: Nginx throwing above connection error / Puma seems not reactive (no entries in puma logs) / When SSHing into the machine it shows Puma is running ( initctl status puma -> shows process ID).我试图让 Rails 6.1.1(当前最新和最好的)应用程序在 AWS Beanstalk(Puma 和 Ruby 2.6)上运行,在那里我得到了与这篇文章中描述的相同的错误:Nginx throwing above connection error / Puma 似乎没有反应(在 puma 日志中没有条目)/当通过 SSH 连接到机器时,它显示 Puma 正在运行( initctl status puma -> 显示进程 ID)。

After some days of research (and trying all above answers without success) - I switched to Passenger - but still same result.经过几天的研究(并尝试了上述所有答案均未成功)-我切换到了Passenger-但结果仍然相同。 Researching the same problem for Passenger showed the proper solution:为乘客研究同样的问题显示了正确的解决方案:

Do not use the gem of the application server in production when you want to use the already provided application server of the AWS platform branch ("Puma with Ruby 2.6..." or "Passenger with Ruby 2.6...") (it somehow collides with the already installed version).当您想使用 AWS 平台分支已经提供的应用服务器(“Puma with Ruby 2.6...”或“Passenger with Ruby 2.6...”)时,不要在生产中使用应用服务器的 gem(它以某种方式与已安装的版本冲突)。 Therefore in your Gemfile:因此在您的 Gemfile 中:

gem 'puma', '~> 5.0', groups: [:development, :test]

respectively:分别:

gem "passenger", ">= 5.3.2", require: "phusion_passenger/rack_handler", groups: [:development, :test]

or use the AWS Beanstalk platform without already installed application server ("Ruby 2.6 running on...)或者在没有安装应用服务器的情况下使用 AWS Beanstalk 平台(“Ruby 2.6 running on...)

PS: Seems to be only a solution for higher Rails versions (my old Rails 5.1 app runs perfectly on the "Puma with Ruby 2.6" platform even with the gem "normally" installed. PS:似乎只是更高版本 Rails 的解决方案(我的旧 Rails 5.1 应用程序在“Puma with Ruby 2.6”平台上完美运行,即使“正常”安装了 gem。

In my case, this was happening because of a Gem loading error.就我而言,这是由于 Gem 加载错误而发生的。 I was able to reproduce the error by connecting to the server with eb ssh and running pumactl start .我能够通过使用eb ssh连接到服务器并运行pumactl start来重现错误。

For me, the conflict was with the nio4r gem, although for others it was the puma gem.对我来说,冲突是与nio4r gem,尽管对其他人来说是puma gem。 Setting it to the same version as the Elastic Beanstalk platform's gem fixed the error.将其设置为与 Elastic Beanstalk 平台的 gem 相同的版本修复了错误。

More details here: https://stackoverflow.com/a/67110462/1852005 and here: https://forums.aws.amazon.com/thread.jspa?messageID=957426更多细节在这里: https://stackoverflow.com/a/67110462/1852005和这里: https://forums.aws.amazon.com/thread.jspa?messageID=957426

config.force_ssl = true更改为false为我解决了这个问题。

This line in your logfile means, that you've configured your nginx incorrectly.日志文件中的这一行表示您错误地配置了 nginx。

upstream: "http://unix:///var/run/puma/my_app.sock:/get"

Actually nginx tries to use unix domain socket as HTTP url.实际上 nginx 尝试使用 unix 域套接字作为 HTTP url。

Can't say exactly what is wrong at your nginx config without it, but you should have something like this:如果没有它,无法准确说出您的 nginx 配置有什么问题,但您应该有这样的东西:

upstream backend {
  server backend1.example.com weight=5;
  server 127.0.0.1:8080       max_fails=3 fail_timeout=30s;
  server unix:/tmp/backend3;

  server backup1.example.com  backup; 
}

You should also check out this nginx doc http://nginx.org/en/docs/http/ngx_http_upstream_module.html您还应该查看此 nginx 文档http://nginx.org/en/docs/http/ngx_http_upstream_module.html

The AWS doc mandates that puma gem be installed as part of your application. AWS 文档要求将 puma gem 作为应用程序的一部分进行安装。

Add the following to your Gemfile as appropriate:根据需要将以下内容添加到您的 Gemfile 中:

group :production do
  gem 'puma'
end

The error unix:///var/run/puma/my_app.sock failed (2: No such file or directory) will also occur if Elastic Beanstalk cannot detect your Rails application root for whatever reason.如果 Elastic Beanstalk 由于任何原因无法检测到您的 Rails 应用程序根目录,也会出现错误unix:///var/run/puma/my_app.sock failed (2: No such file or directory) In my case, the issue was simply that I was zipping up my Rails application contents inside a subfolder.就我而言,问题只是我将 Rails 应用程序内容压缩到一个子文件夹中。 Instead of creating the zip in the same directory as app , bin , config , db , etc., the application content were being created inside a subfolder called <app_name> .不是在与appbinconfigdb等相同的目录中创建 zip,而是在名为<app_name>的子文件夹中创建应用程序内容。

The only hint was in the Elastic Beanstalk Events section where it had an Informational notification (not warning or error level) which said Gemfile not detected. Custom gems will not be included.唯一的提示是在 Elastic Beanstalk事件部分,它有一个信息通知(不是警告或错误级别),表示Gemfile not detected. Custom gems will not be included. Gemfile not detected. Custom gems will not be included.

Hope this helps others;希望这对其他人有帮助; took many deployments to figure out this simple problem.花了很多部署来解决这个简单的问题。

Given you are using Elastic Beanstalk, it starts up puma by running a command very similar to this one (as you can see by running ps ):鉴于您使用的是 Elastic Beanstalk,它通过运行一个与此非常相似的命令来启动puma (正如您通过运行ps所看到的):

sudo su -s /bin/bash -c "puma -C /opt/elasticbeanstalk/support/conf/pumaconf.rb webapp"

Try running that command in your eb ssh console.尝试在您的 eb ssh 控制台中运行该命令。 If you get an error like the one in this post :如果您收到类似这篇文章中的错误:

config/puma.rb:23:in `_load_from': undefined local variable or method `daemonize' for #<Puma::DSL:0x000055596fadb448> (NameError)

you have probably declared Puma 5.x in your Gemfile.您可能已经在 Gemfile 中声明了 Puma 5.x。 The solution would be to use puma < 5.0, as it removed the daemonize method.解决方案是使用 puma < 5.0,因为它删除了daemonize方法。 Note that the configuration Elastic Beanstalk uses is not config/puma.rb but /opt/elasticbeanstalk/support/conf/pumaconf.rb请注意,Elastic Beanstalk 使用的配置不是config/puma.rb而是/opt/elasticbeanstalk/support/conf/pumaconf.rb

Add a Procfile添加配置文件

with this content:内容如下:

web: puma -C /opt/elasticbeanstalk/config/private/pumaconf.rb web: puma -C /opt/elasticbeanstalk/config/private/pumaconf.rb

Reference:参考:

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ruby-platform-procfile.html https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ruby-platform-procfile.html

Check if you're missing any newly added ENV variables.检查您是否缺少任何新添加的 ENV 变量。

To add new Environment variables follow:要添加新的环境变量,请执行以下操作:

  • Elastic Beanstalk > Elastic Beanstalk >
  • Environments > Environments >
  • <Your Environment> > <Your Environment> >
  • Configuration > Configuration >
  • Software > Software >
  • EDIT

Under Environment properties Add the new ENV variablesEnvironment properties下添加新的 ENV 变量

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Elastic Beanstalk 部署的应用程序上的 Elastic IP - Elastic IP on application deployed using Elastic Beanstalk 带有 HTTPS 的 Elastic Beanstalk 应用程序上的 502 Bad Gateway - 502 Bad Gateway on Elastic Beanstalk application with HTTPS Flask 应用程序未在 AWS elastic beanstalk 上部署 - Flask Application is not getting deployed at AWS elastic beanstalk Elastic Beanstalk:自定义 Puma 配置 - Elastic Beanstalk: customize Puma configuration 增加 AWS Elastic Beanstalk 实例大小或增加负载均衡器是否有助于减少随机 502 Nginx 错误? - Will Increasing AWS Elastic Beanstalk Instance Size or Increasing Load Balancers Help Reduce Random 502 Nginx Errors? ExpressJS - Elastic Beanstalk 502 错误网关 - ExpressJS - Elastic Beanstalk 502 Bad Gateway 将 Dockerized NextJs 应用程序部署到 AWS Elastic Beanstalk 抛出 502 Bad Gateway - Deploying Dockerized NextJs Application to AWS Elastic Beanstalk Throws 502 Bad Gateway 当我将 SpringBoot 应用程序部署到 AWS Elastic Beanstalk 并且无法从本地 SpringBoot 连接到 RDS 时出现 502 bad gateway - 502 bad gateway when I deploy SpringBoot application to AWS Elastic Beanstalk and cannot connect to RDS from local SpringBoot 在 Elastic Beanstalk 环境中运行 Rails rake 任务 - Run Rails rake task in Elastic Beanstalk environment 无法在弹性 beantalk 中运行后台应用程序 - Failed to run a background application in elastic beanstalk
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM