504网关超时nginx/1.4.6(Ubuntu)

Has*_*mad 8 ruby-on-rails nginx unicorn digital-ocean

跟踪rails一个应用程序点击部署.数据库做得很好,即使我检查rails控制台一切正常.

Ruby版本是2.3.0,rails版本是5.0.1

但是当我点击IP地址时,它会给出错误超时

在检查独角兽日志我得到

/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
Run Code Online (Sandbox Code Playgroud)

和nginx错误是

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"
Run Code Online (Sandbox Code Playgroud)

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"
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

nginx文件在这里:

upstream app_server {
server unix:/var/run/unicorn.sock fail_timeout=0;
Run Code Online (Sandbox Code Playgroud)

}

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;
}
}
Run Code Online (Sandbox Code Playgroud)

Jer*_*mie 2

这听起来像是独角兽重启问题。你说你不使用 capistrano 。您如何部署您的应用程序?

编辑

Unicorn 使用多进程架构更好地利用可用资源。当它启动时,工作线程会加载 ruby​​ 环境,然后生成处理请求的工作线程。主人从不处理请求,总是由工人处理。

当一个worker花费太长时间时,master可以杀死它并重新启动一个新的worker。

您似乎使用了 4 个工人。我不知道你的DO上的水滴大小,但是master似乎不能再启动worker了。您能告诉我您的 Droplet 的大小(CPU 和内存)吗?

我将安装unicorn-worker-killer gem并再次测试该应用程序。这应该比独角兽大师更有效地重新启动你的工作人员。

编辑2:如果这不起作用,您可以尝试在 nginx conf 文件中用此替换上游行:

upstream app_server { server 127.0.0.1:8080  fail_timeout=0; }
Run Code Online (Sandbox Code Playgroud)

这在你的独角兽conf文件中:

listen "127.0.0.1:8080
Run Code Online (Sandbox Code Playgroud)

然后重启 nginx,然后重启 unicorn。

编辑3:

我想我明白了

您可以像这样更改您的文件吗:

独角兽.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"
Run Code Online (Sandbox Code Playgroud)

Nginx 文件

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

重新启动unicorn(确保替换<>之间的值)

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

然后重启nginx

sudo service nginx restart
Run Code Online (Sandbox Code Playgroud)

看看它是否有效。