Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

504 Gateway Time-out nginx/1.4.6 (Ubuntu)

Follow rails one app click deployment. Database done well, even I check rails console everything working fine.

Ruby version is 2.3.0 and rails version is 5.0.1

But when I hit IP address it gives an error time out

on check unicorn logs I get

/usr/local/rvm/gems/ruby-2.2.1/gems/unicorn-5.0.1/bin/unicorn:126:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/bin/unicorn:23:in `load'
/usr/local/rvm/gems/ruby-2.2.1/bin/unicorn:23:in `<main>'
/usr/local/rvm/gems/ruby-2.2.1@global/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.2.1@global/bin/ruby_executable_hooks:15:in `<main>'
E, [2017-02-26T15:47:18.969274 #9861] ERROR -- : reaped #<Process::Status: pid 11928 exit 1> worker=2
I, [2017-02-26T15:47:18.969471 #9861]  INFO -- : worker=2 spawning...
I, [2017-02-26T15:47:18.974112 #11942]  INFO -- : worker=2 spawned pid=11942
I, [2017-02-26T15:47:18.978540 #11936]  INFO -- : Refreshing Gem list
I, [2017-02-26T15:47:18.986558 #11938]  INFO -- : Refreshing Gem list

and nginx error is

017/02/26 15:34:17 [error] 18564#0: *31 connect() to unix:/var/run/unicorn.sock failed (111: Connection refused) while connecting to upstream, client: 121.52.156.57, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/var/run/unicorn.sock:/", host: "188.166.157.124"
2017/02/26 15:35:42 [error] 32360#0: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 119.155.34.115, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/var/run/unicorn.sock/", host: "188.166.157.124"
2017/02/26 15:42:38 [error] 6296#0: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 119.152.140.90, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/var/run/unicorn.sock/", host: "188.166.157.124"

unicorn.conf

listen "unix:/var/run/unicorn.sock"
worker_processes 4
user "rails"
working_directory "/home/rails/company_startup"
pid "/var/run/unicorn.pid"
stderr_path "/var/log/unicorn/unicorn.log"
stdout_path "/var/log/unicorn/unicorn.log"

ps aux | grep unicor

rails     4751 18.0  4.2 172880 21516 ?        R    14:59   0:00 unicorn worker[2] -D -c /etc/unicorn.conf -E production                                                                                              
rails     4757  0.0  4.1 172404 20972 ?        Rl   14:59   0:00 unicorn worker[3] -D -c /etc/unicorn.conf -E production                                                                                              
rails     4760  0.0  2.9 159860 14568 ?        Rl   14:59   0:00 unicorn worker[1] -D -c /etc/unicorn.conf -E production                                                                                              
root      4764  0.0  0.1  11712   620 pts/0    S+   14:59   0:00 grep --color=auto unicorn
root     20463  0.4  2.6 146740 13176 ?        Sl   04:32   2:48 unicorn master -D -c /etc/unicorn.conf -E production

nginx file here:

upstream app_server {
server unix:/var/run/unicorn.sock fail_timeout=0;

}

server {
listen   80;
root /home/rails/company_startup/public;
server_name _;
index index.htm index.html;
client_max_body_size 1M;
location / {
        try_files $uri/index.html $uri.html $uri @app;
}

location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
                try_files $uri @app;
        }

 location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
}
}
like image 209
Haseeb Ahmad Avatar asked Feb 26 '17 20:02

Haseeb Ahmad


1 Answers

This sounds like an unicorn restart problem. You said you don't use capistrano. How do you deploy your application?

EDIT

Unicorn makes better use of resources available to you using multi-process architecture. When it starts, the worker loads the ruby environment and then spawns workers that handles the requests. The master never handles the request, always the workers.

When a worker takes too long, the master can kill it and starts a new worker again.

You seem to use 4 workers. I don't know the size of your droplet on DO, but it seems that the master can't start anymore workers. Could you tell me the size of your droplet (CPU & memory)?

I would install the unicorn-worker-killer gem and test the application again. This should restart your workers in a more effective way than the unicorn master.

EDIT 2: If this doesn't work, could you try replacing your upstream line with this in your nginx conf file:

upstream app_server { server 127.0.0.1:8080  fail_timeout=0; }

And this in your unicorn conf file:

listen "127.0.0.1:8080

And restart nginx then unicorn.

EDIT 3:

I think I got it

Could you please change your files like this :

unicorn.conf

listen "/var/run/unicorn.sock"
worker_processes 4
user "rails"
working_directory "/home/rails/company_startup"
pid "/var/run/unicorn.pid"
stderr_path "/var/log/unicorn/unicorn.log"
stdout_path "/var/log/unicorn/unicorn.log"

Nginx file

upstream app_server {
  server unix:/var/run/unicorn.sock fail_timeout=0;
}

server {
  listen   80;
  root /home/rails/company_startup/public;
  server_name <PLEASE PUT YOUR SERVER NAME>;
  index index.htm index.html;
  client_max_body_size 1M;

  location / {
    try_files $uri/index.html $uri.html $uri @app;
  }

  location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
    try_files $uri @app;
  }

  location @app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
  }
}

Restart unicorn (Make sure to replace the values between <>)

kill -s QUIT $(< /var/run/unicorn.pid)
bundle exec unicorn -c <PATH TO unicorn.conf FILE> -E <RAILS ENVIRONMENT> -D

Then restart nginx

sudo service nginx restart

and see if it works.

like image 156
Jeremie Avatar answered Nov 13 '22 03:11

Jeremie