Puma停止使用Nginx在EC2实例上运行Rails应用程序(使用Capistrano/Capistrano Puma) [英] Puma Stops Running for Rails App on EC2 Instance with Nginx (using Capistrano/Capistrano Puma)

查看:112
本文介绍了Puma停止使用Nginx在EC2实例上运行Rails应用程序(使用Capistrano/Capistrano Puma)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的首要问题是,如何让Puma停止失败.但这实际上是由许多较小的问题组成的.我将对它们中的每一个进行编号并加粗,以使这个问题可以回答.

My top-level question is, how can I get Puma to stop failing. But that is really made up of lots of smaller questions. I will number and bold each of them, to try to make this question answerable.

我正在t2.nano的EC2实例上托管Rails应用程序.不可否认,这是一个很小的盒子,但是我不希望自己的网站收到任何流量.我使用Capistrano和Capistrano Puma在Nginx和Puma上成功配置了所有东西.一切都很棒,直到有一天我访问我的网站并看到了Nginx 504消息.

I am hosting a Rails application on an EC2 instance that is a t2.nano. This is admittedly, a very small box--but I don't expect my website to receive any traffic. I configured everything successfully with Nginx and Puma using Capistrano and Capistrano Puma. Everything was great, until one day I went to my website and saw the Nginx 504 message.

我打开了Nginx错误日志,发现它无法连接到Puma:

I opened the Nginx error log and saw that it could not connect to Puma:

connect() to unix:/home/deploy/myapp/shared/tmp/sockets/puma.sock failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: localhost, request: "GET / HTTP/1.0", upstream: "http://unix:/home/deploy/myapp/shared/tmp/sockets/puma.sock:/500.html", host: "myapp.com"

调试此命令后,我了解到Puma已停止运行.这就是Nginx无法连接到它的原因.我认为这里有两个问题:第一,彪马不应该停止跑步.服务器很小,但是没有流量.第二,是当Puma确实失败时,它应该正常重启.但是,我现在只关注第一个问题.因为如果Puma不断重启,那么有时它会以苛刻的方式终止进程似乎是合理的.

Debugging this, I learned that Puma had stopped running. That is why Nginx could not connect to it. I think there are two problems here: the first, is that Puma should not stop running. The server is tiny, but there is no traffic. the second, is that when Puma does fail, it should restart gracefully. However, I am just focusing on the first issue for now. Because if Puma is constantly restarting, it seems reasonable that sometimes it kills the process in a harsh way.

要调试此功能,我打开了htop.果然,机器正在运行,没有任何可用内存.这很有意义-我在一台小型计算机上运行数据库,Rails应用程序,Web服务器和内存缓存.它不断耗尽内存并杀死Puma.

To debug this, I opened htop. Sure enough, the machine was running without any memory to spare. This makes sense--I am running a database, rails app, webserver, and memcache on one tiny machine. It keeps running out of memory and killing Puma.

我调查了与Capistrano一起设置的Puma配置.在config/deploy.rb文件中,我有以下几行内容:

I looked into the Puma configuration I had set up with Capistrano. In config/deploy.rb I had these lines--

set :puma_threads, [0, 8]
set :puma_workers, 0

我阅读了有关puma_workers和puma_threads的全部内容.我还了解到Nginx有自己的工人.彪马工艺非常昂贵. Puma之所以如此酷,是因为它具有适当的多线程功能,因此独立进程非常棒.听起来每个工作人员都有自己的线程集-因此,如果有4个工作人员具有8个线程,则将有32个进程.但就我而言,我只想使用很少的内存. 2个过程对我来说听起来很不错. 1.我对工作人员和线程的理解正确吗?

I read all about puma_workers and puma_threads. I also learned that Nginx has its own workers. Puma processes are very expensive. What makes Puma cool is that it is properly muli-threaded--so the independent processes are awesome. It sounds like each worker has its own set of threads--so if there are 4 workers with 8 threads, there will be 32 processes. But in my case, I want to use very little memory. 2 processes sound good to me. 1. Is my understanding of workers and threads correct?

我更新了config/deploy.rb文件并部署了0个puma_workers和min = 0,max = 2个线程.

I updated my config/deploy.rb file and deployed, with 0 puma_workers and min=0, max=2 threads.

看来Nginx的配置位于此处:/etc/nginx/nginx.conf. Puma的配置位于此处:/home/deploy/myapp/shared/puma.rb.我本来希望config/deploy.rb中的更新使Capistano编辑配置文件.运气不好-我的最小,最大线程数仍设置为0.8. 2.使用Capistano时尝试通过config/deploy.rb更新这些值是否正确?

It appears the configuration for Nginx lives here: /etc/nginx/nginx.conf. And the configuration for Puma lives here: /home/deploy/myapp/shared/puma.rb. I would have expected my updates in config/deploy.rb to have had Capistano edit the config files. No luck--my min, max threads was still set to 0,8. 2. Is it correct to try and update these values through config/deploy.rb when using Capistano?

而且-我打开nginx.conf并看到worker_processes 4;. 3.当我安装Nginx时此设置为4还是Capistano设置了此默认设置?

Also--I opened the nginx.conf and saw worker_processes 4;. 3. Was this set to four when I installed Nginx or did Capistano set this default?

我打开了htop,并确定我有很多Puma流程.因此,我手动编辑了配置文件,然后重新启动了Puma和Nginx.

I opened htop and sure enough I had lots of Puma processes. Therefore, I edited my config files manually and restarted Puma and Nginx.

我将Nginx工作人员的数量从4个更改为1个.我现在只有1名Nginx工人.但是,Nginx的工作人员从来都不是很昂贵的(与Puma线程相比).因此,我认为这无关紧要.

I changed the number of Nginx workers from 4 to 1. Looking in htop, this worked. I now only had 1 Nginx worker. However, the Nginx workers were never very expensive (compared to the Puma threads). So I don't think this matters much.

但是,仍然有2个以上的Puma线程-共有6个.在百灵鸟中,我将最小线程数从0更改为1-认为0不可能是数字,所以也许它设置了默认值.这将Puma进程的数量增加到9.出于相同的原因,我还尝试将puma_workers的数量更改为1,并且进程的数量也增加了. 4.拥有0个线程和/或worker是什么意思?

However, there were still more than 2 Puma threads--there were 6. On a lark, I changed the minimum number of threads from 0 to 1--thinking 0 isn't a possible number so maybe it's setting a default. This increased the number of Puma processes to 9. I also tried changing the number of puma_workers to 1, for the same reason, and the number of processes increased. 4. What does it mean to have 0 threads and/or workers?

然后我尝试手动杀死一个puma进程(sudo kill xxxxx),然后所有的Puma进程死亡.

I then tried to kill one of the puma processes manually (sudo kill xxxxx), and then all of the Puma processes died.

5.只有2个puma进程我该怎么办?

如您所见,我对Puma的理解不是很好,并且Puma与Nginx与Capistano之间的区别还不清楚.任何帮助是极大的赞赏.关于此问题,我找不到足够的资源.

As you can see, my understanding of Puma is not great and the lines between what Puma vs Nginx vs Capistano touches is not clear. Any help is greatly appreciated. I haven't been able to find great resources regarding this issue.

推荐答案

这是我所了解的-

  • 如果Puma停止工作,请确保您有足够的内存来处理指定的worker和线程数.每个Puma流程都非常昂贵.
  • 如果将worker设置为0,则Puma将不会在群集模式下运行.建议使用群集模式运行MRI.
  • 每个群集设置
  • 个线程.如果您有2个作品和0,8个线程,则意味着您将有2个作品,每个作品都具有1到8个线程.
  • Puma除了使用线程外还使用进程. Puma具有用于父进程的PID.如果使用的是集群模式,则它具有PID来管理集群.如果使用群集模式,则每个群集还具有一个PID.然后,有固定数量的PID来运行其他任务(每个集群).如果没有群集模式,则有5个固定的PID.在群集模式下,有7个固定的PID.
  • 这就是要说的-如果看到的进程比预期的多,这就是原因.此外,当您添加新工作人员时,还会添加大量昂贵的流程.确保您有空间. 我有一个小应用程序,并且似乎在1个工作者和min = 1,max = 4个线程的情况下运行良好.最多有8条线程似乎是我不断杀死彪马的原因.
  • if Puma stops working, make sure you have enough memory to handle to number of workers and threads that you specified. each Puma process is pretty expensive.
  • if you set 0 workers, Puma will not run in cluster mode. it is recommended to run MRI using cluster mode.
  • threads are set per cluster. if you have 2 works and 0,8 threads that means you will have two works and each will have between 1 and 8 threads.
  • Puma uses processes in addition to the threads. Puma has a PID for the parent process. if you are using cluster mode, it has a PID to manager the clusters. if you are using cluster mode, it also has a PID for each cluster. then, there are a fixed number of PIDs to run other tasks (per cluster). without cluster mode, there are 5 fixed PIDs. with cluster mode, there are 7 fixed PIDs.
  • this is all to say--if you see more processes than you expect, this is why. also--when you add a new worker you add a significant amount of expensive processes. make sure you have the space. i have a small app, and things seem to be working nicely with 1 worker and min=1, max=4 threads. having a max of 8 threads looks to be what kept killing puma for me.

回答我的原始问题-

  1. 是的,上面对worker和thread的解释是正确的.
  2. capistrano-puma似乎只在第一次部署时设置了puma配置.
  3. 我认为nginx配置是在安装nginx时创建的.
  4. 0个工作进程表示您正在运行不带集群模式的puma.不可能有0个线程.我相信0,8与1,8相同.
  5. Puma除了需要您请求的线程外,还需要运行进程.只有两个或三个PID才能运行puma.这些过程运行附加任务.

这篇关于Puma停止使用Nginx在EC2实例上运行Rails应用程序(使用Capistrano/Capistrano Puma)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆