502 bad gateway nginx + puma + rails 3.2 on Elastic Beanstalk - ruby-on-rails

The deployment was successful and everything is green. But when we try to access the application URL, it gives 502 Bad Gateway error.
Checking for puma process with ps -aux | grep puma doesn't return any process attached to puma server but pgrep returns following.
$pgrep -fl puma
18009 su -s /bin/bash -c bundle exec puma -C /opt/elasticbeanstalk/support/conf/pumaconf.rb webapp
18031 ruby /opt/rubies/ruby-2.0.0-p598/bin/puma -C /opt/elasticbeanstalk/support/conf/pumaconf.rb

I have tried all possible combinations, as shown in every other forum/blog OR support sites of nginx/puma. Following is the status.
Default configuration - Where we have UNIX:// sock file used in the UPSTREAM option of nginx.conf and pumaconf.rb - This gives 502 bad gatway. When checked, puma is not running and it is rebooting every 3rd minute.
As we have used it in DigitalOcean - Change the above UPSTREAM conf URL to tcp://127.0.0.1:3000 in pumaconf.rb and 127.0.0.1:3000 in conf.d/webapp.conf file. - This is also not working, puma is not able to run properly same as above.
My question is,
Why there is no control over running puma with diff. configurations? And why we have to always use the UI, which is not able to run the services properly as per other standard configuration options?
There is no configuration options from UI, to change/verify from the UI. So we have to do it from SSH. But, we have no control over rebooting PUMA from console.
Whenever puma is not running, we are not able to see any logs of what error it is facing. This is really not helpful at all.
Puma is not able to run even with default configurations, so it nginx is not able to talk to Puma and so the EC2 does not really make sense!
Please let us know, how we can resolve this issue, if you have any idea on this.
See this - https://forums.aws.amazon.com/thread.jspa?messageID=608148&#608148
Still no answers on this one, this is like our hands are cuffed and not able to change any configurations!
UPDATE
AWS is somehow stopping and starting PUMA, because i can see the process IDs changing when checking with ps -ef|grep puma. So, I started the puma to work on another port and tried to check if it runs or not.
Started on another port, and then from another console accessing the URL using wget http://127.0.0.1:3000. It prints the following log.
current]$ bundle exec puma -b tcp://127.0.0.1:3001
Puma 2.0.1 starting...
* Min threads: 0, max threads: 16
* Environment: production
* Listening on tcp://127.0.0.1:3001
Rails Error: Unable to access log file. Please ensure that /var/app/current/log/production.log exists and is chmod 0666. The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
Use Ctrl-C to stop
2015-03-16 13:19:35 +0000: HTTP parse error, malformed request (): #<Puma::HttpParserError: Invalid HTTP format, parsing fails.>
2015-03-16 13:19:35 +0000: ENV: {"rack.version"=>[1, 1], "rack.errors"=>#<IO:<STDERR>>, "rack.multithread"=>true, "rack.multiprocess"=>false, "rack.run_once"=>false, "SCRIPT_NAME"=>"", "CONTENT_TYPE"=>"text/plain", "QUERY_STRING"=>"", "SERVER_PROTOCOL"=>"HTTP/1.1", "SERVER_SOFTWARE"=>"2.0.1", "GATEWAY_INTERFACE"=>"CGI/1.2"}
So, is it compulsory to use SSL? Because I think by default, it is not enabled.

I had this issue after uploading my rails app, I found this line (auto generated) on secrets.yml (config > secrets.yml) :secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
so you have to add it as an environment variable to your environment.
In the environment dashboard go to Configuration > Software > Environment properties and add a new variable with name SECRET_KEY_BASE.
You can set any value but make sure it is a safe key.
This resolved the issue for me, I hope it helps.

I could not fix this problem. Also we supposed to use EC2 free instance only instead of BeanStalk.
We have now moved to Free EC2 instance with RDS and deployed the rails application using Capistrano with Nginx + Unicorn. Though it was not easy[1][2] but finally we got it working.

Related

How to change WEBrick :AccessLog option when running RedMine?

I'm running RedMine via WEBrick using the following command line (simplified):
bundle exec rails server webrick -e production -p 3012 -P '/var/lib/redmine/redmine.pid'
I don't like how WEBrick outputs the peer address at the beginning of its access log lines (because I'm running it behind nginx and the peer address is always 127.0.0.1), so I want to change the access log format.
I know that I need to tune the :AccessLog config option for WEBrick, but I don't know how to get my hands on it. WEBrick is run by the rails server command, via the rack abstraction, and I don't see an obvious way to pass the necessary configuration to WEBrick.
So, is there any way to do it? Some command line switch? -c is the only switch that accepts some kind of configuration file, but it references "rackup", and I have no idea how to work with it.
Maybe it can be done by changing configuration files? I tried to modify additional_environment.rb by adding config[:AccessLog] = [ [ $stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT ] ], but it had no effect (although the file was executed), so I assume this file's config is not what is passed to WEBrick.
I'm pretty sure there is some way to configure this option without creating a new Rails application and invoking WEBrick manually, and hopefully even without changing RedMine files.

Why will the Puma server not pickup changes to the code?

I am running a local puma server for my rails app.
This is in my local_env.yml file
STAGING_URL: 'http://statrubytwotwo.test'
I can hit the URl when the server is running and all works fine. If I shut the server down with Cntrl-C and then hit the server it is still running.
To get the server to shutdown I have to do
pkill -9 -f 'rb-fsevent|rails|spring|puma'
The main problem is I change code. Do a git add * --all and a git commit -m "something here" and then start the server via bundle exec puma
The server starts up again, but the code changes are NOT seen. The puma server is running the old code?
UPDATE ====
I killed all with the following:
puma-dev -stop
pkill -9 -f 'rb-fsevent|rails|spring|puma'
pkill -USR1 puma-dev
I did a rails s and I see this
Puma starting in single mode...
* Version 3.11.4 (ruby 2.3.1-p112), codename: Love Song
* Min threads: 0, max threads: 16
* Environment: staging
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
I hit the https://stattwotwo.test domain and it loads.
I hit http://localhost:3000 and NOT FOUND
I hit the https://stattwotwo.test domain and it loads
No matter what I do nothing ever loads on http://localhost:3000
I believe I recall the puma-dev will start anytime the domain is hit even if it is down (forman or pow)? Either way when it restarts it is not using new code.
Adding as a response because I can't fit it all in a comment:
You need to find out which tool you're using. My guess would be puma-dev, and you installed it as a background process via here
See if you can confirm.
Normally you'd get a conflict if there were 2 servers running, but that runs on a port other than the puma/rails default of 3000, which is what default bundle exec puma runs (I think?)
If that's the case, you've been starting a new server on a different port, while the old server (running your old cached code from when it started) is still running. That's why you don't see the changes.
You can confirm a couple things:
Don't do bundle exec puma, do rails s and check the log to the screen and see if :3000 is there
Kill your puma like you've been, but visit your URL and see if it's still running
Start rails s again, and visit http://localhost:3000 and see if your code changes when you reload pages as you expect.
Do puma-dev -stop, then rails s, and visit your .test url and see if it breaks (that means puma-dev was running in the background)

puma production server in background: omniauth logging to stdout failed cause error

I am using rails + puma as my production server. I used httpd to set up virtual machine and forward request to localhost:3000. And the command I used to start up my puma server is(inside the app folder already)
puma -e production -p 3000 &
Everything with my web app is fine when I was still logged in the server with ssh. only that some logs actually appears on the screen(I did not pay attention to it). However, when I exited the ssh session. I cannot use omniauth(a gem). I looked inside the server log:
Errno::EIO (Input/output error # io_write - <STDERR>):
I looked for this online--it is a IO connection issue and the cause in my case should be that the parent process that spawned puma is done but puma tried to log something to it.
So I used redirection and hope that console would be fine:
puma -e production -p 3000 > log/console.log &
But the problem still exists.
So far I think it is only related to omniauth: it always logs to stdout even if I did this "https://github.com/intridea/omniauth/issues/583".
So for now I guess I can either try to change logger of omniauth or try to "give a stdout" to omniauth even if it is running in background
Thanks a lot:)
turns out that the logging is for warning and logged to stderr instead of stdout. so I solved this issue by the following command
puma -e production -p 3000 1> log/console.log 2>console_err.log &

Running Rails on Unicorn via Pow, is it possible to know what port Unicorn is running on?

In my Ruby on Rails dev environment, I am starting Rails and Unicorn via Foreman in the typical way:
(Procfile:)
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
I am also running Pow. But not as a webserver. I'm just using Pow to direct http requests from mydomain.dev (port 80) to the port Unicorn is listening on.
You can do this by creating a pow file (mydomain.dev) containing the port number Unicorn is running on.
Given this setup, is it possible in my rails code to know what the port I started Unicorn on?
I'm only wanting to know this in my dev environment, it's not a production issue.
In my Rails code, I've tried a few different things, but none of them work:
Unicorn::Configurator::RACKUP[:port] - returned nothing
Rails::Server.new.options[:Port] - doesn't exist in Rails 4
Rack::Server.new.options[:Port] - returns default rack port (9292) not the one configured for this rack instance.
Is there a way to get the current rack instance from rails?
request.port - returns 80, which is the port that Pow is listening on. Pow is routing http traffic to Unicorn, which is on a different port.
None of these give me the port that Unicorn is running on.
Any ideas?
EDIT If you're wondering why I want to know this, it's because in my dev environment, I'm trying to dynamically create configuration files for Pow, so I can route http requests to Unicorn on the correct port.
If you're responding to a request in a controller or view, use the request object - this should work, although you say it does not:
request.port
If you're in an initialiser :
Rails::Server.new.options[:Port]
How to find the local port a rails instance is running on?
You should just be able to access it via ENV['PORT'], given it's value has been set to the $PORT environment variable.
I've sort of found a way to do this.
Create separate config files for Unicorn. unicorn.development.rb and unicorn.test.rb
Install the dotenv-rails gem
Inside my unicorn config files, do something like this:
# unicorn.development.rb:
require "dotenv"
Dotenv.load(
"./.env.local",
"./.env.development",
"./.env"
)
if ENV['UNICORN_PORT'].nil?
throw 'UNICORN_PORT not set in environment!'
end
worker_processes 3
timeout 30
preload_app true
listen ENV['UNICORN_PORT'], backlog: 64
... rest of unicorn config...
# unicorn.testing.rb:
require "dotenv"
Dotenv.load(
"./.env.local",
"./.env.testing",
"./.env"
)
if ENV['UNICORN_PORT'].nil?
throw 'UNICORN_PORT not set in environment!'
end
worker_processes 3
timeout 30
preload_app true
listen ENV['UNICORN_PORT'], backlog: 64
... rest of unicorn config...
In my .env.development and .env.testing environment files, set the UNICORN_PORT environment variable
Make sure you use the correct Unicorn config file to start the app. This can be done by using separate Procfiles for dev and testing.
# Procfile.dev
web: bundle exec unicorn -c ./config/unicorn.development.rb
# Procfile.testing
web: bundle exec unicorn -c ./config/unicorn.testing.rb
This appears to mostly work, but is not without it's issues...
Probably a bad idea, but whatever:
uport = `netstat -n --listening --tcp -p | grep unicorn | sed 's/.*:\([0-9]*\) .*/\1/'`

Can't Get Mongrel_Service Gem to Start Mongrel as a Windows Service

I’m more or less a newb to Ruby on Rails, but I’ve been tasked with debuging a Rails app that guy that’s no longer around wrote. The app is running on a manchine using:
• Windows XP Professional
• Apache 2.2
• Rails 2.3.8
• mongrel (1.1.5 x86-mingw32)
• mongrel_service (0.3.4 i386-mswin32)
I copied the app from the server and did some debugging on it on my personal machine. I just setup a Git repository on my personal machine and cloned it back over to the server. Everything seems to be working great except mongrel_service doesn’t work anymore. Each time I try to start the service from Windows “Services” tool I get this error:
The MYAPP_Mongrel_As_Service service on Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service
I tried removing the service with:
C:\MyApp>mongrel_rails service::remove --name MYAPP_Mongrel_As_Service
Stopping MYAPP_Mongrel_As_Service if running...
MYAPP_Mongrel_As_Service service removed
and reinstalling it with:
C:\MyApp>mongrel_rails service::install --name MYAPP_Mongrel_As_Service -c "C:\MyApp" --port 3001 --env
ironment production --address localhost --log "log\mongrel_as_service.log" --pid "tmp\pids\mongrel_a
s_service.pid"
MYAPP_Mongrel_As_Service service created.
But no matter how many times I try, or what options I use, I can’t get the service to run. What’s weird is that I can get mongrel to startup by itself just fine.
C:\MyApp>mongrel_rails start -c c:\MyApp --port 3001 --environment production --address localhost --
g "c:\MyApp\log\mongrel_as_service.log" --pid "C:\MyApp\tmp\pids\mongrel_as_service.pid"
** Starting Mongrel listening at localhost:3001
** Starting Rails with production environment...
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready. INT => stop (no restart).
** Mongrel 1.1.5 available at localhost:3001
** Use CTRL-C to stop.
It just won’t work when I try to start it as a service. I've done a lot of googling on the subject, but I can't find anything to fix the problem. It's odd that it was working before but now it doesn't. There must be something wrong with my service::install line because I can't get the original unedited Rails app to work with mongrel_as_service either.
I figured it out. It turns out that the log file for mongrel_service didn't exist on my file. To fix the problem, I just made a blank text file and renamed it to the name of my log file. It worked like a charm. It's odd that mongrel_service doesn't make it's own log file if it can't detected it, but oh well.

Resources