Rails Deployment with Nginx and Unicorn giving 504

2019-08-10 23:53发布

问题:

My Nginx server is not running properly on VPS i did all the settings correctly, According to rails cast VPS Deployment

still I am getting the error 504 Gateway timeout error

Nginx Log

    request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "roomies.co.in"
2014/07/26 11:33:58 [error] 6935#0: *10 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 223.239.147.104, server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "roomies.co.in"
2014/07/26 11:45:56 [error] 6935#0: *20 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 223.239.147.104, server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "roomies.co.in"
2014/07/26 11:48:46 [error] 6935#0: *24 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 223.239.147.104, server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "roomies.co.in"
2014/07/26 11:50:03 [error] 6935#0: *24 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 223.239.147.104, server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "roomies.co.in"
2014/07/26 11:51:06 [error] 6935#0: *24 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 223.239.147.104, server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "roomies.co.in"

Above Nginx log

Below is Unicorn Log

/home/rails/releases/20140726114415/config/boot.rb:4:in `<top (required)>'
/usr/loc
config.ru:1:in `new'
config.ru:1:in `<main>'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn.rb:48:in `eval'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn.rb:48:in `block in builder'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:750:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:750:in `build_app!'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:623:in `init_worker_process'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:643:in `worker_loop'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:527:in `spawn_missing_workers'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:538:in `maintain_worker_count'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/unicorn-4.7.0/lib/unicorn/http_server.rb:303:in `join'
/usr/local/rvm/gems/ruby-2`enter code here`.0.0-p353/gems/unicorn-4.7.0/bin/unicorn:126:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/unicorn:23:in `load'
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/unicorn:23:in `<main>'
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.0.0-p353/bin/ruby_executable_hooks:15:in `<main>'
E, [2014-07-26T11:59:07.161500 #24415] ERROR -- : reaped #<Process::Status: pid 29887 exit 1> worker=0
I, [2014-07-26T11:59:07.161888 #24415]  INFO -- : worker=0 spawning...
E, [2014-07-26T11:59:07.162737 #24415] ERROR -- : reaped #<Process::Status: pid 29885 exit 1> worker=1
I, [2014-07-26T11:59:07.163084 #24415]  INFO -- : worker=1 spawning...
I, [2014-07-26T11:59:07.164414 #29891]  INFO -- : worker=0 spawned pid=29891
I, [2014-07-26T11:59:07.167923 #29893]  INFO -- : worker=1 spawned pid=29893
I, [2014-07-26T11:59:07.186676 #29891]  INFO -- : Refreshing Gem list
I, [2014-07-26T11:59:07.188107 #29893]  INFO -- : Refreshing Gem list

Unicorn.rb file

listen "127.0.0.1:8080"
worker_processes 2
user "rails"
worker_processes 2
timeout 30
working_directory "/home/rails/current"
pid "/home/unicorn/pids/unicorn.pid"
stderr_path "/home/unicorn/log/unicorn.log"
stdout_path "/home/unicorn/log/unicorn.log"

Please help if anybody is familiar with these kind of problem

Error I am getting after running bundle exec unicorn_rails

E, [2014-07-26T13:27:52.445453 #16467] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)
E, [2014-07-26T13:27:52.451129 #16467] ERROR -- : retrying in 0.5 seconds (4 tries left)
E, [2014-07-26T13:27:52.951675 #16467] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)
E, [2014-07-26T13:27:52.951799 #16467] ERROR -- : retrying in 0.5 seconds (3 tries left)
E, [2014-07-26T13:27:53.452214 #16467] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)
E, [2014-07-26T13:27:53.452339 #16467] ERROR -- : retrying in 0.5 seconds (2 tries left)
E, [2014-07-26T13:27:53.952728 #16467] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)
E, [2014-07-26T13:27:53.952846 #16467] ERROR -- : retrying in 0.5 seconds (1 tries left)
E, [2014-07-26T13:27:54.453490 #16467] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)
E, [2014-07-26T13:27:54.453733 #16467] ERROR -- : retrying in 0.5 seconds (0 tries left)
E, [2014-07-26T13:27:54.954208 #16467] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)
/usr/local/rvm/gems/ruby-1.9.3-p484/gems/unicorn-4.8.3/lib/unicorn/socket_helper.rb:185:in `bind': Address already in use - bind(2) (Errno::EADDRINUSE)
    from /usr/local/rvm/gems/ruby-1.9.3-p484/gems/unicorn-4.8.3/lib/unicorn/socket_helper.rb:185:in `new_tcp_server'
    from /usr/local/rvm/gems/ruby-1.9.3-p484/gems/unicorn-4.8.3/lib/unicorn/socket_helper.rb:165:in `bind_listen'
    from /usr/local/rvm/gems/ruby-1.9.3-p484/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:242:in `listen'
    from /usr/local/rvm/gems/ruby-1.9.3-p484/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:809:in `block in bind_new_listeners!'
    from /usr/local/rvm/gems/ruby-1.9.3-p484/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:809:in `each'
    from /usr/local/rvm/gems/ruby-1.9.3-p484/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:809:in `bind_new_listeners!'
    from /usr/local/rvm/gems/ruby-1.9.3-p484/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:138:in `start'
    from /usr/local/rvm/gems/ruby-1.9.3-p484/gems/unicorn-4.8.3/bin/unicorn_rails:209:in `<top (required)>'
    from /usr/local/rvm/gems/ruby-1.9.3-p484/bin/unicorn_rails:23:in `load'
    from /usr/local/rvm/gems/ruby-1.9.3-p484/bin/unicorn_rails:23:in `<main>'
    from /usr/local/rvm/gems/ruby-1.9.3-p484/bin/ruby_executable_hooks:15:in `eval'
    from /usr/local/rvm/gems/ruby-1.9.3-p484/bin/r

回答1:

Seems like your Unicorn workers are being killed by system or some outside monitoring tool like Monit.

My thoughts are:

1 - Either your VPS resources (memory) are low for spawning 2 unicorn workers.

2 - Or a monitoring tool kills your unicorn processes because of the resource consumption.

You can check these similar problems.

Unicorn error: "ERROR -- : reaped #<Process::Status: pid 12345 SIGABRT (signal 6)> worker=2"

ERROR -- : reaped #<Process::Status: pid 4335 exit 1> worker=0