25

I just deployed a Rails app to Elastic Beanstalk, and every request is giving me a 502 error.

Here's the contents of /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 -

[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

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?

chintanparikh
  • 1,632
  • 6
  • 22
  • 36

10 Answers10

21

I faced the same error and adding these two lines to config/puma.rb fixed the problem:

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.

Hope this helps.

aks
  • 8,796
  • 11
  • 50
  • 78
  • I get `No such device or address @ rb_sysopen - /var/run/puma/my_app.sock (Errno::ENXIO)` with this after starting puma on EB now... – teddybear Aug 19 '23 at 14:04
6

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).

After some days of research (and trying all above answers without success) - I switched to Passenger - but still same result. 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). Therefore in your 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...)

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.

Michael Fehr
  • 286
  • 1
  • 7
  • 15
  • 3
    This solved the problem for me. For last 5 hours, I was trying to deploy my Rails 6.1.3.2 application to the Beanstalk platform Ruby 2.6.7 and Puma and this was the only thing which helped. Thanks a ton Michael! – Tom Hert May 10 '21 at 15:59
  • 1
    Worked from me using the platform 'Ruby 2.7.3 and Puma' also. Thanks! – Bob gilong May 27 '21 at 17:35
3

In my case, this was happening because of a Gem loading error. I was able to reproduce the error by connecting to the server with eb ssh and running pumactl start.

For me, the conflict was with the nio4r gem, although for others it was the puma gem. Setting it to the same version as the Elastic Beanstalk platform's gem fixed the error.

More details here: https://stackoverflow.com/a/67110462/1852005 and here: https://forums.aws.amazon.com/thread.jspa?messageID=957426

stwr667
  • 1,566
  • 1
  • 16
  • 31
  • 1
    I had this same issue as well, setting nio4r in gemfile to `gem 'nio4r', '= 2.5.7'` fixed this issue. – Smitty Sep 13 '21 at 18:58
1

Changing config.force_ssl = true to false fixed the issue for me.

Ibukun Muyide
  • 1,294
  • 1
  • 15
  • 23
0

This line in your logfile means, that you've configured your nginx incorrectly.

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

Actually nginx tries to use unix domain socket as HTTP url.

Can't say exactly what is wrong at your nginx config without it, but you should have something like this:

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

Stanislav Mekhonoshin
  • 4,276
  • 2
  • 20
  • 25
0

The AWS doc mandates that puma gem be installed as part of your application.

Add the following to your Gemfile as appropriate:

group :production do
  gem 'puma'
end
David
  • 1,940
  • 3
  • 17
  • 30
dexter
  • 13,365
  • 5
  • 39
  • 56
  • I am still getting the [error](http://stackoverflow.com/questions/37472304/rails-elastic-beanstalk-connection-time-out-puma-server). Seems like request first go to nginx before puma in the time stamps. Dont know how to fix this – Shalafister's May 27 '16 at 08:30
0

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. In my case, the issue was simply that I was zipping up my Rails application contents inside a subfolder. 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>.

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.

Hope this helps others; took many deployments to figure out this simple problem.

Kurt W
  • 321
  • 2
  • 15
0

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):

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

Try running that command in your eb ssh console. 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. The solution would be to use puma < 5.0, as it removed the daemonize method. Note that the configuration Elastic Beanstalk uses is not config/puma.rb but /opt/elasticbeanstalk/support/conf/pumaconf.rb

Tomás Ferrer
  • 55
  • 1
  • 6
0

Add a Procfile

with this content:

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

Reference:

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

Manuel Reis
  • 117
  • 1
  • 7
0

Check if you're missing any newly added ENV variables.

To add new Environment variables follow:

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

Under Environment properties Add the new ENV variables

enter image description here

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88